recognition 0.3.7 → 0.3.8

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/README.md CHANGED
@@ -61,6 +61,12 @@ app/models/post.rb:
61
61
  recognize :user, for: :destroy, loss: 2, maximum: 4
62
62
  end
63
63
 
64
+ **Important:**
65
+ Due to the way Ruby method aliasing work, if you need to recognize users for
66
+ non-ActiveRecord actions (anything that's not :create, :update and :destroy),
67
+ make sure you add the `recognize` line *after* the method you want to
68
+ recognize the user for.
69
+
64
70
  app/controllers/profiles_controller.rb:
65
71
 
66
72
  class ProfilesController < ApplicationController
@@ -86,6 +92,11 @@ Your model might have the following attributes:
86
92
  * `:expires_at` _optional_
87
93
  * `:reusable` _optional_
88
94
 
95
+ You can specify the following extra parameters for vouchers:
96
+
97
+ * `:prefix` can be a number, string or method name or even an anonymous function.
98
+ * `:suffix` can be a number, string or method name or even an anonymous function.
99
+
89
100
  app/models/voucher.rb:
90
101
 
91
102
  class Voucher < ActiveRecord::Base
@@ -98,12 +109,6 @@ Then, you may do:
98
109
  voucher = Voucher.create!(amount: 20, expires_at: (DateTime.now + 1.day), reusable: true)
99
110
  voucher.redeem current_user
100
111
 
101
- **Note:**
102
- Due to the way Ruby method aliasing work, if you need to recognize users for
103
- non-ActiveRecord actions (anything that's not :create, :update and :destroy),
104
- make sure you add the `recognize` line *after* the method you want to
105
- recognize the user for.
106
-
107
112
  ## Example
108
113
  The following won't work:
109
114
 
@@ -1,12 +1,12 @@
1
1
  module Recognition
2
2
  module ActiveRecord
3
3
  module Voucher
4
- # class_attribute :voucher_validators
5
-
6
4
  def regenerate_code
7
- l = self.class.recognitions[:code_length] || 10
5
+ prefix = Recognition::Database.parse_voucher_part(self.class.recognitions[:prefix], self)
6
+ suffix = Recognition::Database.parse_voucher_part(self.class.recognitions[:suffix], self)
7
+ l = (self.class.recognitions[:code_length] - (prefix.length + suffix.length)) || 10
8
8
  dict = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten
9
- code = (1..l).map{ dict[rand(dict.length)] }.join
9
+ code = (1..l).map{ dict[rand(dict.length)] }.prepend(prefix).append(suffix).join
10
10
  # Prevent code collision at all costs
11
11
  if self.class.to_s.constantize.find_all_by_code(code).any?
12
12
  regenerate_code
@@ -71,6 +71,26 @@ module Recognition
71
71
  Database.get_user_counter id, bucket
72
72
  end
73
73
 
74
+ def self.parse_voucher_part part, object
75
+ case part.class.to_s
76
+ when 'String'
77
+ value = part
78
+ when 'Integer'
79
+ value = part.to_s
80
+ when 'Fixnum'
81
+ value = part.to_s
82
+ when 'Symbol'
83
+ value = object.send(part).to_s
84
+ when 'Proc'
85
+ value = part.call(object).to_s
86
+ when 'NilClass'
87
+ # Do not complain about nil amounts
88
+ else
89
+ raise ArgumentError, "type mismatch for voucher part: expecting 'Integer', 'Fixnum', 'Symbol' or 'Proc' but got '#{ amount.class.to_s }' instead."
90
+ end
91
+ value || ''
92
+ end
93
+
74
94
  private
75
95
 
76
96
  def self.get_transactions keypart, start, stop
@@ -103,6 +123,7 @@ module Recognition
103
123
  def self.parse_amount amount, object
104
124
  case amount.class.to_s
105
125
  when 'Integer'
126
+ value = amount
106
127
  when 'Fixnum'
107
128
  value = amount
108
129
  when 'Symbol'
@@ -1,4 +1,4 @@
1
1
  module Recognition
2
2
  # Current Version
3
- VERSION = "0.3.7"
3
+ VERSION = "0.3.8"
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.7
4
+ version: 0.3.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -181,7 +181,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  segments:
183
183
  - 0
184
- hash: 111551344428763194
184
+ hash: 1363284435597105623
185
185
  required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  none: false
187
187
  requirements:
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
190
  version: '0'
191
191
  segments:
192
192
  - 0
193
- hash: 111551344428763194
193
+ hash: 1363284435597105623
194
194
  requirements: []
195
195
  rubyforge_project:
196
196
  rubygems_version: 1.8.23