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 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.0.0"
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/"
@@ -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.id = yield
9
- end while self.class.exists?(:id => self.id)
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
- def acts_as_random_id(options={}, &block)
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashtrain-acts_as_random_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hashtrain.com