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.
- data/History.txt +0 -0
- data/License.txt +4 -0
- data/Manifest.txt +131 -0
- data/Manifest.txt.bak +132 -0
- data/README.txt +92 -0
- data/generators/opensteam/opensteam_generator.rb +295 -0
- data/generators/opensteam/templates/controllers/administration/customers_controller.rb +23 -0
- data/generators/opensteam/templates/controllers/administration/inventories_controller.rb +88 -0
- data/generators/opensteam/templates/controllers/administration/orders_controller.rb +61 -0
- data/generators/opensteam/templates/controllers/administration_controller.rb +80 -0
- data/generators/opensteam/templates/controllers/checkout_controller.rb +142 -0
- data/generators/opensteam/templates/controllers/webshop_controller.rb +68 -0
- data/generators/opensteam/templates/fixtures/inventories.yml +221 -0
- data/generators/opensteam/templates/fixtures/inventories_properties.yml +127 -0
- data/generators/opensteam/templates/fixtures/product_animals.yml +73 -0
- data/generators/opensteam/templates/fixtures/product_dog_foods.yml +7 -0
- data/generators/opensteam/templates/fixtures/properties.yml +42 -0
- data/generators/opensteam/templates/helpers/webshop_helper.rb +29 -0
- data/generators/opensteam/templates/init/opensteam.rb +30 -0
- data/generators/opensteam/templates/migrations/create_addresses.rb +17 -0
- data/generators/opensteam/templates/migrations/create_customers.rb +16 -0
- data/generators/opensteam/templates/migrations/create_dummy_users.rb +16 -0
- data/generators/opensteam/templates/migrations/create_inventories.rb +20 -0
- data/generators/opensteam/templates/migrations/create_inventories_properties.rb +14 -0
- data/generators/opensteam/templates/migrations/create_order_items.rb +16 -0
- data/generators/opensteam/templates/migrations/create_orders.rb +18 -0
- data/generators/opensteam/templates/migrations/create_payment_types.rb +13 -0
- data/generators/opensteam/templates/migrations/create_properties.rb +12 -0
- data/generators/opensteam/templates/migrations/create_sessions.rb +16 -0
- data/generators/opensteam/templates/migrations/create_users.rb +19 -0
- data/generators/opensteam/templates/models/administration_mailer.rb +18 -0
- data/generators/opensteam/templates/models/inventories_property.rb +10 -0
- data/generators/opensteam/templates/models/inventory.rb +57 -0
- data/generators/opensteam/templates/public/images/bullet_arrow_down.png +0 -0
- data/generators/opensteam/templates/public/images/bullet_arrow_up.png +0 -0
- data/generators/opensteam/templates/public/images/cancel.png +0 -0
- data/generators/opensteam/templates/public/images/folder_database.png +0 -0
- data/generators/opensteam/templates/public/images/indicator.gif +0 -0
- data/generators/opensteam/templates/public/images/indicator_big.gif +0 -0
- data/generators/opensteam/templates/public/images/minus.png +0 -0
- data/generators/opensteam/templates/public/images/opensteam_logo_small.jpg +0 -0
- data/generators/opensteam/templates/public/images/opensteam_logo_web.jpg +0 -0
- data/generators/opensteam/templates/public/images/palette.png +0 -0
- data/generators/opensteam/templates/public/images/plus.png +0 -0
- data/generators/opensteam/templates/public/images/rails.png +0 -0
- data/generators/opensteam/templates/public/index.html +297 -0
- data/generators/opensteam/templates/public/stylesheets/webshop.css +262 -0
- data/generators/opensteam/templates/tasks/opensteam.rake +114 -0
- data/generators/opensteam/templates/views/administration/_address.html.erb +4 -0
- data/generators/opensteam/templates/views/administration/_nav.html.erb +11 -0
- data/generators/opensteam/templates/views/administration/_update_tree.html.erb +18 -0
- data/generators/opensteam/templates/views/administration/admin_info.html.erb +12 -0
- data/generators/opensteam/templates/views/administration/customers.html.erb +44 -0
- data/generators/opensteam/templates/views/administration/customers/index.html.erb +27 -0
- data/generators/opensteam/templates/views/administration/customers/show.html.erb +27 -0
- data/generators/opensteam/templates/views/administration/index.html.erb +9 -0
- data/generators/opensteam/templates/views/administration/inventories/edit.html.erb +48 -0
- data/generators/opensteam/templates/views/administration/inventories/index.html.erb +27 -0
- data/generators/opensteam/templates/views/administration/inventories/new.html.erb +41 -0
- data/generators/opensteam/templates/views/administration/inventories/show.html.erb +45 -0
- data/generators/opensteam/templates/views/administration/login.html.erb +16 -0
- data/generators/opensteam/templates/views/administration/order.html.erb +22 -0
- data/generators/opensteam/templates/views/administration/orders.html.erb +32 -0
- data/generators/opensteam/templates/views/administration/orders/_order_item.html.erb +22 -0
- data/generators/opensteam/templates/views/administration/orders/index.html.erb +32 -0
- data/generators/opensteam/templates/views/administration/orders/show.html.erb +25 -0
- data/generators/opensteam/templates/views/administration/products.html.erb +7 -0
- data/generators/opensteam/templates/views/administration/properties.html.erb +7 -0
- data/generators/opensteam/templates/views/administration/show_order_item.html.erb +5 -0
- data/generators/opensteam/templates/views/administration/signup.html.erb +65 -0
- data/generators/opensteam/templates/views/administration/tree.html.erb +15 -0
- data/generators/opensteam/templates/views/administration/update_products.html.erb +5 -0
- data/generators/opensteam/templates/views/administration/update_properties.html.erb +5 -0
- data/generators/opensteam/templates/views/administration/update_tree.rjs +3 -0
- data/generators/opensteam/templates/views/administration_mailer/order_admin.erb +27 -0
- data/generators/opensteam/templates/views/administration_mailer/order_customer.erb +24 -0
- data/generators/opensteam/templates/views/checkout/intro.html.erb +5 -0
- data/generators/opensteam/templates/views/checkout/outro.html.erb +6 -0
- data/generators/opensteam/templates/views/checkout/payment.html.erb +8 -0
- data/generators/opensteam/templates/views/checkout/shipping.html.erb +39 -0
- data/generators/opensteam/templates/views/checkout/show_cart.html.erb +13 -0
- data/generators/opensteam/templates/views/checkout/update_cart_content.rjs +2 -0
- data/generators/opensteam/templates/views/common/_cart.html.erb +11 -0
- data/generators/opensteam/templates/views/common/_cart_content.html.erb +19 -0
- data/generators/opensteam/templates/views/common/_header.html.erb +15 -0
- data/generators/opensteam/templates/views/layouts/administration.html.erb +29 -0
- data/generators/opensteam/templates/views/layouts/webshop.html.erb +36 -0
- data/generators/opensteam/templates/views/webshop/_cart.html.erb +11 -0
- data/generators/opensteam/templates/views/webshop/_cart_content.html.erb +23 -0
- data/generators/opensteam/templates/views/webshop/_header.html.erb +15 -0
- data/generators/opensteam/templates/views/webshop/_inventory_content.html.erb +12 -0
- data/generators/opensteam/templates/views/webshop/add_inventory_to_cart.rjs +2 -0
- data/generators/opensteam/templates/views/webshop/add_to_cart_with_parameters.rjs +2 -0
- data/generators/opensteam/templates/views/webshop/del_item.rjs +1 -0
- data/generators/opensteam/templates/views/webshop/index.html.erb +12 -0
- data/generators/opensteam/templates/views/webshop/inventory.rjs +2 -0
- data/generators/opensteam/templates/views/webshop/show.html.erb +40 -0
- data/generators/opensteam_product/opensteam_product_generator.rb +115 -0
- data/generators/opensteam_product/templates/controllers/product_controller.rb +104 -0
- data/generators/opensteam_product/templates/migration.rb +16 -0
- data/generators/opensteam_product/templates/models/product.rb +3 -0
- data/generators/opensteam_product/templates/views/_attr_product.html.erb +8 -0
- data/generators/opensteam_product/templates/views/_attr_property.html.erb +9 -0
- data/generators/opensteam_product/templates/views/_details.html.erb +29 -0
- data/generators/opensteam_product/templates/views/administration/_attributes.html.erb +21 -0
- data/generators/opensteam_product/templates/views/administration/_get_products.html.erb +8 -0
- data/generators/opensteam_product/templates/views/administration/_inventories.html.erb +27 -0
- data/generators/opensteam_product/templates/views/administration/edit.html.erb +13 -0
- data/generators/opensteam_product/templates/views/administration/index.html.erb +36 -0
- data/generators/opensteam_product/templates/views/administration/new.html.erb +16 -0
- data/generators/opensteam_product/templates/views/administration/show.html.erb +15 -0
- data/generators/opensteam_property/opensteam_property_generator.rb +126 -0
- data/generators/opensteam_property/templates/controllers/property_controller.rb +86 -0
- data/generators/opensteam_property/templates/migration.rb +14 -0
- data/generators/opensteam_property/templates/models/property.rb +3 -0
- data/generators/opensteam_property/templates/views/administration/edit.html.erb +19 -0
- data/generators/opensteam_property/templates/views/administration/index.html.erb +24 -0
- data/generators/opensteam_property/templates/views/administration/new.html.erb +18 -0
- data/generators/opensteam_property/templates/views/administration/show.html.erb +10 -0
- data/lib/opensteam.rb +49 -0
- data/lib/opensteam/base.rb +121 -0
- data/lib/opensteam/checkout.rb +89 -0
- data/lib/opensteam/config.rb +31 -0
- data/lib/opensteam/finder.rb +145 -0
- data/lib/opensteam/inventory_base.rb +79 -0
- data/lib/opensteam/order_base.rb +58 -0
- data/lib/opensteam/product_base.rb +222 -0
- data/lib/opensteam/property_base.rb +63 -0
- data/lib/opensteam/shopping_cart.rb +343 -0
- data/lib/opensteam/user_base.rb +165 -0
- data/lib/opensteam/version.rb +9 -0
- data/test/test_helper.rb +2 -0
- data/test/test_opensteam.rb +11 -0
- metadata +189 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
module Opensteam
|
2
|
+
|
3
|
+
|
4
|
+
=begin rdoc
|
5
|
+
|
6
|
+
PropertyBase Module
|
7
|
+
|
8
|
+
Defines all the Property-specific methods and variables.
|
9
|
+
Used for the Property Base Class (STI)
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
module PropertyBase
|
14
|
+
|
15
|
+
module ClassMethods #:nodoc:
|
16
|
+
|
17
|
+
def table_name() "properties" ; end
|
18
|
+
|
19
|
+
def properties
|
20
|
+
@properties ||= []
|
21
|
+
end
|
22
|
+
|
23
|
+
# save all subclasses in the properties-variable
|
24
|
+
def inherited(property)
|
25
|
+
properties << property.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def self.included(base)
|
32
|
+
base.extend ClassMethods
|
33
|
+
|
34
|
+
base.class_eval do
|
35
|
+
include Opensteam::Base::Helper
|
36
|
+
include Opensteam::Finder
|
37
|
+
|
38
|
+
has_many :inventories_properties,
|
39
|
+
:class_name => "Opensteam::InventoryBase::InventoriesProperty",
|
40
|
+
:foreign_key => "property_id"
|
41
|
+
|
42
|
+
has_many :inventories,
|
43
|
+
:class_name => "Opensteam::InventoryBase::Inventory",
|
44
|
+
:through => :inventories_properties
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def only_properties() #:nodoc:
|
51
|
+
is_property? ? self : nil
|
52
|
+
end
|
53
|
+
|
54
|
+
# sort properties
|
55
|
+
def <=> (other) #:nodoc:
|
56
|
+
self.id <=> other.id
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
@@ -0,0 +1,343 @@
|
|
1
|
+
|
2
|
+
require 'opensteam/finder'
|
3
|
+
|
4
|
+
|
5
|
+
module Opensteam
|
6
|
+
|
7
|
+
|
8
|
+
module ShoppingCart
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
# CartController
|
13
|
+
#
|
14
|
+
# - standard methods for ShoppingCart manipulation
|
15
|
+
# - automatically sets @cart to access shopping-cart
|
16
|
+
#
|
17
|
+
#
|
18
|
+
#
|
19
|
+
class CartController < ActionController::Base
|
20
|
+
|
21
|
+
|
22
|
+
#
|
23
|
+
#include @cart instance-variable in all actions
|
24
|
+
before_filter :set_instance_vars
|
25
|
+
|
26
|
+
# delete item from cart
|
27
|
+
#
|
28
|
+
def del_item
|
29
|
+
get_cart.del( :cart_item_id => params[:id])
|
30
|
+
@current_item = params[:id]
|
31
|
+
redirect_to_index unless request.xhr?
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# show item from cart
|
36
|
+
#
|
37
|
+
def show_cart_item
|
38
|
+
i = Opensteam::InventoryBase::Inventory.find( @cart.items[ params[:id].to_i ].yamlid )
|
39
|
+
@product = i.product
|
40
|
+
@inventory = [i]
|
41
|
+
@product.selected_inventory = @inventory
|
42
|
+
@properties = @product.selected_inventory.last.properties.to_h2 { |x| x.class.to_s.tableize }
|
43
|
+
@cart_details = true
|
44
|
+
render :action => :show
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
# add an inventory object to the shopping cart
|
50
|
+
#
|
51
|
+
# * finds the product + inventory object (based on selected properties)
|
52
|
+
# * checks if product is available
|
53
|
+
# * add inventory object to cart
|
54
|
+
def add_inventory_to_cart
|
55
|
+
if request.post?
|
56
|
+
|
57
|
+
params[:product] = frmt params[:product]
|
58
|
+
|
59
|
+
if params[:product][:properties] && params[:product][:properties].index("")
|
60
|
+
flash[:error] = "Please select a #{params[:product][:properties].index("").humanize}"
|
61
|
+
return
|
62
|
+
end
|
63
|
+
|
64
|
+
product = Opensteam::Find.find_product_with_inventory( params[:product] )
|
65
|
+
if product.is_available?
|
66
|
+
unless @cart.add product.selected_inventories.last.id
|
67
|
+
render :update do |page| page.alert "Sorry, there are only #{product.selected_inventories.last.storage} available!" end
|
68
|
+
end
|
69
|
+
else
|
70
|
+
render :update do |page| page.alert "Sorry, this product is currently not available!" end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
def incr_quantity
|
79
|
+
if request.post?
|
80
|
+
ci = @cart.get( :cart_item_id => params[:id] )
|
81
|
+
unless ci.incr
|
82
|
+
render :update do |page| page.alert "Sorry, there are only #{ci.quantity} available!" end
|
83
|
+
return
|
84
|
+
end
|
85
|
+
end
|
86
|
+
if session[:active_order]
|
87
|
+
render :template => "checkout/update_cart_content.rjs"
|
88
|
+
else
|
89
|
+
render :action => "add_inventory_to_cart.rjs"
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def decr_quantity
|
96
|
+
if request.post?
|
97
|
+
( ( ci = @cart.get( :cart_item_id => params[:id] ) ).quantity - 1 ) == 0 ?
|
98
|
+
@cart.items.delete_at( params[:id].to_i ) :
|
99
|
+
ci.decr ;
|
100
|
+
end
|
101
|
+
if session[:active_order]
|
102
|
+
render :template => "checkout/update_cart_content.rjs"
|
103
|
+
else
|
104
|
+
render :action => "add_inventory_to_cart.rjs"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
#
|
112
|
+
# update quantity for item in cart
|
113
|
+
# params[:quantity] = { "item_id" => "quantity }
|
114
|
+
#
|
115
|
+
def update_quantity
|
116
|
+
if request.post?
|
117
|
+
params[:quantity].each { |key,value| @cart.set_quantity(key, value) }
|
118
|
+
|
119
|
+
if session[:active_order]
|
120
|
+
redirect_to :action => "show_cart"
|
121
|
+
return
|
122
|
+
end
|
123
|
+
end
|
124
|
+
redirect_to_index
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
#
|
131
|
+
# empty cart
|
132
|
+
#
|
133
|
+
def empty_cart
|
134
|
+
wipe_cart
|
135
|
+
redirect_to_index
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
#====================================================
|
142
|
+
# private methods
|
143
|
+
#
|
144
|
+
#
|
145
|
+
private
|
146
|
+
|
147
|
+
# format params[:product] hash (-> rescursive templates...)
|
148
|
+
def frmt(h)
|
149
|
+
returning(Hash.new) { |hash| h.each_pair { |k,v| k =~ /^products/ ? ( hash[:products] ||= [] ) << frmt(v) : hash[k] = v } }
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
def set_instance_vars
|
154
|
+
@cart = get_cart
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
def get_cart
|
159
|
+
session[:cart] ||= Cart.new
|
160
|
+
end
|
161
|
+
|
162
|
+
def wipe_cart
|
163
|
+
session[:cart] = nil
|
164
|
+
get_cart
|
165
|
+
end
|
166
|
+
|
167
|
+
|
168
|
+
def redirect_to_index
|
169
|
+
redirect_to opensteam_index_path
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
def get_user
|
175
|
+
if session[:user_login]
|
176
|
+
return Opensteam::UserBase::User.find(:first, :conditions => ["login = ?", session[:user_login]])
|
177
|
+
end
|
178
|
+
rescue
|
179
|
+
return nil
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
class Cart
|
189
|
+
|
190
|
+
## cart_items
|
191
|
+
attr_accessor :items
|
192
|
+
|
193
|
+
|
194
|
+
# intialize items array
|
195
|
+
def initialize
|
196
|
+
@items = []
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
# get item from cart
|
202
|
+
# get( :cart_itemd_id => 1 ) # get item by internal array id
|
203
|
+
# get( :yamlid => 3 ) # get item by Inventory-id
|
204
|
+
#
|
205
|
+
def get( opts = {} )
|
206
|
+
|
207
|
+
if opts.has_key? :yamlid
|
208
|
+
@items.each do |v|
|
209
|
+
return v if v.yamlid == opts[:yamlid]
|
210
|
+
end
|
211
|
+
|
212
|
+
elsif opts.has_key? :cart_item_id
|
213
|
+
return @items[ opts[:cart_item_id].to_i ]
|
214
|
+
end
|
215
|
+
|
216
|
+
return nil
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
# add an item to cart or increment its quantity
|
223
|
+
def add( yamlid )
|
224
|
+
item = get( :yamlid => yamlid )
|
225
|
+
if item.nil?
|
226
|
+
@items << CartItem.new( yamlid )
|
227
|
+
return true
|
228
|
+
else
|
229
|
+
return item.incr
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
# set quantity for item (by internal array-id)
|
236
|
+
def set_quantity( id, q )
|
237
|
+
item = @items[ id.to_i ]
|
238
|
+
|
239
|
+
return nil if item.nil?
|
240
|
+
|
241
|
+
q = q.to_i
|
242
|
+
if q == 0
|
243
|
+
@items.delete_at( id.to_i )
|
244
|
+
elsif q > 0
|
245
|
+
item.quantity = ( item.inventory.storage < q ) ? item.inventory.storage : q
|
246
|
+
end
|
247
|
+
|
248
|
+
item
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
# delete item from cart (:cart_item_id or :yamlid )
|
253
|
+
def del( opts = {} )
|
254
|
+
if opts.has_key?( :yamlid )
|
255
|
+
@items.delete( get( :yamlid => opts[:yamlid] ) )
|
256
|
+
elsif opts.has_key?( :cart_item_id )
|
257
|
+
@items.delete_at( opts[:cart_item_id].to_i )
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
# calculate total price
|
264
|
+
def total_price
|
265
|
+
@items.collect { |x| x.price * x.quantity }.inject(&:+)
|
266
|
+
end
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
protected
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
class CartItem
|
280
|
+
include Opensteam::Finder
|
281
|
+
|
282
|
+
@@_load_product_on_init = false
|
283
|
+
cattr_accessor :_load_product_on_init
|
284
|
+
|
285
|
+
attr_reader :yamlid
|
286
|
+
|
287
|
+
attr_reader :product
|
288
|
+
|
289
|
+
attr_accessor :quantity
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
#initialize cart-item
|
294
|
+
def initialize(id)
|
295
|
+
@yamlid = id
|
296
|
+
@quantity = 1
|
297
|
+
end
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
# increase quantity
|
302
|
+
def incr
|
303
|
+
if (@quantity + 1 ) > inventory.storage
|
304
|
+
return false
|
305
|
+
else
|
306
|
+
@quantity += 1
|
307
|
+
return true
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
# decrease quantity
|
312
|
+
def decr
|
313
|
+
@quantity -= 1
|
314
|
+
end
|
315
|
+
|
316
|
+
# return product
|
317
|
+
def product
|
318
|
+
return Opensteam::InventoryBase::Inventory.find( @yamlid ).product
|
319
|
+
end
|
320
|
+
|
321
|
+
|
322
|
+
# return Inventory Object
|
323
|
+
def inventory
|
324
|
+
return Opensteam::InventoryBase::Inventory.find( @yamlid )
|
325
|
+
end
|
326
|
+
|
327
|
+
# return price
|
328
|
+
def price
|
329
|
+
return Opensteam::InventoryBase::Inventory.find( @yamlid ).price
|
330
|
+
end
|
331
|
+
|
332
|
+
private
|
333
|
+
def load_product_object
|
334
|
+
|
335
|
+
end
|
336
|
+
|
337
|
+
|
338
|
+
end
|
339
|
+
|
340
|
+
|
341
|
+
end
|
342
|
+
|
343
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
module Opensteam
|
2
|
+
|
3
|
+
|
4
|
+
# UserBase
|
5
|
+
#
|
6
|
+
# Module for all User-Specific Classes
|
7
|
+
#
|
8
|
+
#
|
9
|
+
module UserBase
|
10
|
+
|
11
|
+
class Address < ActiveRecord::Base
|
12
|
+
belongs_to :customer
|
13
|
+
has_many :shipping_orders, :class_name => "Order", :foreign_key => "shipping_address_id"
|
14
|
+
has_many :payment_orders , :class_name => "Order", :foreign_key => "payment_address_id"
|
15
|
+
|
16
|
+
validates_presence_of :city, :postal, :street, :land
|
17
|
+
|
18
|
+
|
19
|
+
# get all orders for current address
|
20
|
+
def orders
|
21
|
+
self.shipping_orders | self.payment_orders
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
require 'digest/sha1'
|
30
|
+
|
31
|
+
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
has_many :addresses
|
34
|
+
|
35
|
+
validates_length_of :login, :within => 3..20
|
36
|
+
validates_length_of :password, :within => 5..40
|
37
|
+
validates_presence_of :login, :email, :password, :password_confirmation, :salt
|
38
|
+
validates_uniqueness_of :email, :login
|
39
|
+
validates_confirmation_of :password
|
40
|
+
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"
|
41
|
+
|
42
|
+
|
43
|
+
attr_protected :id, :salt, :type
|
44
|
+
attr_accessor :password, :password_confirmation
|
45
|
+
|
46
|
+
|
47
|
+
# authenticate
|
48
|
+
def self.authenticate(login,pass)
|
49
|
+
u = find(:first, :conditions => ["login = ?", login])
|
50
|
+
return nil if u.nil?
|
51
|
+
return u.login if Opensteam::UserBase::User.encrypt(pass, u.salt) == u.hashed_password
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
#set password
|
57
|
+
def password=(pass)
|
58
|
+
errors.add_to_base("password is empty") if pass.nil? or pass.empty?
|
59
|
+
@password = pass
|
60
|
+
self.salt = Opensteam::UserBase::User.random_string(10) if !self.salt?
|
61
|
+
self.hashed_password = Opensteam::UserBase::User.encrypt(@password, self.salt)
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
def is_admin?
|
66
|
+
return (self.class == Opensteam::UserBase::Admin ) ? true : false
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
protected
|
71
|
+
def self.random_string(len)
|
72
|
+
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
|
73
|
+
newpass = ""
|
74
|
+
1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
|
75
|
+
return newpass
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.encrypt(pass, salt)
|
79
|
+
Digest::SHA1.hexdigest(pass+salt)
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
class Admin < User
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
class Customer < ActiveRecord::Base
|
92
|
+
has_many :orders, :class_name => "Opensteam::OrderBase::Order"
|
93
|
+
has_many :addresses, :foreign_key => "user_id"
|
94
|
+
|
95
|
+
validates_presence_of :email #, :firstname, :lastname
|
96
|
+
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid format .."
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
# Opensteam::UserBase::Helper
|
107
|
+
#
|
108
|
+
# Helper Methods for a Controller
|
109
|
+
# used for before/after_fileter
|
110
|
+
#
|
111
|
+
#
|
112
|
+
module Helper
|
113
|
+
|
114
|
+
|
115
|
+
# admin_required filter
|
116
|
+
def admin_required
|
117
|
+
if login = session[:user_login]
|
118
|
+
if Opensteam::UserBase::User.find(:first, :conditions => ["login = ?", login]).is_admin?
|
119
|
+
return true
|
120
|
+
end
|
121
|
+
end
|
122
|
+
flash[:error] = "you have to be an admin ..."
|
123
|
+
session[:return_to] = request.request_uri
|
124
|
+
redirect_to login_path
|
125
|
+
return false
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
# user_required filter
|
131
|
+
def user_required
|
132
|
+
if login = session[:user_login]
|
133
|
+
if Opensteam::UserBase::User.find(:first, :conditions => ["login = ?", login])
|
134
|
+
return true
|
135
|
+
end
|
136
|
+
end
|
137
|
+
flash[:error] = "not logged in .."
|
138
|
+
session[:return_to] = request.request_uri
|
139
|
+
redirect_to login_path
|
140
|
+
return false
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
# get the current authenticated user
|
145
|
+
def current_user
|
146
|
+
session[:user]
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
# redirect to stored request
|
152
|
+
def redirect_to_stored
|
153
|
+
if return_to = session[:return_to]
|
154
|
+
session[:return_to] = nil
|
155
|
+
redirect_to return_to
|
156
|
+
else
|
157
|
+
redirect_to administration_path
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|