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_properties=(p) ⇒ Object
(also: #properties=)
set property associations for the current product p can either be a hash or an array of properties.
Class Method Details
.included(base) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 |
# File 'lib/opensteam/product_base.rb', line 62 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" # 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).
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/opensteam/product_base.rb', line 171 def add_properties(prop) prop.each do |pp| unless inventories.exist_by_properties?( 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
191 192 193 |
# File 'lib/opensteam/product_base.rb', line 191 def clear_properties inventories.collect(&:destroy) end |
#del_properties(p) ⇒ Object
delete properties from the current product
184 185 186 187 |
# File 'lib/opensteam/product_base.rb', line 184 def del_properties(p) return nil if p.empty? return ( i = inventories( p ) ) ? i.collect(&:destroy) : nil ; end |
#is_available? ⇒ Boolean
118 119 120 |
# File 'lib/opensteam/product_base.rb', line 118 def is_available? selected_inventories.last.active && selected_inventories.last.storage > 0 end |
#products ⇒ Object
122 |
# File 'lib/opensteam/product_base.rb', line 122 def products ; [] ; end |
#property_errors ⇒ Object
115 |
# File 'lib/opensteam/product_base.rb', line 115 def property_errors() @property_errors ||= [] end |
#property_errors=(a) ⇒ Object
116 |
# File 'lib/opensteam/product_base.rb', line 116 def property_errors=(a) @property_errors = a end |
#selected_inventory ⇒ Object
112 |
# File 'lib/opensteam/product_base.rb', line 112 def selected_inventory() @selected_inventories ||= nil ; end |
#selected_inventory=(i) ⇒ Object
113 |
# File 'lib/opensteam/product_base.rb', line 113 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
204 205 206 207 208 209 210 |
# File 'lib/opensteam/product_base.rb', line 204 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_properties=(p) ⇒ Object Also known as: properties=
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 administration-views, to update the product-property associations.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/opensteam/product_base.rb', line 141 def set_properties=(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| prop[k] = v.collect { |x| k.classify.constantize.find( x.first ) } } 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 |