Module Opensteam::ProductBase
In: lib/opensteam/product_base.rb

ProductBase Module

Defines all the Product-specific methods and variables. Used to either be included into a model or injected with the "opensteam :product" method.

Methods

Included Modules

Opensteam::Base::Helper Opensteam::Finder

Public Class methods

[Source]

     # File lib/opensteam/product_base.rb, line 78
 78:     def self.included(base)
 79:       base.extend ClassMethods
 80: 
 81:       # call class-methods
 82:       base.class_eval do
 83:         
 84:         include Opensteam::Base::Helper
 85:         include Opensteam::Finder
 86:   
 87:         has_many :properties, :class_name => "Opensteam::Base::PropertyBase",
 88:           :finder_sql => 'SELECT properties.* FROM properties ' +
 89:           'INNER JOIN inventories_properties ON inventories_properties.property_id = properties.id ' +
 90:           'INNER JOIN inventories ON inventories.id = inventories_properties.inventory_id ' +
 91:           'WHERE (( inventories.product_type = "#{self.class}" ) AND ( inventories.product_id = #{id} ) ) ',
 92:           :extend => Opensteam::Base::PropertiesExtension,
 93:           :uniq => true
 94: 
 95: 
 96:         
 97:         
 98:         
 99:         # inventory association
100:         has_many :inventories, :as => :product,
101:           :extend => Opensteam::Base::ExistByPropertiesExtension,
102:           :class_name => "Opensteam::InventoryBase::Inventory"
103: 
104:         has_many :inventories_properties, :through => :inventories, :include => :property
105:         
106:         
107:         
108:         
109:         
110:         
111: 
112:         
113:         
114:   
115:         # holds the properties the product is allowed to have
116:         # used for view
117:         class_inheritable_accessor :_has_property
118:                         
119:         # holds the products the bundle-product is allowed to have
120:         # used for view
121:         class_inheritable_accessor :_has_product
122:       
123:         attr_accessor :selected_inventories
124: 
125:         attr_accessor :property_errors
126:         
127:         alias_method :real_inventories, :inventories
128: 
129:         def inventories( a = [] ) 
130:           #puts "********* IIIIIIINNNNNVENTORRRIEEEEEEEESSS ***********  "
131:           a.empty? ? real_inventories : real_inventories.collect { |x| (x.properties.sort - a.sort).empty? ? x : nil }.compact ;
132:         end
133: 
134:       end
135: 
136:     end

Public Instance methods

Add properties to the current product. For every property (or set of properties) an inventory-object is created (unless an inventory-object for the property (or set of properties) already exists).

[Source]

     # File lib/opensteam/product_base.rb, line 199
199:     def add_properties(prop)
200:       prop.each do |pp|
201:         unless inventories.exist_by_properties?( pp )
202:           i = Opensteam::InventoryBase::Inventory.create( :price => 0, :storage => 0, :active => 0 )
203:           i.properties << pp
204:           inventories << i
205:         end
206:       end
207:       
208:     end

clear all properties for the current product

[Source]

     # File lib/opensteam/product_base.rb, line 219
219:     def clear_properties
220:       inventories.collect(&:destroy)
221:     end

delete properties from the current product

[Source]

     # File lib/opensteam/product_base.rb, line 212
212:     def del_properties(p)
213:       return nil if p.empty?
214:       return ( i = inventories( p ) ) ? i.collect(&:destroy) : nil ;
215:     end

[Source]

     # File lib/opensteam/product_base.rb, line 129
129:         def inventories( a = [] ) 
130:           #puts "********* IIIIIIINNNNNVENTORRRIEEEEEEEESSS ***********  "
131:           a.empty? ? real_inventories : real_inventories.collect { |x| (x.properties.sort - a.sort).empty? ? x : nil }.compact ;
132:         end

[Source]

     # File lib/opensteam/product_base.rb, line 146
146:     def is_available?
147:       selected_inventories.last.active && selected_inventories.last.storage > 0
148:     end

[Source]

     # File lib/opensteam/product_base.rb, line 150
150:     def products ; [] ; end
properties=(p)

Alias for set_properties=

[Source]

     # File lib/opensteam/product_base.rb, line 143
143:     def property_errors() @property_errors ||= [] end

[Source]

     # File lib/opensteam/product_base.rb, line 144
144:     def property_errors=(a) @property_errors = a end

[Source]

     # File lib/opensteam/product_base.rb, line 140
140:     def selected_inventory() @selected_inventories ||= nil ; end

[Source]

     # File lib/opensteam/product_base.rb, line 141
141:     def selected_inventory=(i) @selected_inventories = i ; end

DEPRECATED !!!!!

set products-association for object (virtual attributes) used for product-create/edit-view saves the product first, due to "unsaved associations" error

[Source]

     # File lib/opensteam/product_base.rb, line 232
232:     def set_products=(p)
233:       save
234:       products.delete_all
235:       p.each_pair do |k,v|
236:         v.each_pair { |id,n| products << k.classify.constantize.find(id) rescue nil }
237:       end
238:     end

set property associations for the current product p can either be a hash or an array of properties.

  p = [ PropertyA, PropertyB]
  p = [ [ PropertyA, PropertyB], [ PropertyA, PropertyC] ]

  p = { "Size" => { "1" => 1 },
        "Color" => { "32" => 1 } }

for each property (or set of properties) an invenvory-object is created and associated with the properties.

inventoy-objects which are currently associated with the product and do not match the properties (p) are deleted.

used in the admin-views, to update the product-property associations.

[Source]

     # File lib/opensteam/product_base.rb, line 169
169:     def set_properties=(p)
170:       save
171:       
172:       if p.empty? && properties.empty?
173:         inventories << Opensteam::InventoryBase::Inventory.create( :price => 0, :storage => 0, :active => 0 )
174:         return 
175:       end
176:       
177:       if p.kind_of? Hash
178:         prop = {}
179:         p.each_pair { |k,v| prop[k] = v.collect { |x| k.classify.constantize.find( x.first ) } }
180:         prop = prop.values.perm.collect(&:sort)
181:       else
182:         prop = p
183:       end
184:       
185:       inventories.each do |i|
186:         i.destroy unless prop.include? i.properties.sort
187:       end
188: 
189:       add_properties(prop)
190: 
191:       save
192:     end

[Validate]