Adding auto geocoding to the customers

This commit is contained in:
Nicolas VARROT 2015-11-30 12:54:06 +01:00
parent c0cc00975c
commit 11d419aba1
5 changed files with 226 additions and 207 deletions

View File

@ -1,5 +1,7 @@
class Customer < ActiveRecord::Base class Customer < ActiveRecord::Base
# Relationships
has_many :customer_favs has_many :customer_favs
has_many :annonce_favs, :through => :customer_favs, :class_name => "Annonce", :source => :annonce has_many :annonce_favs, :through => :customer_favs, :class_name => "Annonce", :source => :annonce
@ -21,6 +23,8 @@ class Customer < ActiveRecord::Base
has_secure_password has_secure_password
# Validation
validates_presence_of :name validates_presence_of :name
validates_presence_of :firstname validates_presence_of :firstname
@ -28,8 +32,6 @@ class Customer < ActiveRecord::Base
validates_presence_of :phone validates_presence_of :phone
#validates_presence_of :parent_code #validates_presence_of :parent_code
validates_presence_of :password, :on => :create validates_presence_of :password, :on => :create
@ -42,7 +44,20 @@ class Customer < ActiveRecord::Base
validates :need_1 , :presence => true, :if => :step3 validates :need_1 , :presence => true, :if => :step3
# Geocoder
reverse_geocoded_by :latitude, :longitude
geocoded_by :full_address
# Callbacks
# Get lat & lng of the customer only if address has changed
after_validation :geocode, if: ->(customer){
customer.address_changed? or
customer.address2_changed? or
customer.cp_changed? or
customer.city_changed?
}
attr_accessor :force_address, :step2, :step3 attr_accessor :force_address, :step2, :step3
@ -184,22 +199,18 @@ class Customer < ActiveRecord::Base
end end
puts self.binary_side puts self.binary_side
binary_filieul = find_binary_empty(parrain.id,self.binary_side ) binary_filieul = find_binary_empty(parrain.id,self.binary_side )
self.binary_parent_id = binary_filieul.id self.binary_parent_id = binary_filieul.id
self.save(:validate => false) self.save(:validate => false)
end end
end
def full_address
"#{address} #{address2} #{cp} #{city}"
end end
def pseudo def pseudo

View File

@ -13,7 +13,7 @@
%td %td
= "Oui" if customer.enabled = "Oui" if customer.enabled
= "Non" if !customer.enabled
%td %td
=link_to customer.email, "mailto:#{customer.email}" =link_to customer.email, "mailto:#{customer.email}"
@ -23,4 +23,3 @@
=# link_to i(:eye), [:admin, customer] =# link_to i(:eye), [:admin, customer]
= link_to i(:pencil), edit_admin_customer_path(customer) = link_to i(:pencil), edit_admin_customer_path(customer)
= link_to i(:check), validate_admin_customer_path(customer), :data => {:confirm => "Voulez-vous vraiment valider ce compte ?"} if !customer.account_validated = link_to i(:check), validate_admin_customer_path(customer), :data => {:confirm => "Voulez-vous vraiment valider ce compte ?"} if !customer.account_validated

View File

@ -11,6 +11,7 @@
%br %br
NEGOS puisse sadapter à vos besoins NEGOS puisse sadapter à vos besoins
%p Il ne sagit en aucun cas dun engagement %p Il ne sagit en aucun cas dun engagement
%p
=f.inputs do =f.inputs do
=f.hidden_field :step3 =f.hidden_field :step3

View File

@ -0,0 +1,6 @@
class AddLocationToCustomers < ActiveRecord::Migration
def change
add_column :customers, :latitude, :float
add_column :customers, :longitude, :float
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151118213358) do ActiveRecord::Schema.define(version: 20151130111157) do
create_table "admins", force: :cascade do |t| create_table "admins", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 255
@ -153,6 +153,7 @@ ActiveRecord::Schema.define(version: 20151118213358) do
t.string "firstname", limit: 255 t.string "firstname", limit: 255
t.string "organisation", limit: 255 t.string "organisation", limit: 255
t.string "siret", limit: 255 t.string "siret", limit: 255
t.string "tva_number", limit: 255
t.text "bio", limit: 65535 t.text "bio", limit: 65535
t.string "avatar", limit: 255 t.string "avatar", limit: 255
t.string "localisation", limit: 255 t.string "localisation", limit: 255
@ -167,6 +168,9 @@ ActiveRecord::Schema.define(version: 20151118213358) do
t.string "phone", limit: 255 t.string "phone", limit: 255
t.text "attentes", limit: 65535 t.text "attentes", limit: 65535
t.text "suggests", limit: 65535 t.text "suggests", limit: 65535
t.text "need_1", limit: 65535
t.text "need_2", limit: 65535
t.text "need_3", limit: 65535
t.integer "conseil_techniques", limit: 4 t.integer "conseil_techniques", limit: 4
t.integer "conseil_juridiques", limit: 4 t.integer "conseil_juridiques", limit: 4
t.integer "conseil_gestion", limit: 4 t.integer "conseil_gestion", limit: 4
@ -190,12 +194,10 @@ ActiveRecord::Schema.define(version: 20151118213358) do
t.datetime "locked_at" t.datetime "locked_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.text "need_1", limit: 65535
t.text "need_2", limit: 65535
t.text "need_3", limit: 65535
t.string "tva_number", limit: 255
t.boolean "account_validated", limit: 1 t.boolean "account_validated", limit: 1
t.datetime "account_validated_at" t.datetime "account_validated_at"
t.float "latitude", limit: 24
t.float "longitude", limit: 24
end end
create_table "data_files", force: :cascade do |t| create_table "data_files", force: :cascade do |t|