opensteam 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. data/History.txt +0 -0
  2. data/License.txt +4 -0
  3. data/Manifest.txt +131 -0
  4. data/Manifest.txt.bak +132 -0
  5. data/README.txt +92 -0
  6. data/generators/opensteam/opensteam_generator.rb +295 -0
  7. data/generators/opensteam/templates/controllers/administration/customers_controller.rb +23 -0
  8. data/generators/opensteam/templates/controllers/administration/inventories_controller.rb +88 -0
  9. data/generators/opensteam/templates/controllers/administration/orders_controller.rb +61 -0
  10. data/generators/opensteam/templates/controllers/administration_controller.rb +80 -0
  11. data/generators/opensteam/templates/controllers/checkout_controller.rb +142 -0
  12. data/generators/opensteam/templates/controllers/webshop_controller.rb +68 -0
  13. data/generators/opensteam/templates/fixtures/inventories.yml +221 -0
  14. data/generators/opensteam/templates/fixtures/inventories_properties.yml +127 -0
  15. data/generators/opensteam/templates/fixtures/product_animals.yml +73 -0
  16. data/generators/opensteam/templates/fixtures/product_dog_foods.yml +7 -0
  17. data/generators/opensteam/templates/fixtures/properties.yml +42 -0
  18. data/generators/opensteam/templates/helpers/webshop_helper.rb +29 -0
  19. data/generators/opensteam/templates/init/opensteam.rb +30 -0
  20. data/generators/opensteam/templates/migrations/create_addresses.rb +17 -0
  21. data/generators/opensteam/templates/migrations/create_customers.rb +16 -0
  22. data/generators/opensteam/templates/migrations/create_dummy_users.rb +16 -0
  23. data/generators/opensteam/templates/migrations/create_inventories.rb +20 -0
  24. data/generators/opensteam/templates/migrations/create_inventories_properties.rb +14 -0
  25. data/generators/opensteam/templates/migrations/create_order_items.rb +16 -0
  26. data/generators/opensteam/templates/migrations/create_orders.rb +18 -0
  27. data/generators/opensteam/templates/migrations/create_payment_types.rb +13 -0
  28. data/generators/opensteam/templates/migrations/create_properties.rb +12 -0
  29. data/generators/opensteam/templates/migrations/create_sessions.rb +16 -0
  30. data/generators/opensteam/templates/migrations/create_users.rb +19 -0
  31. data/generators/opensteam/templates/models/administration_mailer.rb +18 -0
  32. data/generators/opensteam/templates/models/inventories_property.rb +10 -0
  33. data/generators/opensteam/templates/models/inventory.rb +57 -0
  34. data/generators/opensteam/templates/public/images/bullet_arrow_down.png +0 -0
  35. data/generators/opensteam/templates/public/images/bullet_arrow_up.png +0 -0
  36. data/generators/opensteam/templates/public/images/cancel.png +0 -0
  37. data/generators/opensteam/templates/public/images/folder_database.png +0 -0
  38. data/generators/opensteam/templates/public/images/indicator.gif +0 -0
  39. data/generators/opensteam/templates/public/images/indicator_big.gif +0 -0
  40. data/generators/opensteam/templates/public/images/minus.png +0 -0
  41. data/generators/opensteam/templates/public/images/opensteam_logo_small.jpg +0 -0
  42. data/generators/opensteam/templates/public/images/opensteam_logo_web.jpg +0 -0
  43. data/generators/opensteam/templates/public/images/palette.png +0 -0
  44. data/generators/opensteam/templates/public/images/plus.png +0 -0
  45. data/generators/opensteam/templates/public/images/rails.png +0 -0
  46. data/generators/opensteam/templates/public/index.html +297 -0
  47. data/generators/opensteam/templates/public/stylesheets/webshop.css +262 -0
  48. data/generators/opensteam/templates/tasks/opensteam.rake +114 -0
  49. data/generators/opensteam/templates/views/administration/_address.html.erb +4 -0
  50. data/generators/opensteam/templates/views/administration/_nav.html.erb +11 -0
  51. data/generators/opensteam/templates/views/administration/_update_tree.html.erb +18 -0
  52. data/generators/opensteam/templates/views/administration/admin_info.html.erb +12 -0
  53. data/generators/opensteam/templates/views/administration/customers.html.erb +44 -0
  54. data/generators/opensteam/templates/views/administration/customers/index.html.erb +27 -0
  55. data/generators/opensteam/templates/views/administration/customers/show.html.erb +27 -0
  56. data/generators/opensteam/templates/views/administration/index.html.erb +9 -0
  57. data/generators/opensteam/templates/views/administration/inventories/edit.html.erb +48 -0
  58. data/generators/opensteam/templates/views/administration/inventories/index.html.erb +27 -0
  59. data/generators/opensteam/templates/views/administration/inventories/new.html.erb +41 -0
  60. data/generators/opensteam/templates/views/administration/inventories/show.html.erb +45 -0
  61. data/generators/opensteam/templates/views/administration/login.html.erb +16 -0
  62. data/generators/opensteam/templates/views/administration/order.html.erb +22 -0
  63. data/generators/opensteam/templates/views/administration/orders.html.erb +32 -0
  64. data/generators/opensteam/templates/views/administration/orders/_order_item.html.erb +22 -0
  65. data/generators/opensteam/templates/views/administration/orders/index.html.erb +32 -0
  66. data/generators/opensteam/templates/views/administration/orders/show.html.erb +25 -0
  67. data/generators/opensteam/templates/views/administration/products.html.erb +7 -0
  68. data/generators/opensteam/templates/views/administration/properties.html.erb +7 -0
  69. data/generators/opensteam/templates/views/administration/show_order_item.html.erb +5 -0
  70. data/generators/opensteam/templates/views/administration/signup.html.erb +65 -0
  71. data/generators/opensteam/templates/views/administration/tree.html.erb +15 -0
  72. data/generators/opensteam/templates/views/administration/update_products.html.erb +5 -0
  73. data/generators/opensteam/templates/views/administration/update_properties.html.erb +5 -0
  74. data/generators/opensteam/templates/views/administration/update_tree.rjs +3 -0
  75. data/generators/opensteam/templates/views/administration_mailer/order_admin.erb +27 -0
  76. data/generators/opensteam/templates/views/administration_mailer/order_customer.erb +24 -0
  77. data/generators/opensteam/templates/views/checkout/intro.html.erb +5 -0
  78. data/generators/opensteam/templates/views/checkout/outro.html.erb +6 -0
  79. data/generators/opensteam/templates/views/checkout/payment.html.erb +8 -0
  80. data/generators/opensteam/templates/views/checkout/shipping.html.erb +39 -0
  81. data/generators/opensteam/templates/views/checkout/show_cart.html.erb +13 -0
  82. data/generators/opensteam/templates/views/checkout/update_cart_content.rjs +2 -0
  83. data/generators/opensteam/templates/views/common/_cart.html.erb +11 -0
  84. data/generators/opensteam/templates/views/common/_cart_content.html.erb +19 -0
  85. data/generators/opensteam/templates/views/common/_header.html.erb +15 -0
  86. data/generators/opensteam/templates/views/layouts/administration.html.erb +29 -0
  87. data/generators/opensteam/templates/views/layouts/webshop.html.erb +36 -0
  88. data/generators/opensteam/templates/views/webshop/_cart.html.erb +11 -0
  89. data/generators/opensteam/templates/views/webshop/_cart_content.html.erb +23 -0
  90. data/generators/opensteam/templates/views/webshop/_header.html.erb +15 -0
  91. data/generators/opensteam/templates/views/webshop/_inventory_content.html.erb +12 -0
  92. data/generators/opensteam/templates/views/webshop/add_inventory_to_cart.rjs +2 -0
  93. data/generators/opensteam/templates/views/webshop/add_to_cart_with_parameters.rjs +2 -0
  94. data/generators/opensteam/templates/views/webshop/del_item.rjs +1 -0
  95. data/generators/opensteam/templates/views/webshop/index.html.erb +12 -0
  96. data/generators/opensteam/templates/views/webshop/inventory.rjs +2 -0
  97. data/generators/opensteam/templates/views/webshop/show.html.erb +40 -0
  98. data/generators/opensteam_product/opensteam_product_generator.rb +115 -0
  99. data/generators/opensteam_product/templates/controllers/product_controller.rb +104 -0
  100. data/generators/opensteam_product/templates/migration.rb +16 -0
  101. data/generators/opensteam_product/templates/models/product.rb +3 -0
  102. data/generators/opensteam_product/templates/views/_attr_product.html.erb +8 -0
  103. data/generators/opensteam_product/templates/views/_attr_property.html.erb +9 -0
  104. data/generators/opensteam_product/templates/views/_details.html.erb +29 -0
  105. data/generators/opensteam_product/templates/views/administration/_attributes.html.erb +21 -0
  106. data/generators/opensteam_product/templates/views/administration/_get_products.html.erb +8 -0
  107. data/generators/opensteam_product/templates/views/administration/_inventories.html.erb +27 -0
  108. data/generators/opensteam_product/templates/views/administration/edit.html.erb +13 -0
  109. data/generators/opensteam_product/templates/views/administration/index.html.erb +36 -0
  110. data/generators/opensteam_product/templates/views/administration/new.html.erb +16 -0
  111. data/generators/opensteam_product/templates/views/administration/show.html.erb +15 -0
  112. data/generators/opensteam_property/opensteam_property_generator.rb +126 -0
  113. data/generators/opensteam_property/templates/controllers/property_controller.rb +86 -0
  114. data/generators/opensteam_property/templates/migration.rb +14 -0
  115. data/generators/opensteam_property/templates/models/property.rb +3 -0
  116. data/generators/opensteam_property/templates/views/administration/edit.html.erb +19 -0
  117. data/generators/opensteam_property/templates/views/administration/index.html.erb +24 -0
  118. data/generators/opensteam_property/templates/views/administration/new.html.erb +18 -0
  119. data/generators/opensteam_property/templates/views/administration/show.html.erb +10 -0
  120. data/lib/opensteam.rb +49 -0
  121. data/lib/opensteam/base.rb +121 -0
  122. data/lib/opensteam/checkout.rb +89 -0
  123. data/lib/opensteam/config.rb +31 -0
  124. data/lib/opensteam/finder.rb +145 -0
  125. data/lib/opensteam/inventory_base.rb +79 -0
  126. data/lib/opensteam/order_base.rb +58 -0
  127. data/lib/opensteam/product_base.rb +222 -0
  128. data/lib/opensteam/property_base.rb +63 -0
  129. data/lib/opensteam/shopping_cart.rb +343 -0
  130. data/lib/opensteam/user_base.rb +165 -0
  131. data/lib/opensteam/version.rb +9 -0
  132. data/test/test_helper.rb +2 -0
  133. data/test/test_opensteam.rb +11 -0
  134. metadata +189 -0
