Commit be5eca415f2079b82573e11dbba3af39003cce44
1 parent
5efa4c1c
Exists in
master
and in
29 other branches
Small fixes for searchable classes (Article, Product, Profile)
Showing
3 changed files
with
25 additions
and
22 deletions
Show diff stats
app/models/article.rb
@@ -28,7 +28,7 @@ class Article < ActiveRecord::Base | @@ -28,7 +28,7 @@ class Article < ActiveRecord::Base | ||
28 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] | 28 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] |
29 | has_many :categories, :through => :article_categorizations | 29 | has_many :categories, :through => :article_categorizations |
30 | 30 | ||
31 | - has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization' | 31 | + has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization', :dependent => :destroy |
32 | has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category | 32 | has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category |
33 | 33 | ||
34 | acts_as_having_settings :field => :setting | 34 | acts_as_having_settings :field => :setting |
@@ -104,12 +104,13 @@ class Article < ActiveRecord::Base | @@ -104,12 +104,13 @@ class Article < ActiveRecord::Base | ||
104 | @pending_categorizations ||= [] | 104 | @pending_categorizations ||= [] |
105 | end | 105 | end |
106 | 106 | ||
107 | - def add_category(c) | ||
108 | - if self.id | ||
109 | - ArticleCategorization.add_category_to_article(c, self) | ||
110 | - else | 107 | + def add_category(c, reload=false) |
108 | + if new_record? | ||
111 | pending_categorizations << c | 109 | pending_categorizations << c |
110 | + else | ||
111 | + ArticleCategorization.add_category_to_article(c, self) | ||
112 | end | 112 | end |
113 | + self.categories(reload) | ||
113 | end | 114 | end |
114 | 115 | ||
115 | def category_ids=(ids) | 116 | def category_ids=(ids) |
app/models/product.rb
@@ -198,13 +198,14 @@ class Product < ActiveRecord::Base | @@ -198,13 +198,14 @@ class Product < ActiveRecord::Base | ||
198 | 198 | ||
199 | def percentage_from_solidarity_economy | 199 | def percentage_from_solidarity_economy |
200 | se_i = t_i = 0 | 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, _("")]; | 201 | + self.inputs(true).each{ |i| t_i += 1; se_i += 1 if i.is_from_solidarity_economy } |
202 | + t_i = 1 if t_i == 0 # avoid division by 0 | ||
203 | + p = case (se_i.to_f/t_i)*100 | ||
204 | + when 0..24.999 then [0, _("")]; | ||
204 | when 25..49.999 then [25, _("25%")]; | 205 | when 25..49.999 then [25, _("25%")]; |
205 | when 50..74.999 then [50, _("50%")]; | 206 | when 50..74.999 then [50, _("50%")]; |
206 | when 75..99.999 then [75, _("75%")]; | 207 | when 75..99.999 then [75, _("75%")]; |
207 | - when 100 then [100, _("100%")]; | 208 | + when 100 then [100, _("100%")]; |
208 | end | 209 | end |
209 | end | 210 | end |
210 | 211 | ||
@@ -260,15 +261,15 @@ class Product < ActiveRecord::Base | @@ -260,15 +261,15 @@ class Product < ActiveRecord::Base | ||
260 | :order => [:f_category, :f_region, :f_qualifier] | 261 | :order => [:f_category, :f_region, :f_qualifier] |
261 | 262 | ||
262 | Boosts = [ | 263 | 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}], | 264 | + [:image, 0.55, proc{ |p| p.image ? 1 : 0}], |
265 | + [:qualifiers, 0.45, proc{ |p| p.product_qualifiers.count > 0 ? 1 : 0}], | ||
266 | + [:open_price, 0.45, proc{ |p| p.price_described? ? 1 : 0}], | ||
267 | + [:solidarity, 0.45, proc{ |p| p.percentage_from_solidarity_economy[0].to_f/100 }], | ||
268 | + [:available, 0.35, proc{ |p| p.available ? 1 : 0}], | ||
269 | + [:price, 0.35, proc{ |p| (!p.price.nil? and p.price > 0) ? 1 : 0}], | ||
270 | + [:new_product, 0.35, proc{ |p| (p.updated_at.to_i - p.created_at.to_i) < 24*3600 ? 1 : 0}], | ||
271 | + [:description, 0.3, proc{ |p| !p.description.blank? ? 1 : 0}], | ||
272 | + [:enabled, 0.2, proc{ |p| p.enterprise.enabled ? 1 : 0}], | ||
272 | ] | 273 | ] |
273 | 274 | ||
274 | acts_as_searchable :fields => facets_fields_for_solr + [ | 275 | acts_as_searchable :fields => facets_fields_for_solr + [ |
app/models/profile.rb
@@ -231,12 +231,13 @@ class Profile < ActiveRecord::Base | @@ -231,12 +231,13 @@ class Profile < ActiveRecord::Base | ||
231 | @pending_categorizations ||= [] | 231 | @pending_categorizations ||= [] |
232 | end | 232 | end |
233 | 233 | ||
234 | - def add_category(c) | ||
235 | - if self.id | ||
236 | - ProfileCategorization.add_category_to_profile(c, self) | ||
237 | - else | 234 | + def add_category(c, reload=false) |
235 | + if new_record? | ||
238 | pending_categorizations << c | 236 | pending_categorizations << c |
237 | + else | ||
238 | + ProfileCategorization.add_category_to_profile(c, self) | ||
239 | end | 239 | end |
240 | + self.categories(reload) | ||
240 | end | 241 | end |
241 | 242 | ||
242 | def category_ids=(ids) | 243 | def category_ids=(ids) |