enum_accessor 2.0.0 → 2.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.
- checksums.yaml +4 -4
- data/README.md +17 -5
- data/lib/enum_accessor.rb +7 -2
- data/lib/enum_accessor/version.rb +1 -1
- data/spec/enum_accessor_spec.rb +16 -0
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8d55d176ac9bb908cb4db598a6c58126b88abc8f
|
|
4
|
+
data.tar.gz: 3aa1ca869f36a19c21f65b5cb853459306cc878a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 664d056bcc81faa2ad01861a1a2cb2cc9400889648aa0883c8c38acc4fbce374d52ef050226ea0496b36866b34fc65ec33b6a08fe62e8ea83087aa34f77b3073
|
|
7
|
+
data.tar.gz: 4d85f92d53fe5e9339853c74449ef83f62150159af3142717cd7e2884ec7047b4773892cf3865f410f7bbbcf329d21963479231b1333cc786c779cde21355402
|
data/README.md
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
EnumAccessor lets you define enum for attributes, and store them as integer in the database.
|
|
4
4
|
|
|
5
|
-
It is very similar to [Official Rails 4.1 Implementation](http://edgeguides.rubyonrails.org/4_1_release_notes.html#active-record-enums), but EnumAccessor offers quite a few advantages:
|
|
5
|
+
It is very similar to [Official Rails 4.1 Implementation](http://edgeguides.rubyonrails.org/4_1_release_notes.html#active-record-enums), but EnumAccessor offers quite a few advantages, with even fewer lines of code. :) Compare [rails version](https://github.com/rails/rails/blob/v4.1.5/activerecord/lib/active_record/enum.rb) with [enum_accessor](https://github.com/kenn/enum_accessor/blob/v2.0.0/lib/enum_accessor.rb).
|
|
6
6
|
|
|
7
7
|
* No-conflict safe predicate methods (`user.status_active?` instead of `user.active?`)
|
|
8
8
|
* Validation
|
|
9
9
|
* Scope
|
|
10
10
|
* Translation
|
|
11
|
+
* Forms
|
|
11
12
|
|
|
12
13
|
Compatible with ActiveRecord 3 or later.
|
|
13
14
|
|
|
@@ -125,22 +126,33 @@ user.human_gender # => 'Female'
|
|
|
125
126
|
User.human_genders # => { 'female' => 'Female', 'male' => 'Male' }
|
|
126
127
|
```
|
|
127
128
|
|
|
129
|
+
## Forms
|
|
130
|
+
|
|
131
|
+
Thanks to the translation support, forms just work as you expect. Pass an inverted `human_*` hash for the select tag options.
|
|
132
|
+
|
|
133
|
+
```haml
|
|
134
|
+
= form_for @user do |f|
|
|
135
|
+
= f.select :gender, User.human_genders.invert
|
|
136
|
+
```
|
|
137
|
+
|
|
128
138
|
## Changelog
|
|
129
139
|
|
|
130
|
-
- v2.
|
|
140
|
+
- v2.1:
|
|
141
|
+
- Support for uniqueness validation
|
|
142
|
+
- v2.0:
|
|
131
143
|
- Reworked to remove the "dict" methods. Now `User.genders.dict` is `User.genders` and `User.genders.human_dict` is `User.human_genders`
|
|
132
|
-
- v1.1
|
|
144
|
+
- v1.1:
|
|
133
145
|
- Validate by default again.
|
|
134
146
|
- Added `:class_attribute` option to specify class attribute to hold definitions
|
|
135
147
|
- Cache translations on the fly
|
|
136
|
-
- v1.0
|
|
148
|
+
- v1.0:
|
|
137
149
|
- Drop support for Ruby 1.8
|
|
138
150
|
- Now getter method returns a String rather than a Symbol
|
|
139
151
|
- Do not validate by default
|
|
140
152
|
- Added `where_gender(:female)` scope
|
|
141
153
|
- Removed the `_raw=` as setter automatically handles both types
|
|
142
154
|
- Removed constants (e.g. `User::GENDERS`) and now use the class attribute to save the definition
|
|
143
|
-
- v0.3
|
|
155
|
+
- v0.3: Add support for `find_or_create_by` - just pass integer value
|
|
144
156
|
|
|
145
157
|
## Other solutions
|
|
146
158
|
|
data/lib/enum_accessor.rb
CHANGED
|
@@ -15,15 +15,20 @@ module EnumAccessor
|
|
|
15
15
|
keys
|
|
16
16
|
else
|
|
17
17
|
raise ArgumentError.new('enum_accessor takes Array or Hash as the second argument')
|
|
18
|
-
end
|
|
18
|
+
end.with_indifferent_access.freeze
|
|
19
19
|
|
|
20
20
|
# Define class attributes
|
|
21
21
|
definition = options[:class_attribute] || column.to_s.pluralize.to_sym
|
|
22
22
|
class_attribute definition
|
|
23
23
|
class_attribute "_human_#{definition}"
|
|
24
|
-
send "#{definition}=", dict
|
|
24
|
+
send "#{definition}=", dict
|
|
25
25
|
send "_human_#{definition}=", {}
|
|
26
26
|
|
|
27
|
+
# ActiveRecord::Enum uses defined_enums for UniquenessValidator
|
|
28
|
+
if respond_to?(:defined_enums)
|
|
29
|
+
defined_enums[column.to_s] = dict
|
|
30
|
+
end
|
|
31
|
+
|
|
27
32
|
# Getter
|
|
28
33
|
define_method(column) do
|
|
29
34
|
send(definition).key(read_attribute(column))
|
data/spec/enum_accessor_spec.rb
CHANGED
|
@@ -107,6 +107,22 @@ describe EnumAccessor do
|
|
|
107
107
|
expect(user.valid?).to be_truthy
|
|
108
108
|
end
|
|
109
109
|
|
|
110
|
+
it 'supports uniqueness validation' do
|
|
111
|
+
class UserValidateWithUniqueness < ActiveRecord::Base
|
|
112
|
+
self.table_name = :users
|
|
113
|
+
enum_accessor :gender, [:female, :male]
|
|
114
|
+
|
|
115
|
+
validates :gender, uniqueness: true
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
UserValidateWithUniqueness.create!(gender: :male)
|
|
119
|
+
|
|
120
|
+
user = UserValidateWithUniqueness.new
|
|
121
|
+
user.gender = 'male'
|
|
122
|
+
expect(user.valid?).to be_falsey
|
|
123
|
+
expect(user.errors[:gender]).to eq(['has already been taken'])
|
|
124
|
+
end
|
|
125
|
+
|
|
110
126
|
it 'supports find_or_create_by' do
|
|
111
127
|
# `find_or_create_by` uses where-based raw value for find,
|
|
112
128
|
# then passes the raw value to the setter method for create.
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: enum_accessor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kenn Ejima
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
105
105
|
version: '0'
|
|
106
106
|
requirements: []
|
|
107
107
|
rubyforge_project:
|
|
108
|
-
rubygems_version: 2.
|
|
108
|
+
rubygems_version: 2.4.8
|
|
109
109
|
signing_key:
|
|
110
110
|
specification_version: 4
|
|
111
111
|
summary: Simple enum fields for ActiveRecord
|