Module: Opensteam::ProductBase
- Included in:
- Base::ProductBase
- Defined in:
- lib/opensteam/product_base.rb
Overview
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.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#add_properties(prop) ⇒ Object
Add properties to the current product.
-
#clear_properties ⇒ Object
clear all properties for the current product.
-
#del_properties(p) ⇒ Object
delete properties from the current product.
- #is_available? ⇒ Boolean
- #products ⇒ Object
- #property_errors ⇒ Object
- #property_errors=(a) ⇒ Object
- #selected_inventory ⇒ Object
- #selected_inventory=(i) ⇒ Object
-
#set_products=(p) ⇒ Object
DEPRECATED !!!!!.
-
#set_properties2=(p) ⇒ Object
set property associations for the current product p can either be a hash or an array of properties.
- #set_properties=(p) ⇒ Object (also: #properties=)
Class Method Details
.included(base) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/opensteam/product_base.rb', line 78 def self.included(base) base.extend ClassMethods # call class-methods base.class_eval do include Opensteam::Base::Helper include Opensteam::Finder has_many :properties, :class_name => "Opensteam::Base::PropertyBase", :finder_sql => 'SELECT properties.* FROM properties ' + 'INNER JOIN inventories_properties ON inventories_properties.property_id = properties.id ' + 'INNER JOIN inventories ON inventories.id = inventories_properties.inventory_id ' + 'WHERE (( inventories.product_type = "#{self.class}" ) AND ( inventories.product_id = #{id} ) ) ', :extend => Opensteam::Base::PropertiesExtension, :uniq => true # inventory association has_many :inventories, :as => :product, :extend => Opensteam::Base::ExistByPropertiesExtension, :class_name => "Opensteam::InventoryBase::Inventory" has_many :inventories_properties, :through => :inventories, :include => :property # holds the properties the product is allowed to have # used for view class_inheritable_accessor :_has_property # holds the products the bundle-product is allowed to have # used for view class_inheritable_accessor :_has_product attr_accessor :selected_inventories attr_accessor :property_errors alias_method :real_inventories, :inventories def inventories( a = [] ) #puts "********* IIIIIIINNNNNVENTORRRIEEEEEEEESSS *********** " a.empty? ? real_inventories : real_inventories.collect { |x| (x.properties.sort - a.sort).empty? ? x : nil }.compact ; end end end |
Instance Method Details
#add_properties(prop) ⇒ Object
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).
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/opensteam/product_base.rb', line 234 def add_properties(prop) prop.each do |pp| unless self.inventories.exist_by_properties?( pp.is_a?( Array ) ? pp : [pp] ) i = Opensteam::InventoryBase::Inventory.create( :price => 0, :storage => 0, :active => 0 ) i.properties << pp inventories << i end end end |
#clear_properties ⇒ Object
clear all properties for the current product
255 256 257 |
# File 'lib/opensteam/product_base.rb', line 255 def clear_properties inventories.collect(&:destroy) end |
#del_properties(p) ⇒ Object
delete properties from the current product
247 248 249 250 251 |
# File 'lib/opensteam/product_base.rb', line 247 def del_properties(p) p = Array(p) return nil if p.empty? return ( i = inventories( p ) ) ? i.collect(&:destroy) : nil ; end |
#is_available? ⇒ Boolean
140 141 142 |
# File 'lib/opensteam/product_base.rb', line 140 def is_available? selected_inventories.active && selected_inventories.storage > 0 end |
#products ⇒ Object
144 |
# File 'lib/opensteam/product_base.rb', line 144 def products ; [] ; end |
#property_errors ⇒ Object
137 |
# File 'lib/opensteam/product_base.rb', line 137 def property_errors() @property_errors ||= [] end |
#property_errors=(a) ⇒ Object
138 |
# File 'lib/opensteam/product_base.rb', line 138 def property_errors=(a) @property_errors = a end |
#selected_inventory ⇒ Object
134 |
# File 'lib/opensteam/product_base.rb', line 134 def selected_inventory() @selected_inventories ||= nil ; end |
#selected_inventory=(i) ⇒ Object
135 |
# File 'lib/opensteam/product_base.rb', line 135 def selected_inventory=(i) @selected_inventories = i ; end |
#set_products=(p) ⇒ Object
DEPRECATED !!!!!
set products-association for object (virtual attributes) used for product-create/edit-view saves the product first, due to “unsaved associations” error
268 269 270 271 272 273 274 |
# File 'lib/opensteam/product_base.rb', line 268 def set_products=(p) save products.delete_all p.each_pair do |k,v| v.each_pair { |id,n| products << k.classify.constantize.find(id) rescue nil } end end |
#set_properties2=(p) ⇒ Object
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.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/opensteam/product_base.rb', line 163 def set_properties2=(p) save if p.empty? && properties.empty? inventories << Opensteam::InventoryBase::Inventory.create( :price => 0, :storage => 0, :active => 0 ) return end if p.kind_of? Hash prop = {} p.each_pair { |k,v| if ( pr = k.classify.constantize ).ancestors.include?( Opensteam::PropertyBase ) prop[ k.to_sym ] = v.collect { |x| pr.find( x ) } else raise Opensteam::Config::Errors::NotAProperty, "'#{k.classify}' is not a property" end } prop = prop.values.perm.collect(&:sort) else prop = p end inventories.each do |i| i.destroy unless prop.include? i.properties.sort end add_properties(prop) save end |
#set_properties=(p) ⇒ Object Also known as: properties=
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/opensteam/product_base.rb', line 197 def set_properties= p if p.empty? && properties.empty? self.inventories.build( :price => 0, :storage => 0, :active => 0 ) return end if p.is_a? Hash prop = {} p.each_pair { |k,v| if ( pr = k.classify.constantize ) < Opensteam::PropertyBase prop[ k.to_sym ] = v.collect { |x| pr.find( x ) } else raise Opensteam::Config::Errors::NotAProperty, "'#{k.classify}' is not a property" end } prop = prop.values.perm.collect(&:sort) else prop = p end inventories.each do |i| i.destroy unless prop.include? i.properties.sort end prop.each do |pp| if self.inventories.by_properties( pp.is_a?( Array ) ? pp : [pp] ).empty? # unless self.inventories.by_properties?( pp.is_a?( Array ) ? pp : [pp] ) self.inventories.build( :price => 0, :storage => 0, :active => 0, :properties => pp ) end end end |