app controllers application_controll er rb -53 17 53 class Application

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
+++ b/app/controllers/application_controller.rb
@@ -53,17 +53,9 @@ class ApplicationController < ActionController::Base
end
def is_activated?
- return true if (current_user && current_user.activated)
- total_summ = Order.all(:conditions => {
- :CustomerId => current_user.id,
- :Status => 'S'
- }, :limit => 2).map {|o| o.TotalPrice.to_f}.sum
- if total_summ >= 20
- current_user.update_attributes :activated => true
- return true
- else
- return false
- end
+ return true if current_user.is_activated?
+ return false unless current_user.last_2_valid_orders_sum >= 20
+ current_user.activate!
end
def require_user
diff --git a/app/models/user.rb b/app/models/user.rb
index 793108d..eb7d76b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -98,4 +98,28 @@ class User < ActiveRecord::Base
save
end
+ def is_activated?
+ activated
+ end
+
+ def activate!
+ update_attributes :activated => true
+ end
+
+ def orders_sum(*args)
+ options = args.extract_options!
+
+ orders.scoped(options).map {|o| o.TotalPrice.to_f}.sum
+ end
+
+ def last_2_valid_orders_sum
+ orders_sum(
+ :conditions => {
+ :Status => 'S',
+ :Store => APP_CONFIG[:site][:store_id]
+ },
+ :limit => 2,
+ :order => "TimeStamp DESC")
+ end
+
end