Commit 1d10ce614c26dd667efa94b1662c7a2f7281bf8f
1 parent
26332265
Exists in
master
and in
28 other branches
Add has_many for qualifiers/certifiers
Refactor acts_as_searchable and acts_as_faceted for products
Showing
4 changed files
with
54 additions
and
31 deletions
Show diff stats
app/models/certifier.rb
@@ -2,9 +2,12 @@ class Certifier < ActiveRecord::Base | @@ -2,9 +2,12 @@ class Certifier < ActiveRecord::Base | ||
2 | 2 | ||
3 | belongs_to :environment | 3 | belongs_to :environment |
4 | 4 | ||
5 | - has_many :qualifier_certifiers | 5 | + has_many :qualifier_certifiers, :dependent => :destroy |
6 | has_many :qualifiers, :through => :qualifier_certifiers | 6 | has_many :qualifiers, :through => :qualifier_certifiers |
7 | 7 | ||
8 | + has_many :product_qualifiers, :dependent => :destroy | ||
9 | + has_many :products, :through => :product_qualifiers, :source => :product | ||
10 | + | ||
8 | validates_presence_of :environment_id | 11 | validates_presence_of :environment_id |
9 | validates_presence_of :name | 12 | validates_presence_of :name |
10 | 13 |
app/models/product.rb
1 | class Product < ActiveRecord::Base | 1 | class Product < ActiveRecord::Base |
2 | belongs_to :enterprise | 2 | belongs_to :enterprise |
3 | + has_one :region, :through => :enterprise | ||
4 | + | ||
3 | belongs_to :product_category | 5 | belongs_to :product_category |
4 | - has_many :product_categorizations | ||
5 | - has_many :product_qualifiers | ||
6 | - has_many :qualifiers, :through => :product_qualifiers | 6 | + |
7 | has_many :inputs, :dependent => :destroy, :order => 'position' | 7 | has_many :inputs, :dependent => :destroy, :order => 'position' |
8 | 8 | ||
9 | + has_many :product_qualifiers, :dependent => :destroy | ||
10 | + has_many :qualifiers, :through => :product_qualifiers | ||
11 | + has_many :certifiers, :through => :product_qualifiers | ||
12 | + | ||
9 | validates_uniqueness_of :name, :scope => :enterprise_id, :allow_nil => true | 13 | validates_uniqueness_of :name, :scope => :enterprise_id, :allow_nil => true |
10 | validates_presence_of :product_category_id | 14 | validates_presence_of :product_category_id |
11 | validates_associated :product_category | 15 | validates_associated :product_category |
@@ -26,15 +30,6 @@ class Product < ActiveRecord::Base | @@ -26,15 +30,6 @@ class Product < ActiveRecord::Base | ||
26 | p.enterprise.product_updated if p.enterprise | 30 | p.enterprise.product_updated if p.enterprise |
27 | end | 31 | end |
28 | 32 | ||
29 | - after_save do |p| | ||
30 | - if (p.product_category && !ProductCategorization.find(:first, :conditions => {:category_id => p.product_category.id, :product_id => p.id})) || (!p.product_category) | ||
31 | - ProductCategorization.remove_all_for(p) | ||
32 | - if p.product_category | ||
33 | - ProductCategorization.add_category_to_product(p.product_category, p) | ||
34 | - end | ||
35 | - end | ||
36 | - end | ||
37 | - | ||
38 | xss_terminate :only => [ :name ], :on => 'validation' | 33 | xss_terminate :only => [ :name ], :on => 'validation' |
39 | xss_terminate :only => [ :description ], :with => 'white_list', :on => 'validation' | 34 | xss_terminate :only => [ :description ], :with => 'white_list', :on => 'validation' |
40 | 35 | ||
@@ -86,9 +81,11 @@ class Product < ActiveRecord::Base | @@ -86,9 +81,11 @@ class Product < ActiveRecord::Base | ||
86 | end | 81 | end |
87 | 82 | ||
88 | def enterprise_updated(e) | 83 | def enterprise_updated(e) |
89 | - self.lat = e.lat | ||
90 | - self.lng = e.lng | ||
91 | - save! | 84 | + if self.lat != e.lat or self.lng != e.lng |
85 | + self.lat = e.lat | ||
86 | + self.lng = e.lng | ||
87 | + save! | ||
88 | + end | ||
92 | end | 89 | end |
93 | 90 | ||
94 | def url | 91 | def url |
@@ -155,31 +152,51 @@ class Product < ActiveRecord::Base | @@ -155,31 +152,51 @@ class Product < ActiveRecord::Base | ||
155 | def name_or_category | 152 | def name_or_category |
156 | name ? name : product_category.name | 153 | name ? name : product_category.name |
157 | end | 154 | end |
155 | + def price_sort | ||
156 | + (price.nil? or price.zero?) ? 999999999.9 : price | ||
157 | + end | ||
158 | def f_category | 158 | def f_category |
159 | - #def childs(id) | ||
160 | - #ret = ProductCategory.find(:all, :conditions => {:parent_id => id}).collect(&:id) | ||
161 | - #([id, ret.map { |i| childs(i) }]) | ||
162 | - #end | ||
163 | - #childs(self.product_category.id).flatten | ||
164 | self.product_category.name | 159 | self.product_category.name |
165 | end | 160 | end |
166 | def f_region | 161 | def f_region |
167 | - self.enterprise.region.name if self.enterprise.region | 162 | + self.enterprise.region.id if self.enterprise.region |
163 | + end | ||
164 | + def self.f_region_proc(id) | ||
165 | + c = Region.find(id) | ||
166 | + s = c.parent | ||
167 | + if c and c.kind_of?(City) and s and s.kind_of?(State) and s.acronym | ||
168 | + [c.name, ', ' + s.acronym] | ||
169 | + else | ||
170 | + c.name | ||
171 | + end | ||
172 | + end | ||
173 | + def self.f_qualifier_proc(id) | ||
174 | + pq = ProductQualifier.find(id) | ||
175 | + if pq.certifier | ||
176 | + [pq.qualifier.name, _(' cert. ') + pq.certifier.name] | ||
177 | + else | ||
178 | + pq.qualifier.name | ||
179 | + end | ||
168 | end | 180 | end |
169 | def f_qualifier | 181 | def f_qualifier |
170 | - product_qualifiers.collect{|i| i.qualifier.name if i.qualifier} + | ||
171 | - product_qualifiers.collect{|i| i.certifier.name if i.certifier} | 182 | + product_qualifier_ids |
172 | end | 183 | end |
173 | public | 184 | public |
174 | 185 | ||
175 | acts_as_faceted :fields => { | 186 | acts_as_faceted :fields => { |
176 | :f_category => {:label => _('Related products')}, | 187 | :f_category => {:label => _('Related products')}, |
177 | - :f_region => {:label => _('Region')}, | ||
178 | - :f_qualifier => {:label => _('Qualifiers')}}, | 188 | + :f_region => {:label => _('City'), :proc => proc { |id| f_region_proc(id) }}, |
189 | + :f_qualifier => {:label => _('Qualifiers'), :proc => proc { |id| f_qualifier_proc(id) }}}, | ||
190 | + :category_query => proc { |c| "f_category:\"#{c.name}\"" }, | ||
179 | :order => [:f_category, :f_region, :f_qualifier] | 191 | :order => [:f_category, :f_region, :f_qualifier] |
180 | 192 | ||
181 | - acts_as_searchable :fields => [{:name_or_category => {:type => :text, :as => :name, :boost => 2.0}}, | ||
182 | - {:name_or_category => {:type => :string, :as => :name_sort}}, :description, {:price => :float}, :category_full_name ] + facets.keys.map{|i| {i => :facet}}, | 193 | + acts_as_searchable :additional_fields => [ |
194 | + {:name => {:type => :text, :boost => 5.0}}, | ||
195 | + {:price_sort => {:type => :decimal}}, | ||
196 | + {:name_or_category => {:type => :string, :as => :name_or_category_sort, :boost => 2.0}}, | ||
197 | + :category_full_name ] + facets.keys.map{|i| {i => :facet}}, | ||
198 | + :include => [:enterprise, :qualifiers, :certifiers, :product_category], | ||
199 | + :boost => proc {|p| 10 if p.enterprise.enabled}, | ||
183 | :facets => facets.keys | 200 | :facets => facets.keys |
184 | 201 | ||
185 | end | 202 | end |
app/models/qualifier.rb
@@ -2,14 +2,15 @@ class Qualifier < ActiveRecord::Base | @@ -2,14 +2,15 @@ class Qualifier < ActiveRecord::Base | ||
2 | 2 | ||
3 | belongs_to :environment | 3 | belongs_to :environment |
4 | 4 | ||
5 | - has_many :qualifier_certifiers | 5 | + has_many :qualifier_certifiers, :dependent => :destroy |
6 | has_many :certifiers, :through => :qualifier_certifiers | 6 | has_many :certifiers, :through => :qualifier_certifiers |
7 | 7 | ||
8 | + has_many :product_qualifiers, :dependent => :destroy | ||
9 | + has_many :products, :through => :product_qualifiers, :source => :product | ||
10 | + | ||
8 | validates_presence_of :environment_id | 11 | validates_presence_of :environment_id |
9 | validates_presence_of :name | 12 | validates_presence_of :name |
10 | 13 | ||
11 | - has_many :product_qualifiers, :dependent => :destroy | ||
12 | - | ||
13 | def <=>(b) | 14 | def <=>(b) |
14 | self.name.downcase.transliterate <=> b.name.downcase.transliterate | 15 | self.name.downcase.transliterate <=> b.name.downcase.transliterate |
15 | end | 16 | end |
app/models/qualifier_certifier.rb