recognition 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,19 +11,20 @@ module Recognition
11
11
  module ClassMethods
12
12
  # to be called from user model
13
13
  def acts_as_recognizable options = {}
14
- include RecognizableInstanceMethods
14
+ require "recognition/activerecord/recognizable"
15
+ include Recognition::ActiveRecord::Recognizable
15
16
  self.recognizable = true
16
17
  self.recognitions ||= {}
17
18
  self.recognitions[:initial] = {
18
19
  amount: options[:initial]
19
20
  }
20
21
  after_save :add_initial_points
21
-
22
22
  end
23
23
 
24
24
  # to be called from other models
25
25
  def recognize recognizable, condition
26
- include ObjectInstanceMethods
26
+ require "recognition/activerecord/model"
27
+ include Recognition::ActiveRecord::Model
27
28
  self.recognitions ||= {}
28
29
  self.recognitions[condition[:for]] = {
29
30
  recognizable: recognizable,
@@ -55,95 +56,11 @@ module Recognition
55
56
 
56
57
  # to be called from user-model
57
58
  def acts_as_voucher options = {}
58
- include VoucherInstanceMethods
59
+ require "recognition/activerecord/voucher"
60
+ include Recognition::ActiveRecord::Voucher
59
61
  self.recognitions = options
60
62
  before_create :regenerate_code
61
63
  end
62
64
  end
63
-
64
- module RecognizableInstanceMethods
65
- # Determine user current balance of points
66
- def points
67
- Database.get_user_points self.id
68
- end
69
-
70
- def recognition_counter bucket
71
- Database.get_user_counter self.id, bucket
72
- end
73
-
74
- def add_initial_points
75
- Database.update_points self, :initial, self.class.recognitions[:initial]
76
- end
77
-
78
- def update_points amount, bucket
79
- Database.log(self.id, amount.to_i, bucket)
80
- end
81
-
82
- def transactions page = 0, per = 20
83
- Database.get_user_transactions self.id, page, per
84
- end
85
- end
86
-
87
- module ObjectInstanceMethods
88
- def recognize_creating
89
- if self.id_changed? # Unless we are creating
90
- Database.update_points self, :create, self.class.recognitions[:create] unless self.class.recognitions[:create].nil?
91
- end
92
- end
93
-
94
- def recognize_updating
95
- unless self.id_changed? # Unless we are creating
96
- Database.update_points self, :update, self.class.recognitions[:update] unless self.class.recognitions[:update].nil?
97
- end
98
- end
99
-
100
- def recognize_destroying
101
- Database.update_points self, :destroy, self.class.recognitions[:destroy] unless self.class.recognitions[:destroy].nil?
102
- end
103
-
104
- end
105
-
106
- module VoucherInstanceMethods
107
- def regenerate_code
108
- l = self.class.recognitions[:code_length] || 10
109
- dict = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten
110
- code = (1..l).map{ dict[rand(dict.length)] }.join
111
- # Prevent code collision at all costs
112
- if self.class.to_s.constantize.find_all_by_code(code).any?
113
- regenerate_code
114
- else
115
- self.code = code
116
- self
117
- end
118
- end
119
-
120
- def redeem recognizable
121
- # Make sure we have an amount beforehand
122
- if defined? self.amount
123
- if self.redeemable? recognizable
124
- Database.redeem_voucher recognizable.id, self.code, self.amount
125
- end
126
- end
127
- end
128
-
129
- def redeemable? recognizable
130
- # default: not redeemable
131
- pass = false
132
- # has the voucher ever been redeemed?
133
- if Database.get_voucher_transactions(self.code).any?
134
- # has the voucher ever been redeemed by this user?
135
- if Database.get_user_voucher(recognizable.id, code) != 0
136
- pass = false
137
- # is the voucher reusable?
138
- elsif defined?(self.reusable?) && self.reusable?
139
- pass = true
140
- end
141
- else
142
- pass = true
143
- end
144
- pass
145
- end
146
-
147
- end
148
65
  end
149
66
  end
@@ -0,0 +1,21 @@
1
+ module Recognition
2
+ module ActiveRecord
3
+ module Model
4
+ def recognize_creating
5
+ if self.id_changed? # Unless we are creating
6
+ Database.update_points self, :create, self.class.recognitions[:create] unless self.class.recognitions[:create].nil?
7
+ end
8
+ end
9
+
10
+ def recognize_updating
11
+ unless self.id_changed? # Unless we are creating
12
+ Database.update_points self, :update, self.class.recognitions[:update] unless self.class.recognitions[:update].nil?
13
+ end
14
+ end
15
+
16
+ def recognize_destroying
17
+ Database.update_points self, :destroy, self.class.recognitions[:destroy] unless self.class.recognitions[:destroy].nil?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ module Recognition
2
+ module ActiveRecord
3
+ module Recognizable
4
+ # Determine user current balance of points
5
+ def points
6
+ Database.get_user_points self.id
7
+ end
8
+
9
+ def recognition_counter bucket
10
+ Database.get_user_counter self.id, bucket
11
+ end
12
+
13
+ def add_initial_points
14
+ Database.update_points self, :initial, self.class.recognitions[:initial]
15
+ end
16
+
17
+ def update_points amount, bucket
18
+ Database.log(self.id, amount.to_i, bucket)
19
+ end
20
+
21
+ def transactions page = 0, per = 20
22
+ Database.get_user_transactions self.id, page, per
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,45 @@
1
+ module Recognition
2
+ module ActiveRecord
3
+ module Voucher
4
+ def regenerate_code
5
+ l = self.class.recognitions[:code_length] || 10
6
+ dict = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten
7
+ code = (1..l).map{ dict[rand(dict.length)] }.join
8
+ # Prevent code collision at all costs
9
+ if self.class.to_s.constantize.find_all_by_code(code).any?
10
+ regenerate_code
11
+ else
12
+ self.code = code
13
+ self
14
+ end
15
+ end
16
+
17
+ def redeem recognizable
18
+ # Make sure we have an amount beforehand
19
+ if defined? self.amount
20
+ if self.redeemable? recognizable
21
+ Database.redeem_voucher recognizable.id, self.code, self.amount
22
+ end
23
+ end
24
+ end
25
+
26
+ def redeemable? recognizable
27
+ # default: not redeemable
28
+ pass = false
29
+ # has the voucher ever been redeemed?
30
+ if Database.get_voucher_transactions(self.code).any?
31
+ # has the voucher ever been redeemed by this user?
32
+ if Database.get_user_voucher(recognizable.id, code) != 0
33
+ pass = false
34
+ # is the voucher reusable?
35
+ elsif defined?(self.reusable?) && self.reusable?
36
+ pass = true
37
+ end
38
+ else
39
+ pass = true
40
+ end
41
+ pass
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,4 +1,4 @@
1
1
  module Recognition
2
2
  # Current Version
3
- VERSION = "0.3.1"
3
+ VERSION = "0.3.2"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recognition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-15 00:00:00.000000000 Z
12
+ date: 2013-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -151,6 +151,9 @@ files:
151
151
  - lib/generators/recognition/templates/recognition.yml
152
152
  - lib/recognition/action_controller_extension.rb
153
153
  - lib/recognition/active_record_extension.rb
154
+ - lib/recognition/activerecord/model.rb
155
+ - lib/recognition/activerecord/recognizable.rb
156
+ - lib/recognition/activerecord/voucher.rb
154
157
  - lib/recognition/backend.rb
155
158
  - lib/recognition/condition.rb
156
159
  - lib/recognition/database.rb
@@ -177,7 +180,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
180
  version: '0'
178
181
  segments:
179
182
  - 0
180
- hash: 453535705823621240
183
+ hash: 4318160502676074066
181
184
  required_rubygems_version: !ruby/object:Gem::Requirement
182
185
  none: false
183
186
  requirements:
@@ -186,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
189
  version: '0'
187
190
  segments:
188
191
  - 0
189
- hash: 453535705823621240
192
+ hash: 4318160502676074066
190
193
  requirements: []
191
194
  rubyforge_project:
192
195
  rubygems_version: 1.8.23