acts_as_keyed 0.1.1 → 0.1.2

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.
@@ -5,23 +5,30 @@ module ActsAsKeyed
5
5
  class_attribute :options
6
6
 
7
7
  options[:as_param] ||= false
8
+ options[:column] ||= 'key'
8
9
  options[:size] ||= 10
9
10
  options[:chars] ||= ('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a - ['l','I','O']
10
11
 
11
12
  self.options = options
12
13
 
13
- raise MissingKeyColumnError if ActiveRecord::Base.connection.table_exists?(self.table_name) && columns_hash['key'].nil?
14
+ options[:column] = options[:column].to_s
14
15
 
15
- attr_protected :key
16
+ raise MissingKeyColumnError if ActiveRecord::Base.connection.table_exists?(self.table_name) && columns_hash[options[:column]].nil?
17
+
18
+ attr_protected options[:column]
16
19
 
17
20
  class << self
18
21
  def find(*args)
19
22
  if self.options[:as_param] && args.first.is_a?(String)
20
- find_by_key(args)
23
+ send("find_by_#{options[:column]}", args)
21
24
  else
22
25
  super(*args)
23
26
  end
24
27
  end
28
+
29
+ def key_exists?(k)
30
+ exists?(["#{options[:column]} = ?", k])
31
+ end
25
32
  end
26
33
 
27
34
  include InstanceMethods
@@ -10,13 +10,21 @@ module ActsAsKeyed
10
10
  self.save
11
11
  end
12
12
 
13
+ def key=(val)
14
+ write_attribute(options[:column], val)
15
+ end
16
+
17
+ def key
18
+ read_attribute(options[:column])
19
+ end
20
+
13
21
  protected
14
22
 
15
23
  def create_key
16
24
  k = nil
17
25
  100.times do
18
26
  k = random_key
19
- break if self.class.count(:conditions => { :key => k }) == 0
27
+ break if !self.class.key_exists?(k)
20
28
  k = nil
21
29
  end
22
30
  raise NoAvailableKeysError if k.nil?
@@ -1,3 +1,3 @@
1
1
  module ActsAsKeyed
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -60,6 +60,20 @@ class ActsAsKeyedTest < ActsAsKeyedBaseTest
60
60
  end
61
61
  end
62
62
 
63
+ test "should use a different column if specified as a string" do
64
+ ObjectWithoutKey.acts_as_keyed(:column => 'name')
65
+
66
+ o = ObjectWithoutKey.create()
67
+ assert_not_nil o.name
68
+ end
69
+
70
+ test "should use a different column if specified as a symbol" do
71
+ ObjectWithoutKey.acts_as_keyed(:column => :name)
72
+
73
+ o = ObjectWithoutKey.create()
74
+ assert_not_nil o.name
75
+ end
76
+
63
77
  test "should fail if object doesn't have key column" do
64
78
  output = nil
65
79
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_keyed
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Hubert
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-04 00:00:00 Z
18
+ date: 2012-02-13 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activerecord