Commit 51533efa7b3fa16cadee76d48bfdc46c746f1531

Authored by Braulio Bhavamitra
1 parent a6d15687

Remove lat/lng from product and load them from enterprise

app/models/enterprise.rb
@@ -14,7 +14,11 @@ class Enterprise < Organization @@ -14,7 +14,11 @@ class Enterprise < Organization
14 14
15 has_and_belongs_to_many :fans, :class_name => 'Person', :join_table => 'favorite_enteprises_people' 15 has_and_belongs_to_many :fans, :class_name => 'Person', :join_table => 'favorite_enteprises_people'
16 16
  17 + after_save_reindex [:products], :with => :delayed_job
17 extra_data_for_index :product_categories 18 extra_data_for_index :product_categories
  19 + def product_categories
  20 + products.map{|p| p.category_full_name}.compact
  21 + end
18 22
19 N_('Organization website'); N_('Historic and current context'); N_('Activities short description'); N_('City'); N_('State'); N_('Country'); N_('ZIP code') 23 N_('Organization website'); N_('Historic and current context'); N_('Activities short description'); N_('City'); N_('State'); N_('Country'); N_('ZIP code')
20 24
@@ -74,18 +78,6 @@ class Enterprise < Organization @@ -74,18 +78,6 @@ class Enterprise < Organization
74 environment ? environment.signup_enterprise_fields : [] 78 environment ? environment.signup_enterprise_fields : []
75 end 79 end
76 80
77 - def product_categories  
78 - products.map{|p| p.category_full_name}.compact  
79 - end  
80 -  
81 - def product_updated  
82 - solr_save  
83 - end  
84 -  
85 - after_save do |e|  
86 - e.products.each{ |p| p.enterprise_updated(e) }  
87 - end  
88 -  
89 def closed? 81 def closed?
90 true 82 true
91 end 83 end
app/models/product.rb
@@ -24,22 +24,16 @@ class Product < ActiveRecord::Base @@ -24,22 +24,16 @@ class Product < ActiveRecord::Base
24 24
25 after_update :save_image 25 after_update :save_image
26 26
27 - before_create do |p|  
28 - if p.enterprise  
29 - p['lat'] = p.enterprise.lat  
30 - p['lng'] = p.enterprise.lng  
31 - end 27 + def lat
  28 + self.enterprise.lat
32 end 29 end
33 -  
34 - after_save do |p|  
35 - p.enterprise.product_updated if p.enterprise 30 + def lng
  31 + self.enterprise.lng
36 end 32 end
37 33
38 xss_terminate :only => [ :name ], :on => 'validation' 34 xss_terminate :only => [ :name ], :on => 'validation'
39 xss_terminate :only => [ :description ], :with => 'white_list', :on => 'validation' 35 xss_terminate :only => [ :description ], :with => 'white_list', :on => 'validation'
40 36
41 - acts_as_mappable  
42 -  
43 belongs_to :unit 37 belongs_to :unit
44 38
45 include FloatHelper 39 include FloatHelper
@@ -85,14 +79,6 @@ class Product < ActiveRecord::Base @@ -85,14 +79,6 @@ class Product < ActiveRecord::Base
85 self.find(:all, :order => 'id desc', :limit => limit) 79 self.find(:all, :order => 'id desc', :limit => limit)
86 end 80 end
87 81
88 - def enterprise_updated(e)  
89 - if self.lat != e.lat or self.lng != e.lng  
90 - self.lat = e.lat  
91 - self.lng = e.lng  
92 - save!  
93 - end  
94 - end  
95 -  
96 def url 82 def url
97 enterprise.public_profile_url.merge(:controller => 'manage_products', :action => 'show', :id => id) 83 enterprise.public_profile_url.merge(:controller => 'manage_products', :action => 'show', :id => id)
98 end 84 end
@@ -210,13 +196,19 @@ class Product < ActiveRecord::Base @@ -210,13 +196,19 @@ class Product < ActiveRecord::Base
210 url_for({:host => enterprise.default_hostname, :controller => 'manage_products', :action => 'display_inputs_cost', :profile => enterprise.identifier, :id => self.id }.merge(Noosfero.url_options)) 196 url_for({:host => enterprise.default_hostname, :controller => 'manage_products', :action => 'display_inputs_cost', :profile => enterprise.identifier, :id => self.id }.merge(Noosfero.url_options))
211 end 197 end
212 198
213 - private  
214 - def name_or_category  
215 - name ? name : product_category.name  
216 - end  
217 - def price_sort  
218 - (price.nil? or price.zero?) ? 999999999.9 : price 199 + def percentage_from_solidarity_economy
  200 + se_i = t_i = 0
  201 + self.inputs.each{ |i| t_i += 1; se_i += 1 if i.is_from_solidarity_economy }
  202 + p = case (se_i.to_f/t_i)*100
  203 + when 0..24.999 then [0, _("")];
  204 + when 25..49.999 then [25, _("25%")];
  205 + when 50..74.999 then [50, _("50%")];
  206 + when 75..99.999 then [75, _("75%")];
  207 + when 100 then [100, _("100%")];
  208 + end
219 end 209 end
  210 +
  211 + private
