diff --git a/app/controllers/admin/reseauxes_controller.rb b/app/controllers/admin/reseauxes_controller.rb index a5135e2..849d7c7 100755 --- a/app/controllers/admin/reseauxes_controller.rb +++ b/app/controllers/admin/reseauxes_controller.rb @@ -46,10 +46,10 @@ class Admin::ReseauxesController < ApplicationController def destroy @reseaux = Reseaux.find(params[:id]) - if !@reseaux.superadmin and @reseaux.destroy - flash[:notice] = "Reseauxe supprimée avec succès." + if @reseaux.destroy + flash[:notice] = "Reseaux supprimée avec succès." else - flash[:error] = "Impossible de supprimer ce reseauxe." + flash[:error] = "Impossible de supprimer ce reseaux." end redirect_to :action => :index diff --git a/app/models/customer.rb b/app/models/customer.rb index 92be9ff..51616df 100755 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -102,9 +102,25 @@ class Customer < ActiveRecord::Base self.generate_token end - + + + before_validation do + + if !self.id + if self.reseaux_token? + + if reseaux = Reseaux.find_by_token(self.reseaux_token) + self.reseaux_token = reseaux.token + + else + errors.add(:reseaux_token, "Ce code de réseau n'est pas valide, merci de vérifier son exactitude. ") + end + end + end + + if !self.id if self.parent_code? @@ -120,7 +136,17 @@ class Customer < ActiveRecord::Base end after_create do - + if self.reseaux_token? + + if reseaux = Reseaux.find_by_token(self.reseaux_token) + self.reseauxes << reseaux + + else + + end + end + + newsgroups = Newsgroup.where(:default_checked => true).all diff --git a/app/models/reseaux.rb b/app/models/reseaux.rb index c10789c..db4174c 100644 --- a/app/models/reseaux.rb +++ b/app/models/reseaux.rb @@ -9,5 +9,20 @@ class Reseaux < ActiveRecord::Base has_many :offer_reseauxes has_many :offers, :through => :offer_reseauxes + before_create do + self.generate_token + end + + + + def generate_token + self.token = loop do + + token = SecureRandom.hex(3) + token = token.upcase + break token unless Reseaux.exists?(token: token) or token.include?("0") or token.include?("O") or token.include?("1") or token.include?("L") or token.include?("I") + end + end + end diff --git a/app/views/admin/reseauxes/_reseaux.html.haml b/app/views/admin/reseauxes/_reseaux.html.haml index 1152656..603c384 100755 --- a/app/views/admin/reseauxes/_reseaux.html.haml +++ b/app/views/admin/reseauxes/_reseaux.html.haml @@ -2,6 +2,10 @@ %tr{:id => reseaux.id} %td = reseaux.name + %td + = reseaux.chef_reseau.pseudo_admin if reseaux.chef_reseau + %td + = reseaux.token %td = reseaux.customers.count diff --git a/app/views/admin/reseauxes/index.html.haml b/app/views/admin/reseauxes/index.html.haml index 7926082..411d8b7 100755 --- a/app/views/admin/reseauxes/index.html.haml +++ b/app/views/admin/reseauxes/index.html.haml @@ -5,6 +5,10 @@ %tr %th Nom + %th + Chef réseau + %th + Code %th Utilisateurs %th{:style => "width:100px"}   diff --git a/app/views/public/customers/_new_form.haml b/app/views/public/customers/_new_form.haml index 3c9b7b2..f5521a9 100755 --- a/app/views/public/customers/_new_form.haml +++ b/app/views/public/customers/_new_form.haml @@ -72,9 +72,16 @@ Votre mot de passe = error + .form-group{:class => ("has-error has-feedback" if @customer.errors[:reseaux_token].size > 0)} + =f.text_field :reseaux_token, :class => "form-control", :placeholder => "Code réseau" + -if @customer.errors[:reseaux_token].uniq + -@customer.errors[:reseaux_token].uniq.each do |error| + + .error + = error - + %p diff --git a/db/migrate/20170523073951_add_token_to_reseauxes.rb b/db/migrate/20170523073951_add_token_to_reseauxes.rb new file mode 100644 index 0000000..532b520 --- /dev/null +++ b/db/migrate/20170523073951_add_token_to_reseauxes.rb @@ -0,0 +1,7 @@ +class AddTokenToReseauxes < ActiveRecord::Migration + def change + add_column :reseauxes, :token, :string + add_column :customers, :reseaux_token, :string + + end +end diff --git a/db/schema.rb b/db/schema.rb index df95cc3..f89a45b 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170517225135) do +ActiveRecord::Schema.define(version: 20170523073951) do create_table "accepted_offers", force: :cascade do |t| t.datetime "created_at", null: false @@ -247,6 +247,7 @@ ActiveRecord::Schema.define(version: 20170517225135) do t.string "role_signataire", limit: 255 t.boolean "referent", limit: 1, default: false t.boolean "chef_reseau", limit: 1, default: false + t.string "reseaux_token", limit: 255 end create_table "data_files", force: :cascade do |t| @@ -604,6 +605,7 @@ ActiveRecord::Schema.define(version: 20170517225135) do t.integer "chef_reseau_id", limit: 4 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "token", limit: 255 end create_table "sessions", force: :cascade do |t|