rails_column_enumerator 1.0.1 → 1.0.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.
- checksums.yaml +4 -4
- data/README.md +18 -18
- data/lib/rails_column_enumerator/version.rb +1 -1
- data/lib/rails_column_enumerator.rb +4 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6142a21527dc7356777b36c1d29f0ecb092cbf7
|
4
|
+
data.tar.gz: 5f5af75ce9697d802da55f2afd630c79b2ff2130
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4122a21688fb8d6d72f862b1e9a1855a3d1f8877a7087d3df00a26e4c04f6edadd14c93e672fb6a5c69ad1ca9d731e90077cd1c8419517a549428fa46812635
|
7
|
+
data.tar.gz: 983bf0c595fc9bcd4e182858a459296a6327d01625f97aa127f6139591f98e68874e09547125a3c0aadf489257cea103b84691ecc3a4ef958021f11152fb2630
|
data/README.md
CHANGED
@@ -20,46 +20,46 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
Add ```extends RailsColumnEnumerator``` to the top of whatever class you want to add an enumerated column to
|
23
|
+
Add ```extends RailsColumnEnumerator``` to the top of whatever class you want to add an enumerated column to.
|
24
24
|
|
25
|
-
Then define the attribute with ```enumerated_column``` method
|
25
|
+
Then define the attribute with ```enumerated_column``` method.
|
26
26
|
|
27
27
|
example:
|
28
28
|
```ruby
|
29
29
|
class TestClass < ActiveRecord::Base
|
30
30
|
extends RailsColumnEnumerator
|
31
31
|
|
32
|
-
enumerated_column :
|
32
|
+
enumerated_column :state, { open: 1, closed: 2, failed: 3 }
|
33
33
|
end
|
34
34
|
|
35
|
-
# The following two consts would be added to your class (Their name is derived from the name you pass into 'enumerated_column')
|
36
|
-
TestClass::
|
37
|
-
TestClass::
|
35
|
+
# The following two consts would be added to your class (Their name is derived from the attribute name you pass into 'enumerated_column')
|
36
|
+
TestClass::STATES # { "open" => 1, "closed" => 2, "failed" => 3}
|
37
|
+
TestClass::STATE_NAMES # { 1 => :open, 2 => :closed, 3 => :failed }
|
38
38
|
|
39
39
|
# You can use your enumerated attribute as follows
|
40
40
|
example = TestClass.new
|
41
41
|
|
42
|
-
example.
|
42
|
+
example.state = :closed
|
43
43
|
|
44
|
-
# When accessing the attribute it will return the integer value by default, if you
|
44
|
+
# When accessing the attribute it will return the integer value by default, if you want to return the symbol then simply pass in a parameter of "true"
|
45
45
|
# This is to maintain support for XML serialization of the object (to_xml) amongst other things.
|
46
|
-
example.
|
47
|
-
example.
|
46
|
+
example.state # 2
|
47
|
+
example.state(true) # :closed
|
48
48
|
|
49
|
-
example.
|
49
|
+
example.state = 3
|
50
50
|
|
51
|
-
example.
|
52
|
-
example.
|
51
|
+
example.state # 3
|
52
|
+
example.state(true) # :failed
|
53
53
|
|
54
54
|
# The enumerated column will also be validated so that it must include values present in your enumerator
|
55
55
|
|
56
|
-
example.
|
57
|
-
example.
|
56
|
+
example.state = :pending
|
57
|
+
example.state # nil
|
58
58
|
|
59
|
-
example.
|
60
|
-
example.
|
59
|
+
example.state = 4
|
60
|
+
example.state # 4
|
61
61
|
|
62
|
-
example.
|
62
|
+
example.state.save! # raises "ActiveRecord::RecordInvalid, 'Validation failed: Sample column is not included in the list'"
|
63
63
|
```
|
64
64
|
|
65
65
|
## Development
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "rails_column_enumerator/version"
|
2
2
|
|
3
3
|
module RailsColumnEnumerator
|
4
|
-
def enumerated_column(column_name, enumerated_values)
|
4
|
+
def enumerated_column(column_name, enumerated_values, options = {})
|
5
5
|
enum_name = column_name.to_s.pluralize.upcase.to_sym
|
6
6
|
enum_symbols_name = (column_name.to_s.upcase + '_NAMES').to_sym
|
7
7
|
|
@@ -21,6 +21,8 @@ module RailsColumnEnumerator
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
options.merge!(inclusion: const_get(enum_name).values) unless options[:inclusion].present?
|
25
|
+
|
26
|
+
validates column_name, options
|
25
27
|
end
|
26
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_column_enumerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian O'Reilly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|