auto_increment 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module AutoIncrement
4
4
  def auto_increment(options = {})
5
5
  raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
6
6
 
7
- default_options = { :column => :code, :scope => nil, :initial => 1 }
7
+ default_options = { :column => :code, :scope => nil, :initial => 1, :force => false }
8
8
  options = default_options.merge(options) unless options.nil?
9
9
 
10
10
  options[:scope] = [options[:scope]] if options[:scope].class != Array
@@ -12,7 +12,7 @@ module AutoIncrement
12
12
  before_create "auto_increment_#{options[:column]}"
13
13
 
14
14
  define_method "auto_increment_#{options[:column]}" do
15
-
15
+ return if self.send(options[:column]).present? and !options[:force]
16
16
  query = self.class.scoped
17
17
  options[:scope].each do |scope|
18
18
  if scope.present? and respond_to?(scope)
@@ -1,3 +1,3 @@
1
1
  module AutoIncrement
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -41,12 +41,26 @@ class AutoIncrementActiveRecordTest < ActiveRecord::TestCase
41
41
  assert_equal 'A', @user_account2.letter_code
42
42
  end
43
43
 
44
- test "checks if Z increments to AA" do
44
+ test "Z increments to AA" do
45
45
  assert_equal 'AA', @user4.letter_code
46
46
  end
47
47
 
48
- test "checks if AA increments to AB" do
48
+ test "AA increments to AB" do
49
49
  assert_equal 'AB', @user5.letter_code
50
50
  end
51
51
 
52
+ test "not set column if is already set" do
53
+ account = Account.new :name => 'Another Account', :code => 50
54
+ account.save
55
+
56
+ assert_equal account.code, 50
57
+ end
58
+
59
+ test "set column if option force is used" do
60
+ User.destroy_all
61
+ user = User.new :name => 'Maria', :letter_code => 'Z'
62
+ user.save
63
+
64
+ assert_equal user.letter_code, 'A'
65
+ end
52
66
  end
data/test/test_helper.rb CHANGED
@@ -29,7 +29,7 @@ class Account < ActiveRecord::Base
29
29
  end
30
30
 
31
31
  class User < ActiveRecord::Base
32
- auto_increment :column => :letter_code, :scope => :account_id, :initial => 'A'
32
+ auto_increment :column => :letter_code, :scope => :account_id, :initial => 'A', :force => true
33
33
 
34
34
  belongs_to :account
35
35
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_increment
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Felipe Diesel
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-20 00:00:00 -03:00
17
+ date: 2011-03-31 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20