hashtrain-acts_as_random_id 1.0.3 → 1.1.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/Rakefile +1 -1
- data/lib/acts_as_random_id.rb +8 -6
- data/test/acts_as_random_id_test.rb +10 -0
- data/test/schema.rb +6 -1
- metadata +1 -1
data/Rakefile
CHANGED
|
@@ -38,7 +38,7 @@ PKG_FILES = FileList[
|
|
|
38
38
|
|
|
39
39
|
spec = Gem::Specification.new do |s|
|
|
40
40
|
s.name = "acts_as_random_id"
|
|
41
|
-
s.version = "1.
|
|
41
|
+
s.version = "1.1.0"
|
|
42
42
|
s.author = "hashtrain.com and author idea Stanislav Pogrebnyak (stanislav.pogrebnyak@gmail.com)"
|
|
43
43
|
s.email = "mail@hashtrain.com"
|
|
44
44
|
s.homepage = "http://github.com/hashtrain/acts_as_random_id/"
|
data/lib/acts_as_random_id.rb
CHANGED
|
@@ -3,20 +3,22 @@ module ActsAsRandomId
|
|
|
3
3
|
def self.included(base)
|
|
4
4
|
base.send :extend, ClassMethods
|
|
5
5
|
|
|
6
|
-
def ensure_unique_id
|
|
6
|
+
def ensure_unique_id(options)
|
|
7
7
|
begin
|
|
8
|
-
self.
|
|
9
|
-
end while self.class.exists?(:
|
|
8
|
+
self.send "#{options[:field]}=", yield
|
|
9
|
+
end while self.class.exists?(options[:field] => self.send(options[:field]))
|
|
10
10
|
end
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
module ClassMethods
|
|
14
|
-
|
|
14
|
+
# Default options:
|
|
15
|
+
# :field => :id
|
|
16
|
+
def acts_as_random_id(options={:field => :id}, &block)
|
|
15
17
|
before_create do |record|
|
|
16
18
|
if block
|
|
17
|
-
record.ensure_unique_id(&block)
|
|
19
|
+
record.ensure_unique_id(options, &block)
|
|
18
20
|
else
|
|
19
|
-
record.ensure_unique_id do
|
|
21
|
+
record.ensure_unique_id(options) do
|
|
20
22
|
rand(2_147_483_647) + 1 # mysql and SQLite type integer
|
|
21
23
|
end
|
|
22
24
|
end
|
|
@@ -7,6 +7,12 @@ class ActsAsRandomIdTest < Test::Unit::TestCase
|
|
|
7
7
|
class Comment < ActiveRecord::Base
|
|
8
8
|
acts_as_random_id
|
|
9
9
|
end
|
|
10
|
+
|
|
11
|
+
class Payment < ActiveRecord::Base
|
|
12
|
+
acts_as_random_id :field => :secure_identifier do
|
|
13
|
+
Time.now.to_i
|
|
14
|
+
end
|
|
15
|
+
end
|
|
10
16
|
|
|
11
17
|
class Group < ActiveRecord::Base
|
|
12
18
|
acts_as_random_id do
|
|
@@ -36,4 +42,8 @@ class ActsAsRandomIdTest < Test::Unit::TestCase
|
|
|
36
42
|
assert_equal Time.now.to_i, a.id
|
|
37
43
|
end
|
|
38
44
|
|
|
45
|
+
def test_should_assign_custom_field
|
|
46
|
+
p = Payment.create
|
|
47
|
+
assert_equal Time.now.to_i, p.secure_identifier
|
|
48
|
+
end
|
|
39
49
|
end
|
data/test/schema.rb
CHANGED
|
@@ -17,5 +17,10 @@ ActiveRecord::Schema.define(:version => 20090217091952) do
|
|
|
17
17
|
t.datetime "created_at"
|
|
18
18
|
t.datetime "updated_at"
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
|
|
21
|
+
create_table "payments", :force => true do |t|
|
|
22
|
+
t.text "secure_identifier"
|
|
23
|
+
t.datetime "created_at"
|
|
24
|
+
t.datetime "updated_at"
|
|
25
|
+
end
|
|
21
26
|
end
|