@@ -0,0 +1,31 @@
1
+ module Opensteam
2
+
3
+
4
+
5
+ # Config Module for Opensteam
6
+ #
7
+ #
8
+ module Config
9
+
10
+
11
+ PRODUCT_BASE_TABLE_PREFIX = :product
12
+
13
+ module Errors
14
+ # generic error class
15
+ class OpensteamError < StandardError
16
+ end
17
+
18
+ # NotAProduct Error
19
+ # raised if Object is not a product (make "constantize" secure..)
20
+ class NotAProduct < OpensteamError
21
+ end
22
+
23
+ # NotAProperty
24
+ # raised if Object is not a property
25
+ class NotAProperty < OpensteamError
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,145 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'opensteam/config'
5
+
6
+ module Opensteam
7
+
8
+
9
+ # Finder Module
10
+ #
11
+ #
12
+ module Finder
13
+
14
+
15
+ def self.included(base) #:nodoc:
16
+ base.extend(ClassMethods)
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+
22
+ # method is invoked before adding a product to the cart
23
+ #
24
+ # finds the product, specified in h and saves the inventory, based on the
25
+ # selected properties, in the "selected_inventory" var.
26
+ #
27
+ # h is the params[:product] Hash,:
28
+ # params[:product] = { :class => "ProductClass", :id => 1,
29
+ # :properties => { "Color" => 2, "Size" => 5 }
30
+ # }
31
+ #
32
+ def find_product_with_inventory( h )
33
+
34
+ product = find_product_by_id( h[:class], h[:id] )
35
+ props = h[:properties] ? h[:properties].collect { |x| find_property_by_id( x.first, x.last ) } : []
36
+ product.selected_inventory = product.inventories( props )
37
+ product
38
+
39
+ end
40
+
41
+
42
+ # find all product tables (tables prefixed with Opensteam::Config::PRODUCT_BASE_TABLE_PREFIX)
43
+ # TODO : implement paginate for finder
44
+ def find_product_tables
45
+ find_tables("product")
46
+ end
47
+
48
+
49
+ # find all products
50
+ def find_products
51
+ find_product_tables.inject([]) { |r,v| r += v.classify.constantize.find(:all) }
52
+ end
53
+
54
+
55
+ # find property tables
56
+ def find_property_tables
57
+ Opensteam::Base::PropertyBase.properties.collect(&:tableize)
58
+ # find_tables("property")
59
+ end
60
+
61
+ # find all properties
62
+ def find_properties
63
+ Properties.find(:all)
64
+ # find_property_tables.inject([]) { |r,v| r += v.classify.constantize.find(:all) }
65
+ end
66
+
67
+
68
+ # find all products of the specified Class
69
+ def find_product(klass)
70
+ contantize_pro(:product, klass).find(:all)
71
+ end
72
+
73
+ # find a product specified by klass and id
74
+ def find_product_by_id(klass,id)
75
+ return nil if id.empty?
76
+ contantize_pro(:product,klass).find(id)
77
+ end
78
+
79
+ # find all properties of the specified Class
80
+ def find_property(klass)
81
+ contantize_pro(:property,klass).find(:all)
82
+ end
83
+
84
+ # find a property by klass and id
85
+ def find_property_by_id(klass,id)
86
+ return nil if id.empty?
87
+ contantize_pro(:property, klass).find(id)
88
+ end
89
+
90
+ # checks if the "inventories_properties" table exists
91
+ def inventories_properties_exist?
92
+ not ActiveRecord::Base.connection.select_all("SHOW TABLES LIKE 'inventories_properties'").empty?
93
+ end
94
+
95
+ # checks if the properties table exists
96
+ def properties_table_exists?
97
+ not ActiveRecord::Base.connection.select_all("SHOW TABLES LIKE 'properties'").empty?
98
+ end
99
+
100
+
101
+
102
+
103
+
104
+ # TODO implement STI support for products
105
+ private
106
+
107
+
108
+ def contantize_pro(s, klass ) #:nodoc:
109
+ if klass.classify.constantize.respond_to?("is_#{s.to_s}?") && klass.classify.constantize.send("is_#{s.to_s}?")
110
+ return klass.classify.constantize
111
+ else
112
+ raise Opensteam::Config::Errors::NotAProduct, "#{klass} is not a product, sorry .." if s == :product
113
+ raise Opensteam::Config::Errors::NotAProperty, "#{klass} is not a property, sorry .." if s == :property
114
+ end
115
+ end
116
+
117
+ def find_tables(type) #:nodoc:
118
+ prefix =
119
+ case type
120
+ when "property"
121
+ Opensteam::Config::PROPERTY_BASE_TABLE_PREFIX.to_s
122
+ when "product"
123
+ Opensteam::Config::PRODUCT_BASE_TABLE_PREFIX.to_s
124
+ end
125
+ tables = ActiveRecord::Base.connection.select_all("SHOW TABLES LIKE '#{prefix}\\_%'").collect(&:values).flatten.collect {
126
+ |t| t.gsub("#{prefix}_", "") }
127
+ end
128
+
129
+
130
+
131
+
132
+
133
+ end #class methods
134
+
135
+ end #Finder
136
+
137
+
138
+ # dummy class to access the finder-methods without including the model.
139
+ class Find
140
+ include Opensteam::Finder
141
+ end
142
+
143
+
144
+
145
+ end #Opensteam
@@ -0,0 +1,79 @@
1
+ module Opensteam
2
+
3
+
4
+
5
+ =begin rdoc
6
+ InventoryBase Module
7
+
8
+ - defines the InventoriesProperty Join Model (joins Opensteam::InventoryBase::Inventory with Opensteam::Base::PropertyBase ).
9
+ - defins the Inventory Model
10
+
11
+ =end
12
+ module InventoryBase
13
+
14
+ # Join Model for Inventory and ProductBase
15
+ class InventoriesProperty < ActiveRecord::Base
16
+ belongs_to :inventory, :class_name => "Opensteam::InventoryBase::Inventory"
17
+ belongs_to :property, :class_name => "Opensteam::Base::PropertyBase"
18
+
19
+ alias :real_property :property
20
+ def property() return !property_type ? real_property : property_type.constantize.find(:all) ; end
21
+
22
+ end
23
+
24
+
25
+
26
+
27
+ # Inventory Model
28
+ class Inventory < ActiveRecord::Base
29
+
30
+ has_many :order_items, :class_name => "Opensteam::OrderBase::OrderItem"
31
+
32
+ belongs_to :product, :polymorphic => true
33
+
34
+ has_many :inventories_properties
35
+ has_many :properties, :through => :inventories_properties
36
+
37
+ validates_presence_of :price, :storage
38
+
39
+
40
+
41
+ # alias :real_properties :properties
42
+ # def properties
43
+ # inventories_properties.collect(&:property).flatten
44
+ # end
45
+
46
+
47
+ # define property accessor methods, like "Inventory.find(:first).colors"
48
+ def self.define_property_accessors
49
+ Opensteam::Base::PropertyBase.properties.each do |accessor|
50
+ self.class_eval do
51
+ define_method( accessor.tableize ) { accessor.classify.constantize.find(:all) }
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+ # check if inventory-object is active
58
+ def is_active?() active == 1 ; end
59
+
60
+ # checks if inventory-object is available
61
+ def is_available?() storage > 0 && is_active? ; end
62
+
63
+
64
+ end
65
+
66
+
67
+
68
+
69
+ end
70
+
71
+
72
+
73
+
74
+
75
+
76
+ end
77
+
78
+
79
+
@@ -0,0 +1,58 @@
1
+ module Opensteam
2
+
3
+
4
+ # OrderBase Module
5
+ #
6
+ # defines all Order specific Classes
7
+ #
8
+ #
9
+ module OrderBase
10
+
11
+
12
+
13
+ # Order Class
14
+ #
15
+ #
16
+ class Order < ActiveRecord::Base
17
+ belongs_to :customer, :class_name => 'Opensteam::UserBase::Customer'
18
+ has_many :order_items
19
+
20
+ belongs_to :payment_address, :class_name => 'Opensteam::UserBase::Address', :foreign_key => 'payment_address_id'
21
+ belongs_to :shipping_address, :class_name => 'Opensteam::UserBase::Address', :foreign_key => 'shipping_address_id'
22
+
23
+ belongs_to :payment_type
24
+
25
+
26
+ def copy_from_cart( cart )
27
+ cart.items.each do |v|
28
+ li = OrderItem.new_item(v)
29
+ order_items << li
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+
36
+ # OrderItem Class
37
+ #
38
+ #
39
+ class OrderItem < ActiveRecord::Base
40
+ belongs_to :order
41
+ belongs_to :inventory , :class_name => "Opensteam::InventoryBase::Inventory"
42
+
43
+
44
+
45
+ def self.new_item(i)
46
+ returning(self.new) do |li|
47
+ li.itemid = i.yamlid
48
+ li.quantity = i.quantity
49
+ li.inventory = i.inventory
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
@@ -0,0 +1,222 @@
1
+ module Opensteam
2
+
3
+ =begin rdoc
4
+
5
+ ProductBase Module
6
+
7
+ Defines all the Product-specific methods and variables.
8
+ Used to either be included into a model or injected with the
9
+ "opensteam :product" method.
10
+
11
+ =end
12
+
13
+ module ProductBase
14
+
15
+
16
+
17
+ module ClassMethods #:nodoc:
18
+
19
+
20
+ # define table_name and prefix
21
+ # all product-tables are prefixed with "product_"
22
+ def table_prefix() Opensteam::Config::PRODUCT_BASE_TABLE_PREFIX.to_s ; end
23
+ def table_name() self.table_prefix + "_" + self.to_s.tableize ; end
24
+
25
+ def has_property(p = nil, opt = nil )
26
+ p ? ( self._has_property ||= {} ).store(p, opt) : ( self._has_property ||= {} )
27
+ end
28
+
29
+ def has_product(p) (self._has_product ||= []) << p if p ; end
30
+
31
+
32
+
33
+ # get all property-objects (records) for current product
34
+ # to define the properties the product is allowed to have use:
35
+ # - *has_property :property_class* in ProductModel
36
+ # default: all properties
37
+ #
38
+ def get_has_property
39
+ (self._has_property.nil? ? self.find_property_tables : self._has_property).inject({}) { |r,v|
40
+ r.merge!( { v => v.to_s.classify.constantize.find(:all) } ) unless v.first == :none } || {}
41
+ end
42
+
43
+ # get all product-objects (records) for current product (used for bundle-products)
44
+ # to define the products the current bundle-product is allowed to have use:
45
+ # - *has_product :peoduct_model* in ProductModel
46
+ # default: no products
47
+ #
48
+ def get_has_products
49
+ products = (self._has_product || []).inject({}) { |r,v|
50
+ r.merge!( { v => v.to_s.classify.constantize.find(:all) } ) } || {}
51
+
52
+ products.delete(self.to_s.tableize) #unless <%= class_name %>.self.referential
53
+
54
+ products
55
+ end
56
+
57
+
58
+ end
59
+
60
+
61
+
62
+ def self.included(base)
63
+ base.extend ClassMethods
64
+
65
+ # call class-methods
66
+ base.class_eval do
67
+
68
+ include Opensteam::Base::Helper
69
+ include Opensteam::Finder
70
+
71
+ has_many :properties, :class_name => "Opensteam::Base::PropertyBase",
72
+ :finder_sql => 'SELECT properties.* FROM properties ' +
73
+ 'INNER JOIN inventories_properties ON inventories_properties.property_id = properties.id ' +
74
+ 'INNER JOIN inventories ON inventories.id = inventories_properties.inventory_id ' +
75
+ 'WHERE (( inventories.product_type = "#{self.class}" ) AND ( inventories.product_id = #{id} ) ) ',
76
+ :extend => Opensteam::Base::PropertiesExtension,
77
+ :uniq => true
78
+
79
+
80
+
81
+ # inventory association
82
+ has_many :inventories, :as => :product,
83
+ :extend => Opensteam::Base::ExistByPropertiesExtension,
84
+ :class_name => "Opensteam::InventoryBase::Inventory"
85
+
86
+
87
+ # holds the properties the product is allowed to have
88
+ # used for view
89
+ class_inheritable_accessor :_has_property
90
+
91
+ # holds the products the bundle-product is allowed to have
92
+ # used for view
93
+ class_inheritable_accessor :_has_product
94
+
95
+ attr_accessor :selected_inventories
96
+
97
+ attr_accessor :property_errors
98
+
99
+ alias_method :real_inventories, :inventories
100
+
101
+ def inventories( a = [] )
102
+ #puts "********* IIIIIIINNNNNVENTORRRIEEEEEEEESSS *********** "
103
+ a.empty? ? real_inventories : real_inventories.collect { |x| (x.properties.sort - a.sort).empty? ? x : nil }.compact ;
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+
110
+
111
+
112
+ def selected_inventory() @selected_inventories ||= nil ; end
113
+ def selected_inventory=(i) @selected_inventories = i ; end
114
+
115
+ def property_errors() @property_errors ||= [] end
116
+ def property_errors=(a) @property_errors = a end
117
+
118
+ def is_available?
119
+ selected_inventories.last.active && selected_inventories.last.storage > 0
120
+ end
121
+
122
+ def products ; [] ; end
123
+
124
+ # set property associations for the current product
125
+ # p can either be a hash or an array of properties.
126
+ #
127
+ # p = [ PropertyA, PropertyB]
128
+ # p = [ [ PropertyA, PropertyB], [ PropertyA, PropertyC] ]
129
+ #
130
+ # p = { "Size" => { "1" => 1 },
131
+ # "Color" => { "32" => 1 } }
132
+ #
133
+ # for each property (or set of properties) an invenvory-object is created and associated
134
+ # with the properties.
135
+ #
136
+ # inventoy-objects which are currently associated with the product and do not
137
+ # match the properties (p) are deleted.
138
+ #
139
+ # used in the administration-views, to update the product-property associations.
140
+ #
141
+ def set_properties=(p)
142
+ save
143
+
144
+ if p.empty? && properties.empty?
145
+ inventories << Opensteam::InventoryBase::Inventory.create( :price => 0, :storage => 0, :active => 0 )
146
+ return
147
+ end
148
+
149
+ if p.kind_of? Hash
150
+ prop = {}
151
+ p.each_pair { |k,v| prop[k] = v.collect { |x| k.classify.constantize.find( x.first ) } }
152
+ prop = prop.values.perm.collect(&:sort)
153
+ else
154
+ prop = p
155
+ end
156
+
157
+ inventories.each do |i|
158
+ i.destroy unless prop.include? i.properties.sort
159
+ end
160
+
161
+ add_properties(prop)
162
+
163
+ save
164
+ end
165
+
166
+ alias :properties= :set_properties=
167
+
168
+
169
+ # Add properties to the current product.
170
+ # 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
+ def add_properties(prop)
172
+ prop.each do |pp|
173
+ unless inventories.exist_by_properties?( pp )
174
+ i = Opensteam::InventoryBase::Inventory.create( :price => 0, :storage => 0, :active => 0 )
175
+ i.properties << pp
176
+ inventories << i
177
+ end
178
+ end
179
+
180
+ end
181
+
182
+
183
+ # delete properties from the current product
184
+ def del_properties(p)
185
+ return nil if p.empty?
186
+ return ( i = inventories( p ) ) ? i.collect(&:destroy) : nil ;
187
+ end
188
+
189
+
190
+ # clear all properties for the current product
191
+ def clear_properties
192
+ inventories.collect(&:destroy)
193
+ end
194
+
195
+
196
+
197
+ # DEPRECATED !!!!!
198
+ #
199
+ #
200
+ # set products-association for object (virtual attributes)
201
+ # used for product-create/edit-view
202
+ # saves the product first, due to "unsaved associations" error
203
+ #
204
+ def set_products=(p)
205
+ save
206
+ products.delete_all
207
+ p.each_pair do |k,v|
208
+ v.each_pair { |id,n| products << k.classify.constantize.find(id) rescue nil }
209
+ end
210
+ end
211
+
212
+ end
213
+
214
+
215
+
216
+
217
+
218
+
219
+ end
220
+
221
+
222
+