simple_enum 2.0.1 → 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 -14
- data/lib/simple_enum/enum.rb +5 -0
- data/lib/simple_enum/version.rb +1 -1
- data/lib/simple_enum/view_helpers.rb +16 -0
- data/spec/simple_enum/enum_spec.rb +10 -0
- data/spec/simple_enum/view_helpers_spec.rb +9 -0
- 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: 764053ae9a23795f23ac3d6c9a108d5c1c17f307
|
4
|
+
data.tar.gz: 7e3bf97b4177c12cf5582b1302dced1b90a90b39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e40c68baf656cb1064153f47eb3cc826375d578988bd6260d5016bdce25e42accf6c3de77cce14adb0b44433f78a9389a4f3b581d54d74335871e635ab08b8b
|
7
|
+
data.tar.gz: be89ccd3fa051379cc0ea45be3ceec545a14d1a0af4e5a6de5e2afc8bcc182922106e0719c0ebee5a894fdfce19f861abbc0b7bb428bfaa7330de5d89a78bf8a
|
data/README.md
CHANGED
@@ -96,9 +96,10 @@ joe.gender_cd # => 0
|
|
96
96
|
Accessing actual enum values is possible at the class level:
|
97
97
|
|
98
98
|
```ruby
|
99
|
-
User.genders
|
100
|
-
User.genders[:male]
|
101
|
-
User.
|
99
|
+
User.genders # => #<SimpleEnum::Enum:0x0....>
|
100
|
+
User.genders[:male] # => 0
|
101
|
+
User.genders.values_at(:male, :female) # => [0, 1]
|
102
|
+
User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd = 1)
|
102
103
|
```
|
103
104
|
|
104
105
|
### Wait, there's more!
|
@@ -153,6 +154,7 @@ User.females # => #<ActiveRecord::Relation:0x0.....> (WHERE gender_cd
|
|
153
154
|
user.gender_was
|
154
155
|
# => :male
|
155
156
|
```
|
157
|
+
- Require translated enum values? See [SimpleEnum::ViewHelpers](https://github.com/lwe/simple_enum/blob/master/lib/simple_enum/view_helpers.rb)
|
156
158
|
- Need to provide custom options for the mongoid field, or skip the automatically generated field?
|
157
159
|
|
158
160
|
```ruby
|
@@ -219,7 +221,7 @@ Do not use values named after existing, or well known method names, like `new`,
|
|
219
221
|
as_enum :handle, [:new, :create, :update]
|
220
222
|
|
221
223
|
# GOOD, prefixes all methods
|
222
|
-
as_enum :handle, [:new, :create, :update], :
|
224
|
+
as_enum :handle, [:new, :create, :update], prefix: true
|
223
225
|
```
|
224
226
|
|
225
227
|
Searching for certain values by using the finder methods:
|
@@ -231,16 +233,17 @@ User.females # => returns an ActiveRecord::Relation
|
|
231
233
|
Contributors
|
232
234
|
------------
|
233
235
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
236
|
+
- @dmitry - bugfixes and other improvements
|
237
|
+
- @tarsolya - implemented all the ruby 1.9 and rails 3 goodness!
|
238
|
+
- @dbalatero - rails 2.3.5 bugfix & validator fixes
|
239
|
+
- @johnthethird - feature for `_for_select` to return the values
|
240
|
+
- @sinsiliux - ruby 1.9 fixes and removed AR dependency
|
241
|
+
- @sled - mongoid support
|
242
|
+
- @abrom - `find_by_...` method
|
243
|
+
- @mhuggins - translations fixes
|
244
|
+
- @patbenatar - for helping move towards 2.0 (scopes et all)
|
245
|
+
- @abacha - translation helpers
|
246
|
+
- and all others: https://github.com/lwe/simple_enum/graphs/contributors thanks
|
244
247
|
|
245
248
|
License & Copyright
|
246
249
|
-------------------
|
data/lib/simple_enum/enum.rb
CHANGED
data/lib/simple_enum/version.rb
CHANGED
@@ -29,6 +29,22 @@ module SimpleEnum
|
|
29
29
|
}
|
30
30
|
end
|
31
31
|
|
32
|
+
# Helper method to return the translated value of an enum.
|
33
|
+
#
|
34
|
+
# translate_enum(user, :gender) # => "Frau"
|
35
|
+
#
|
36
|
+
# Has been aliased to `te` as a convenience method as well.
|
37
|
+
#
|
38
|
+
# record - The model instance holding the enum
|
39
|
+
# key - The Symbol with the name of the enum, i.e. same key as used in the
|
40
|
+
# `as_enum` call
|
41
|
+
#
|
42
|
+
# Returns String with translation of enum
|
43
|
+
def translate_enum(record, key)
|
44
|
+
record.class.human_enum_name(key, record.public_send(key))
|
45
|
+
end
|
46
|
+
alias_method :te, :translate_enum
|
47
|
+
|
32
48
|
private
|
33
49
|
|
34
50
|
def translate_enum_key(enum, key)
|
@@ -99,4 +99,14 @@ describe SimpleEnum::Enum do
|
|
99
99
|
expect(subject.include?(2)).to be_false
|
100
100
|
end
|
101
101
|
end
|
102
|
+
|
103
|
+
context '#values_at' do
|
104
|
+
it 'fetches multiple values by string' do
|
105
|
+
expect(subject.values_at("male", "female")).to eq [0, 1]
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'fetches multiple values by symbol' do
|
109
|
+
expect(subject.values_at(:male)).to eq [0]
|
110
|
+
end
|
111
|
+
end
|
102
112
|
end
|
@@ -59,4 +59,13 @@ describe SimpleEnum::ViewHelpers, i18n: true do
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
62
|
+
|
63
|
+
context '#translate_enum' do
|
64
|
+
let(:fake_object) { klass.new }
|
65
|
+
it "translates with object scope" do
|
66
|
+
fake_object.gender = :male
|
67
|
+
expect(klass).to receive(:human_enum_name).with(:gender, :male) { "Mr." }
|
68
|
+
expect(helper.translate_enum(fake_object, :gender)).to eq "Mr."
|
69
|
+
end
|
70
|
+
end
|
62
71
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukas Westermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|