auto_increment 0.2 → 1.0.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.
- checksums.yaml +7 -0
- data/README.markdown +53 -0
- data/auto_increment.gemspec +1 -1
- data/lib/auto_increment/active_record.rb +11 -8
- data/lib/auto_increment/version.rb +1 -1
- metadata +24 -45
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2d10c327e17646c875c89c2eb2c225e59085de41
|
4
|
+
data.tar.gz: 3c0c3584f65e534a755fe89e152d926135ba60a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a2973f0afd4736371b1b9b844cfb9e9b22318ef4f229d9a9a62dedb032d5dfd9c8cd2fd9489c64fb67cbc83da73bfcc091adcf943bcaca6628f27fd1cf0a09e
|
7
|
+
data.tar.gz: ee1005cd6e16fdd8b30ff6a302f05ad3ccf4e7a62ecac69d2fb33f013af7122efec2215edc2d84cd2596baa7f29e813930f7b291c604928711432ea4c06d199e
|
data/README.markdown
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
auto_increment
|
2
|
+
==============
|
3
|
+
|
4
|
+
auto_increment provides automatic incrementation for a string or integer fields in Rails.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
You can use auto_increment as a gem in Rails 3.
|
10
|
+
|
11
|
+
To use the gem version, put the following gem requirement in your `Gemfile`:
|
12
|
+
|
13
|
+
gem "auto_increment"
|
14
|
+
|
15
|
+
|
16
|
+
Usage
|
17
|
+
-----
|
18
|
+
|
19
|
+
To work with a auto increment column you used to do sometihng like this in your model:
|
20
|
+
|
21
|
+
before_create :set_code
|
22
|
+
def set_code
|
23
|
+
max_code = Operation.maximum(:code)
|
24
|
+
self.code = max_code.to_i + 1
|
25
|
+
end
|
26
|
+
|
27
|
+
Looks fine, but not when you need to do it over and over again. In fact auto_increment does it under the cover.
|
28
|
+
|
29
|
+
All you need to do is this:
|
30
|
+
|
31
|
+
auto_increment
|
32
|
+
|
33
|
+
And your code field will be incremented
|
34
|
+
|
35
|
+
|
36
|
+
### Customizing
|
37
|
+
|
38
|
+
So you have a different column or need a scope. auto_increment provides options. You can use it like this:
|
39
|
+
|
40
|
+
auto_increment :column => :letter, :scope => [:account_id, :job_id], :initial => 'C', :force => true
|
41
|
+
|
42
|
+
* column: the column that will be incremented. Can be integer os string (default: code)
|
43
|
+
* scope: you can define columns that will be scoped and you can use as many as you want (default: nil)
|
44
|
+
* initial: initial value of column (default: 1)
|
45
|
+
* force: you can set a value before create and auto_increment will not change that, but if you do want this, set force to true (default: false)
|
46
|
+
|
47
|
+
|
48
|
+
### Compatibility
|
49
|
+
|
50
|
+
* Tested with Rails 3.0.4 in Ruby 1.8.7 and Ruby 1.9.2
|
51
|
+
|
52
|
+
### License
|
53
|
+
MIT License. Copyright 2011 29sul Tecnologia da Informação <http://www.29sul.com.br/>
|
data/auto_increment.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Felipe Diesel"]
|
10
10
|
s.email = ["felipediesel@gmail.com"]
|
11
|
-
s.homepage = "http://
|
11
|
+
s.homepage = "http://github.com/felipediesel/auto_increment"
|
12
12
|
s.summary = %q{Auto increment a string or integer field}
|
13
13
|
s.description = %q{Automaticaly increments a string or integer field in ActiveRecord.}
|
14
14
|
|
@@ -4,19 +4,22 @@ 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
|
-
|
8
|
-
options = default_options.merge(options) unless options.nil?
|
7
|
+
options.reverse_merge! column: :code, scope: nil, initial: 1, force: false
|
9
8
|
|
10
|
-
options[:scope] = [options[:scope]]
|
9
|
+
options[:scope] = [ options[:scope] ] unless options[:scope].is_a? Array
|
11
10
|
|
12
|
-
|
11
|
+
method_name = "auto_increment_#{options[:column]}"
|
12
|
+
|
13
|
+
before_create method_name
|
14
|
+
|
15
|
+
define_method method_name do
|
16
|
+
return if send(options[:column]).present? and !options[:force]
|
17
|
+
|
18
|
+
query = self.class.all
|
13
19
|
|
14
|
-
define_method "auto_increment_#{options[:column]}" do
|
15
|
-
return if self.send(options[:column]).present? and !options[:force]
|
16
|
-
query = self.class.scoped
|
17
20
|
options[:scope].each do |scope|
|
18
21
|
if scope.present? and respond_to?(scope)
|
19
|
-
query = query.where
|
22
|
+
query = query.where(scope => send(scope))
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
metadata
CHANGED
@@ -1,35 +1,25 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_increment
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: "0.2"
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Felipe Diesel
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2011-03-31 00:00:00 -03:00
|
18
|
-
default_executable:
|
11
|
+
date: 2014-04-30 00:00:00.000000000 Z
|
19
12
|
dependencies: []
|
20
|
-
|
21
13
|
description: Automaticaly increments a string or integer field in ActiveRecord.
|
22
|
-
email:
|
14
|
+
email:
|
23
15
|
- felipediesel@gmail.com
|
24
16
|
executables: []
|
25
|
-
|
26
17
|
extensions: []
|
27
|
-
|
28
18
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
19
|
+
files:
|
31
20
|
- .gitignore
|
32
21
|
- Gemfile
|
22
|
+
- README.markdown
|
33
23
|
- Rakefile
|
34
24
|
- auto_increment.gemspec
|
35
25
|
- lib/auto_increment.rb
|
@@ -48,41 +38,30 @@ files:
|
|
48
38
|
- test/app/config/environments/test.rb
|
49
39
|
- test/auto_increment_test.rb
|
50
40
|
- test/test_helper.rb
|
51
|
-
|
52
|
-
homepage: http://felipediesel.com
|
41
|
+
homepage: http://github.com/felipediesel/auto_increment
|
53
42
|
licenses: []
|
54
|
-
|
43
|
+
metadata: {}
|
55
44
|
post_install_message:
|
56
45
|
rdoc_options: []
|
57
|
-
|
58
|
-
require_paths:
|
46
|
+
require_paths:
|
59
47
|
- lib
|
60
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
none: false
|
71
|
-
requirements:
|
72
|
-
- - ">="
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
hash: 3
|
75
|
-
segments:
|
76
|
-
- 0
|
77
|
-
version: "0"
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
78
58
|
requirements: []
|
79
|
-
|
80
59
|
rubyforge_project: auto_increment
|
81
|
-
rubygems_version: 1.
|
60
|
+
rubygems_version: 2.1.11
|
82
61
|
signing_key:
|
83
|
-
specification_version:
|
62
|
+
specification_version: 4
|
84
63
|
summary: Auto increment a string or integer field
|
85
|
-
test_files:
|
64
|
+
test_files:
|
86
65
|
- test/app/Gemfile
|
87
66
|
- test/app/app/controllers/application_controller.rb
|
88
67
|
- test/app/app/helpers/application_helper.rb
|