220 def f_category 212 def f_category
221 self.product_category.name 213 self.product_category.name
222 end 214 end
@@ -243,11 +235,20 @@ class Product < ActiveRecord::Base @@ -243,11 +235,20 @@ class Product < ActiveRecord::Base
243 "#{pq.qualifier_id} #{pq.certifier_id}" 235 "#{pq.qualifier_id} #{pq.certifier_id}"
244 end 236 end
245 end 237 end
  238 +
  239 + alias_method :name_sortable, :name
  240 + delegate :region, :region_id, :environment, :environment_id, :to => :enterprise
  241 + def name_sortable # give a different name for solr
  242 + name
  243 + end
246 def public 244 def public
247 self.public? 245 self.public?
248 end 246 end
249 - def environment_id  
250 - enterprise.environment_id 247 + def price_sortable
  248 + (price.nil? or price.zero?) ? 999999999.9 : price
  249 + end
  250 + def categories
  251 + enterprise.category_ids << product_category_id
251 end 252 end
252 public 253 public
253 254
@@ -255,19 +256,40 @@ class Product &lt; ActiveRecord::Base @@ -255,19 +256,40 @@ class Product &lt; ActiveRecord::Base
255 :f_category => {:label => _('Related products')}, 256 :f_category => {:label => _('Related products')},
256 :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }}, 257 :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }},
257 :f_qualifier => {:label => _('Qualifiers'), :proc => proc { |id| f_qualifier_proc(id) }}}, 258 :f_qualifier => {:label => _('Qualifiers'), :proc => proc { |id| f_qualifier_proc(id) }}},
258 - :category_query => proc { |c| "f_category:\"#{c.name}\"" }, 259 + :category_query => proc { |c| "categories:#{c.id}" },
259 :order => [:f_category, :f_region, :f_qualifier] 260 :order => [:f_category, :f_region, :f_qualifier]
260 261
261 - acts_as_searchable :additional_fields => [  
262 - {:name => {:type => :text, :boost => 5.0}},  
263 - {:price_sort => {:type => :decimal}},  
264 - {:public => {:type => :boolean}},  
265 - {:environment_id => {:type => :integer}},  
266 - {:name_or_category => {:type => :string, :as => :name_or_category_sort, :boost => 2.0}},  
267 - :category_full_name ] + facets.keys.map{|i| {i => :facet}},  
268 - :include => [:enterprise, :region, :qualifiers, :certifiers, :product_category],  
269 - :boost => proc {|p| 10 if p.enterprise.enabled},  
270 - :facets => facets.keys 262 + Boosts = [
  263 + [:image, 0.4, proc{ |p| p.image ? 1 : 0}],
  264 + [:qualifiers, 0.3, proc{ |p| p.product_qualifiers.count > 0 ? 1 : 0}],
  265 + [:open_price, 0.3, proc{ |p| p.price_described? ? 1 : 0}],
  266 + [:solidarity, 0.3, proc{ |p| p.inputs.count > 0 ? p.percentage_from_solidarity_economy[0]/100 : 0 }],
  267 + [:available, 0.2, proc{ |p| p.available ? 1 : 0}],
  268 + [:price, 0.2, proc{ |p| (!p.price.nil? and p.price > 0) ? 1 : 0}],
  269 + [:new_product, 0.2, proc{ |p| (p.updated_at.to_i - p.created_at.to_i) < 24*3600 ? 1 : 0}],
  270 + [:description, 0.15, proc{ |p| (!p.description.nil? and !p.description.empty?) ? 1 : 0}],
  271 + [:enabled, 0.05, proc{ |p| p.enterprise.enabled ? 1 : 0}],
  272 + ]
  273 +
  274 + acts_as_searchable :fields => facets_fields_for_solr + [
  275 + # searched fields
  276 + {:name => {:type => :text, :boost => 2.0}},
  277 + {:description => :text},
  278 + # filtered fields
  279 + {:public => :boolean}, {:environment_id => :integer},
  280 + {:categories => :integer},
  281 + # ordered/query-boosted fields
  282 + {:price_sortable => :decimal}, {:name_sortable => :string},
  283 + {:lat => :float}, {:lng => :float},
  284 + :updated_at, :created_at,
  285 + ], :include => [
  286 + {:product_category => {:fields => [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
  287 + {:region => {:fields => [:name, :path, :slug, :lat, :lng]}},
  288 + {:enterprise => {:fields => [:name, :identifier, :address, :nickname, :lat, :lng]}},
  289 + {:qualifiers => {:fields => [:name]}},
  290 + {:certifiers => {:fields => [:name]}},
  291 + ], :facets => facets_option_for_solr,
  292 + :boost => proc{ |p| boost = 1; Boosts.each{ |b| boost = boost * (1 - ((1 - b[2].call(p)) * b[1])) }; boost}
271 handle_asynchronously :solr_save 293 handle_asynchronously :solr_save
272 294
273 end 295 end
db/migrate/20120329165742_remove_lat_lng_from_product.rb 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +class RemoveLatLngFromProduct < ActiveRecord::Migration
  2 + def self.up
  3 + remove_column :products, :lat
  4 + remove_column :products, :lng
  5 + end
  6 +
  7 + def self.down
  8 + say "this migration can't be reverted"
  9 + end
  10 +end