Class: ProductGroup

Inherits:
ActiveRecord::Base
  • Object
show all
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 Searchlogic

Class Method Summary collapse

Instance Method Summary collapse

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



202
203
204
205
206
# File 'app/models/product_group.rb', line 202

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
# File 'app/models/product_group.rb', line 92

def add_scope(scope_name, arguments=[])
  self.product_scopes << ProductScope.new({
      :name => scope_name.to_s,
      :arguments => [*arguments]
    })
  self
end

#apply_on(scopish, use_order = true) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/product_group.rb', line 100

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.condition?(self.order_scope)
    base_product_scope = base_product_scope.send(self.order_scope)
  end

  return self.product_scopes.reject {|s|
           s.is_ordering?
         }.inject(base_product_scope){|result, scope|
           scope.apply_on(result)
         }
end

#dynamic_products(use_order = true) ⇒ Object

returns chain of named scopes generated from order scope and product scopes.



117
118
119
# File 'app/models/product_group.rb', line 117

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.condition?(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



178
179
180
181
182
# File 'app/models/product_group.rb', line 178

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

Returns:

  • (Boolean)


138
139
140
141
# File 'app/models/product_group.rb', line 138

def include?(product)
  res = apply_on(Product.id_equals(product.id), false)
  res.count > 0
end

#order_scopeObject



188
189
190
191
192
# File 'app/models/product_group.rb', line 188

def order_scope
  if scope = product_scopes.detect {|s| s.is_ordering?}
    scope.name
  end
end

#order_scope=(scope_name) ⇒ Object



193
194
195
196
197
198
199
# File 'app/models/product_group.rb', line 193

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/product_group.rb', line 123

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_hashObject



143
144
145
146
147
148
149
# File 'app/models/product_group.rb', line 143

def scopes_to_hash
  result = {}
  self.product_scopes.each do |scope|
    result[scope.name] = scope.arguments
  end
  result
end


166
167
168
# File 'app/models/product_group.rb', line 166

def set_permalink
  self.permalink = self.name.to_url
end

#to_sObject



184
185
186
# File 'app/models/product_group.rb', line 184

def to_s
  "<ProductGroup" + (id && "[#{id}]").to_s + ":'#{to_url}'>"
end

#to_urlObject

generates ProductGroup url



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/product_group.rb', line 152

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_membershipsObject



170
171
172
173
174
175
176
# File 'app/models/product_group.rb', line 170

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