recognition 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -16,8 +16,12 @@ Then, you need to run the generator:
16
16
 
17
17
  $ rails generate recogintion:install
18
18
 
19
+ And finally, configure your REDIS server connection parameters in: `config/recognition.yml`
20
+
19
21
  ## Usage
20
22
 
23
+ ### Points
24
+
21
25
  Assuming you have two models User and Post and you want to give users points for the following:
22
26
 
23
27
  5 points for registration
@@ -54,8 +58,28 @@ app/controllers/profiles_controller.rb:
54
58
  end
55
59
  end
56
60
 
61
+ ### Vouchers
62
+
63
+ Use an existing model or generate a new one. Your model might have the following attributes:
64
+
65
+ * `:code` **required**
66
+ * `:amount` **required**
67
+ * `:expires_at` _optional_
68
+ * `:reusable` _optional_
69
+
70
+ app/models/voucher.rb:
71
+
72
+ class Voucher < ActiveRecord::Base
73
+ attr_accessible :code, :amount, :expires_at, :reusable
74
+ acts_as_voucher code_length: 14
75
+ end
76
+
77
+ Then, you may do:
78
+
79
+ voucher = Voucher.create!(amount: 20, expires_at: (DateTime.now + 1.day), reusable: true)
80
+ voucher.redeem current_user
57
81
 
58
- ### Note
82
+ **Note:**
59
83
  Due to the way Ruby method aliasing work, if you need to recognize users for
60
84
  non-ActiveRecord actions (anything that's not :create, :update and :destroy),
61
85
  make sure you add the `recognize` line *after* the method you want to
@@ -26,20 +26,27 @@ module Recognition
26
26
  def redeemable? recognizable
27
27
  # default: not redeemable
28
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?
29
+ # only check if the voucher did not expire
30
+ unless expired?
31
+ # has the voucher ever been redeemed?
32
+ if Database.get_voucher_transactions(self.code).any?
33
+ # has the voucher ever been redeemed by this user?
34
+ if Database.get_user_voucher(recognizable.id, code) != 0
35
+ pass = false
36
+ # is the voucher reusable?
37
+ elsif defined?(self.reusable?) && self.reusable?
38
+ pass = true
39
+ end
40
+ else
36
41
  pass = true
37
42
  end
38
- else
39
- pass = true
40
43
  end
41
44
  pass
42
45
  end
46
+
47
+ def expired?
48
+ defined?(self.expires_at) && self.expires_at.present? && self.expires_at < DateTime.now
49
+ end
43
50
  end
44
51
  end
45
52
  end
@@ -1,4 +1,4 @@
1
1
  module Recognition
2
2
  # Current Version
3
- VERSION = "0.3.3"
3
+ VERSION = "0.3.4"
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.3
4
+ version: 0.3.4
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-16 00:00:00.000000000 Z
12
+ date: 2013-05-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -180,7 +180,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
180
180
  version: '0'
181
181
  segments:
182
182
  - 0
183
- hash: -708415274929764439
183
+ hash: 582050913544218696
184
184
  required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  none: false
186
186
  requirements:
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  version: '0'
190
190
  segments:
191
191
  - 0
192
- hash: -708415274929764439
192
+ hash: 582050913544218696
193
193
  requirements: []
194
194
  rubyforge_project:
195
195
  rubygems_version: 1.8.23