Class: ProductGroup
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ProductGroup
- Defined in:
- app/models/product_group.rb
Overview
ProductGroups are used for creating and managing sets of products. Product group can be either anonymous(adhoc) or named.
Anonymous Product groups are created by combining product scopes generated from url in 2 formats:
/t/*taxons/s/name_of_scope/comma_separated_arguments/name_of_scope_that_doesn_take_any//order
*/s/name_of_scope/comma_separated_arguments/name_of_scope_that_doesn_take_any//order
Named product groups can be created from anonymous ones, lub from another named scope (using ProductGroup.from_url method). Named product groups have pernament urls, that don’t change even after changes to scopes are made, and come in two types.
/t/*taxons/pg/named_product_group
*/pg/named_product_group
first one is used for combining named scope with taxons, named product group can have #in_taxon or #taxons_name_eq scope defined, result should combine both and return products that exist in both taxons.
ProductGroup#dynamic_products returns chain of named scopes generated from order and product scopes. So you can do counting, calculations etc, on resulted set of products, without retriving all records.
ProductGroup operates on named scopes defined for product in Scopes::Product, or generated automatically by meta_search
Class Method Summary collapse
-
.from_url(url) ⇒ Object
Testing utility: creates new ProductGroup from search permalink url.
-
.new_from_products(products, attrs = {}) ⇒ Object
Build a new product group with a scope to filter by specified products.
Instance Method Summary collapse
- #add_scope(scope_name, arguments = []) ⇒ Object
- #apply_on(scopish, use_order = true) ⇒ Object
-
#dynamic_products(use_order = true) ⇒ Object
returns chain of named scopes generated from order scope and product scopes.
- #from_product_group(opg) ⇒ Object
- #from_route(attrs) ⇒ Object
- #from_search(search_hash) ⇒ Object
- #generate_preview(size = ) ⇒ Object
- #include?(product) ⇒ Boolean
- #order_scope ⇒ Object
- #order_scope=(scope_name) ⇒ Object
-
#products(use_order = true) ⇒ Object
Does the final ordering if requested TODO: move the order stuff out of the above - is superfluous now.
- #scopes_to_hash ⇒ Object
- #set_permalink ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
-
#to_url ⇒ Object
generates ProductGroup url.
- #update_memberships ⇒ Object
Class Method Details
.from_url(url) ⇒ Object
Testing utility: creates new ProductGroup from search permalink url. Follows conventions for accessing PGs from URLs, as decoded in routes
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/product_group.rb', line 43 def self.from_url(url) pg = nil; case url when /\/t\/(.+?)\/s\/(.+)/ then taxons = $1; attrs = $2; when /\/t\/(.+?)\/pg\/(.+)/ then taxons = $1; pg_name = $2; when /(.*?)\/s\/(.+)/ then attrs = $2; when /(.*?)\/pg\/(.+)/ then pg_name = $2; else return(nil) end if pg_name && opg = ProductGroup.find_by_permalink(pg_name) pg = new.from_product_group(opg) elsif attrs attrs = url.split("/") pg = new.from_route(attrs) end taxon = taxons && taxons.split("/").last pg.add_scope("in_taxon", taxon) if taxon pg end |
.new_from_products(products, attrs = {}) ⇒ Object
Build a new product group with a scope to filter by specified products
210 211 212 213 214 |
# File 'app/models/product_group.rb', line 210 def self.new_from_products(products, attrs = {}) pg = new(attrs) pg.product_scopes.build(:name => 'with_ids', :arguments => [products.map(&:id).join(',')]) pg end |
Instance Method Details
#add_scope(scope_name, arguments = []) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/product_group.rb', line 92 def add_scope(scope_name, arguments=[]) if scope_name.to_s !~ /eval|send|system|[^a-z0-9_!?]/ self.product_scopes << ProductScope.new({ :name => scope_name.to_s, :arguments => [*arguments] }) else raise ArgumentError.new("'#{scope_name}` can't be used as scope") end self end |
#apply_on(scopish, use_order = true) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/product_group.rb', line 104 def apply_on(scopish, use_order = true) # There's bug in AR, it doesn't merge :order, instead it takes order # from first nested_scope so we have to apply ordering FIRST. # see #2253 on rails LH base_product_scope = scopish if use_order && !self.order_scope.blank? && Product.respond_to?(self.order_scope.intern) base_product_scope = base_product_scope.send(self.order_scope) end return self.product_scopes.reject {|s| s.is_ordering? }.inject(base_product_scope) do |result, scope| scope.apply_on(result) end end |
#dynamic_products(use_order = true) ⇒ Object
returns chain of named scopes generated from order scope and product scopes.
120 121 122 |
# File 'app/models/product_group.rb', line 120 def dynamic_products(use_order = true) apply_on(Product.group_by_products_id, use_order) end |
#from_product_group(opg) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'app/models/product_group.rb', line 65 def from_product_group(opg) self.product_scopes = opg.product_scopes.map{|ps| ps = ps.clone; ps.product_group_id = nil; ps.product_group = self; ps } self end |
#from_route(attrs) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'app/models/product_group.rb', line 75 def from_route(attrs) self.order_scope = attrs.pop if attrs.length % 2 == 1 attrs.each_slice(2) do |scope| next unless Product.respond_to?(scope.first) add_scope(scope.first, scope.last.split(",")) end self end |
#from_search(search_hash) ⇒ Object
84 85 86 87 88 89 90 |
# File 'app/models/product_group.rb', line 84 def from_search(search_hash) search_hash.each_pair do |scope_name, scope_attribute| add_scope(scope_name, scope_attribute) end self end |
#generate_preview(size = ) ⇒ Object
181 182 183 184 185 |
# File 'app/models/product_group.rb', line 181 def generate_preview(size = Spree::Config[:admin_pgroup_preview_size]) count = self.class.count_by_sql ["SELECT COUNT(*) FROM product_groups_products WHERE product_groups_products.product_group_id = ?", self] return count, products.limit(size) end |
#include?(product) ⇒ Boolean
141 142 143 144 |
# File 'app/models/product_group.rb', line 141 def include?(product) res = apply_on(Product.id_equals(product.id), false) res.count > 0 end |
#order_scope ⇒ Object
195 196 197 198 199 |
# File 'app/models/product_group.rb', line 195 def order_scope if scope = product_scopes.detect {|s| s.is_ordering?} scope.name end end |
#order_scope=(scope_name) ⇒ Object
201 202 203 204 205 206 207 |
# File 'app/models/product_group.rb', line 201 def order_scope=(scope_name) if scope = product_scopes.detect {|s| s.is_ordering?} scope.update_attribute(:name, scope_name) else self.product_scopes.build(:name => scope_name, :arguments => []) end end |
#products(use_order = true) ⇒ Object
Does the final ordering if requested TODO: move the order stuff out of the above - is superfluous now
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/product_group.rb', line 126 def products(use_order = true) cached_group = Product.in_cached_group(self) if cached_group.limit(1).blank? dynamic_products(use_order) elsif !use_order cached_group else product_scopes.select {|s| s.is_ordering? }.inject(cached_group) {|res,order| order.apply_on(res) } end end |
#scopes_to_hash ⇒ Object
146 147 148 149 150 151 152 |
# File 'app/models/product_group.rb', line 146 def scopes_to_hash result = {} self.product_scopes.each do |scope| result[scope.name] = scope.arguments end result end |
#set_permalink ⇒ Object
169 170 171 |
# File 'app/models/product_group.rb', line 169 def set_permalink self.permalink = self.name.to_url end |
#to_param ⇒ Object
191 192 193 |
# File 'app/models/product_group.rb', line 191 def to_param self.permalink end |
#to_s ⇒ Object
187 188 189 |
# File 'app/models/product_group.rb', line 187 def to_s "<ProductGroup" + (id && "[#{id}]").to_s + ":'#{to_url}'>" end |
#to_url ⇒ Object
generates ProductGroup url
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/product_group.rb', line 155 def to_url if (new_record? || name.blank?) result = "" result+= self.product_scopes.map{|ps| [ps.name, ps.arguments.join(",")] }.flatten.join('/') result+= self.order_scope if self.order_scope result else name.to_url end end |
#update_memberships ⇒ Object
173 174 175 176 177 178 179 |
# File 'app/models/product_group.rb', line 173 def update_memberships # wipe everything directly to avoid expensive in-rails sorting ActiveRecord::Base.connection.execute "DELETE FROM product_groups_products WHERE product_group_id = #{self.id}" # and generate the new group entirely in SQL ActiveRecord::Base.connection.execute "INSERT INTO product_groups_products #{dynamic_products(false).scoped(:select => "products.id, #{self.id}").to_sql}" end |