enumerize 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +19 -4
- data/Rakefile +1 -1
- data/lib/enumerize/activerecord.rb +2 -4
- data/lib/enumerize/attribute.rb +14 -9
- data/lib/enumerize/base.rb +6 -4
- data/lib/enumerize/predicates.rb +2 -0
- data/lib/enumerize/set.rb +7 -2
- data/lib/enumerize/value.rb +1 -0
- data/lib/enumerize/version.rb +1 -1
- data/test/activerecord_test.rb +41 -2
- data/test/attribute_test.rb +6 -0
- data/test/value_test.rb +6 -0
- metadata +23 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89ff95b68f2d587db0234b6cae3b434c904b8575
|
4
|
+
data.tar.gz: d758065069a4f5ab996281648c12df3a4ba70b1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ee9ff2df723ab6e83155fe4e9697f9e8dcf12cd11b42dbb0822de7da4c2050269561f51e316ec6ac71b3e3ea77cfc74c70eb899a24af34366cf6a657e9fa3b
|
7
|
+
data.tar.gz: 2e77cbb5dbe9abb980984ce5837ad41009ad5dddc9bf13ceae86dccb58d294feead696a6cbff41a517607587284ba26bb04bfa4d65b82e31e864fed6c72c6ccb
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,36 @@
|
|
1
|
+
## 0.9.0 (December 11, 2014) ##
|
2
|
+
|
3
|
+
### enhancements
|
4
|
+
|
5
|
+
* Add :value_class option (by [@lest](https://github.com/lest))
|
6
|
+
* Use 'defaults' scope in the localization file for the attributes that used across several models. This will help to avoid conflicting keys with model names and attribute names. Example:
|
7
|
+
|
8
|
+
```yml
|
9
|
+
en:
|
10
|
+
enumerize:
|
11
|
+
defaults:
|
12
|
+
sex:
|
13
|
+
male: Male
|
14
|
+
female: Female
|
15
|
+
```
|
16
|
+
|
17
|
+
You still can use the old solution without "default" scope:
|
18
|
+
|
19
|
+
```yml
|
20
|
+
en:
|
21
|
+
enumerize:
|
22
|
+
sex:
|
23
|
+
male: Male
|
24
|
+
female: Female
|
25
|
+
```
|
26
|
+
(by [@nashby](https://github.com/nashby))
|
27
|
+
|
28
|
+
### bug fix
|
29
|
+
* Store values for validation using string keys (by [@nagyt234](https://github.com/nagyt234))
|
30
|
+
* Store custom values for multiple attributes (by [@lest](https://github.com/lest))
|
31
|
+
* Support validations after using AR#becomes (by [@lest](https://github.com/lest))
|
32
|
+
* Do not try to set attribute for not selected attributes (by [@dany1468](https://github.com/dany1468))
|
33
|
+
|
1
34
|
## 0.8.0 (March 4, 2014) ##
|
2
35
|
|
3
36
|
### enhancements
|
data/README.md
CHANGED
@@ -34,6 +34,17 @@ Note that enumerized values are just identificators so if you want to use multi-
|
|
34
34
|
ActiveRecord:
|
35
35
|
|
36
36
|
```ruby
|
37
|
+
class CreateUsers < ActiveRecord::Migration
|
38
|
+
def change
|
39
|
+
create_table :users do |t|
|
40
|
+
t.string :sex
|
41
|
+
t.string :role
|
42
|
+
|
43
|
+
t.timestamps
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
37
48
|
class User < ActiveRecord::Base
|
38
49
|
extend Enumerize
|
39
50
|
|
@@ -78,14 +89,15 @@ en:
|
|
78
89
|
female: "Female"
|
79
90
|
```
|
80
91
|
|
81
|
-
or if you use `sex` attribute across several models you can use
|
92
|
+
or if you use `sex` attribute across several models you can use `defaults` scope:
|
82
93
|
|
83
94
|
```ruby
|
84
95
|
en:
|
85
96
|
enumerize:
|
86
|
-
|
87
|
-
|
88
|
-
|
97
|
+
defaults:
|
98
|
+
sex:
|
99
|
+
male: "Male"
|
100
|
+
female: "Female"
|
89
101
|
```
|
90
102
|
|
91
103
|
You can also pass `i18n_scope` option to specify scope (or array of scopes) storring the translations. Note that `i18n_scope` option does not accept scope as array:
|
@@ -314,6 +326,9 @@ end
|
|
314
326
|
describe User do
|
315
327
|
it { should enumerize(:sex).in(:male, :female) }
|
316
328
|
it { should enumerize(:sex).in(:male, :female).with_default(:male) }
|
329
|
+
|
330
|
+
# or with RSpec 3 expect syntax
|
331
|
+
it { is_expected.to enumerize(:sex).in(:male, :female) }
|
317
332
|
end
|
318
333
|
```
|
319
334
|
|
data/Rakefile
CHANGED
@@ -45,7 +45,7 @@ module Enumerize
|
|
45
45
|
# https://github.com/brainspec/enumerize/issues/74
|
46
46
|
def write_attribute(attr_name, value)
|
47
47
|
if self.class.enumerized_attributes[attr_name]
|
48
|
-
_enumerized_values_for_validation[attr_name] = value
|
48
|
+
_enumerized_values_for_validation[attr_name.to_s] = value
|
49
49
|
end
|
50
50
|
|
51
51
|
super
|
@@ -55,9 +55,7 @@ module Enumerize
|
|
55
55
|
def becomes(klass)
|
56
56
|
became = super
|
57
57
|
klass.enumerized_attributes.each do |attr|
|
58
|
-
|
59
|
-
became.send("#{attr.name}=", send(attr.name))
|
60
|
-
end
|
58
|
+
became.send("#{attr.name}=", send(attr.name))
|
61
59
|
end
|
62
60
|
|
63
61
|
became
|
data/lib/enumerize/attribute.rb
CHANGED
@@ -9,7 +9,10 @@ module Enumerize
|
|
9
9
|
|
10
10
|
@klass = klass
|
11
11
|
@name = name.to_sym
|
12
|
-
|
12
|
+
|
13
|
+
value_class = options.fetch(:value_class, Value)
|
14
|
+
@values = Array(options[:in]).map { |v| value_class.new(self, *v) }
|
15
|
+
|
13
16
|
@value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
|
14
17
|
@value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
|
15
18
|
|
@@ -84,8 +87,6 @@ module Enumerize
|
|
84
87
|
end
|
85
88
|
|
86
89
|
def #{name}=(new_value)
|
87
|
-
_enumerized_values_for_validation[:#{name}] = new_value.nil? ? nil : new_value.to_s
|
88
|
-
|
89
90
|
allowed_value_or_nil = self.class.enumerized_attributes[:#{name}].find_value(new_value)
|
90
91
|
allowed_value_or_nil = allowed_value_or_nil.value unless allowed_value_or_nil.nil?
|
91
92
|
|
@@ -96,6 +97,10 @@ module Enumerize
|
|
96
97
|
else
|
97
98
|
@#{name} = allowed_value_or_nil
|
98
99
|
end
|
100
|
+
|
101
|
+
_enumerized_values_for_validation['#{name}'] = new_value.nil? ? nil : new_value.to_s
|
102
|
+
|
103
|
+
allowed_value_or_nil
|
99
104
|
end
|
100
105
|
|
101
106
|
def #{name}_text
|
@@ -131,19 +136,19 @@ module Enumerize
|
|
131
136
|
end
|
132
137
|
|
133
138
|
def #{name}=(values)
|
134
|
-
_enumerized_values_for_validation[:#{name}] = values.respond_to?(:map) ? values.map(&:to_s) : values
|
135
|
-
|
136
139
|
@_#{name}_enumerized_set = Enumerize::Set.new(self, self.class.enumerized_attributes[:#{name}], values)
|
137
|
-
|
140
|
+
raw_values = #{name}.values.map(&:value)
|
138
141
|
|
139
142
|
if defined?(super)
|
140
|
-
super
|
143
|
+
super raw_values
|
141
144
|
elsif respond_to?(:write_attribute, true)
|
142
|
-
write_attribute '#{name}',
|
145
|
+
write_attribute '#{name}', raw_values
|
143
146
|
else
|
144
|
-
@#{name} =
|
147
|
+
@#{name} = raw_values
|
145
148
|
end
|
146
149
|
|
150
|
+
_enumerized_values_for_validation['#{name}'] = values.respond_to?(:map) ? values.map(&:to_s) : values
|
151
|
+
|
147
152
|
#{name}
|
148
153
|
end
|
149
154
|
RUBY
|
data/lib/enumerize/base.rb
CHANGED
@@ -50,6 +50,8 @@ module Enumerize
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def read_attribute_for_validation(key)
|
53
|
+
key = key.to_s
|
54
|
+
|
53
55
|
if _enumerized_values_for_validation.has_key?(key)
|
54
56
|
_enumerized_values_for_validation[key]
|
55
57
|
else
|
@@ -80,13 +82,13 @@ module Enumerize
|
|
80
82
|
self.class.enumerized_attributes.each do |attr|
|
81
83
|
# remove after dropping support for Rails 3.x
|
82
84
|
# https://github.com/brainspec/enumerize/issues/101
|
83
|
-
|
84
|
-
public_send(attr.name)
|
85
|
+
begin
|
86
|
+
attr_value = public_send(attr.name)
|
85
87
|
rescue ActiveModel::MissingAttributeError
|
86
|
-
|
88
|
+
next
|
87
89
|
end
|
88
90
|
|
89
|
-
if !attr_value && !_enumerized_values_for_validation.key?(attr.name)
|
91
|
+
if !attr_value && !_enumerized_values_for_validation.key?(attr.name.to_s)
|
90
92
|
value = attr.default_value
|
91
93
|
|
92
94
|
if value.respond_to?(:call)
|
data/lib/enumerize/predicates.rb
CHANGED
data/lib/enumerize/set.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
|
1
3
|
module Enumerize
|
2
4
|
class Set
|
3
5
|
include Enumerable
|
@@ -8,12 +10,15 @@ module Enumerize
|
|
8
10
|
def initialize(obj, attr, values)
|
9
11
|
@obj = obj
|
10
12
|
@attr = attr
|
11
|
-
@values =
|
13
|
+
@values = []
|
12
14
|
|
13
15
|
if values.respond_to?(:each)
|
14
16
|
values.each do |input|
|
15
17
|
value = @attr.find_value(input)
|
16
|
-
|
18
|
+
|
19
|
+
if value && !@values.include?(value)
|
20
|
+
@values << value
|
21
|
+
end
|
17
22
|
end
|
18
23
|
end
|
19
24
|
end
|
data/lib/enumerize/value.rb
CHANGED
@@ -34,6 +34,7 @@ module Enumerize
|
|
34
34
|
def i18n_keys
|
35
35
|
@i18n_keys ||= begin
|
36
36
|
i18n_keys = i18n_scopes
|
37
|
+
i18n_keys << [:"enumerize.defaults.#{@attr.name}.#{self}"]
|
37
38
|
i18n_keys << [:"enumerize.#{@attr.name}.#{self}"]
|
38
39
|
i18n_keys << self.humanize # humanize value if there are no translations
|
39
40
|
i18n_keys.flatten
|
data/lib/enumerize/version.rb
CHANGED
data/test/activerecord_test.rb
CHANGED
@@ -57,6 +57,7 @@ end
|
|
57
57
|
|
58
58
|
class UniqStatusUser < User
|
59
59
|
validates :status, uniqueness: true
|
60
|
+
validates :sex, presence: true
|
60
61
|
end
|
61
62
|
|
62
63
|
describe Enumerize::ActiveRecordSupport do
|
@@ -92,7 +93,8 @@ describe Enumerize::ActiveRecordSupport do
|
|
92
93
|
it 'does not set default value for not selected attributes' do
|
93
94
|
User.delete_all
|
94
95
|
User.create!(:sex => :male)
|
95
|
-
|
96
|
+
|
97
|
+
assert_equal ['id'], User.select(:id).first.attributes.keys
|
96
98
|
end
|
97
99
|
|
98
100
|
it 'has default value with lambda' do
|
@@ -128,9 +130,18 @@ describe Enumerize::ActiveRecordSupport do
|
|
128
130
|
user.errors[:role].must_include 'is not included in the list'
|
129
131
|
end
|
130
132
|
|
131
|
-
it 'validates inclusion when using write_attribute' do
|
133
|
+
it 'validates inclusion when using write_attribute with string attribute' do
|
134
|
+
user = User.new
|
135
|
+
user.send(:write_attribute, 'role', 'wrong')
|
136
|
+
user.read_attribute_for_validation(:role).must_equal 'wrong'
|
137
|
+
user.wont_be :valid?
|
138
|
+
user.errors[:role].must_include 'is not included in the list'
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'validates inclusion when using write_attribute with symbol attribute' do
|
132
142
|
user = User.new
|
133
143
|
user.send(:write_attribute, :role, 'wrong')
|
144
|
+
user.read_attribute_for_validation(:role).must_equal 'wrong'
|
134
145
|
user.wont_be :valid?
|
135
146
|
user.errors[:role].must_include 'is not included in the list'
|
136
147
|
end
|
@@ -199,6 +210,22 @@ describe Enumerize::ActiveRecordSupport do
|
|
199
210
|
user.must_be :valid?
|
200
211
|
end
|
201
212
|
|
213
|
+
it 'stores custom values for multiple attributes' do
|
214
|
+
User.delete_all
|
215
|
+
|
216
|
+
klass = Class.new(User)
|
217
|
+
klass.enumerize :interests, in: { music: 0, sports: 1, dancing: 2, programming: 3}, multiple: true
|
218
|
+
|
219
|
+
user = klass.new
|
220
|
+
user.interests << :music
|
221
|
+
user.read_attribute(:interests).must_equal [0]
|
222
|
+
user.interests.must_equal %w(music)
|
223
|
+
user.save
|
224
|
+
|
225
|
+
user = klass.find(user.id)
|
226
|
+
user.interests.must_equal %w(music)
|
227
|
+
end
|
228
|
+
|
202
229
|
it 'adds scope' do
|
203
230
|
User.delete_all
|
204
231
|
|
@@ -258,6 +285,18 @@ describe Enumerize::ActiveRecordSupport do
|
|
258
285
|
user.errors[:status].wont_be :empty?
|
259
286
|
end
|
260
287
|
|
288
|
+
it 'is valid after #becomes' do
|
289
|
+
User.delete_all
|
290
|
+
user = User.new
|
291
|
+
user.sex = :male
|
292
|
+
user.save!
|
293
|
+
|
294
|
+
uniq_user = User.find(user.id).becomes(UniqStatusUser)
|
295
|
+
uniq_user.valid?
|
296
|
+
|
297
|
+
uniq_user.errors.must_be_empty
|
298
|
+
end
|
299
|
+
|
261
300
|
it 'supports multiple attributes in #becomes' do
|
262
301
|
User.delete_all
|
263
302
|
|
data/test/attribute_test.rb
CHANGED
@@ -19,6 +19,12 @@ describe Enumerize::Attribute do
|
|
19
19
|
attr.name.must_equal :foo
|
20
20
|
end
|
21
21
|
|
22
|
+
it 'uses custom value class' do
|
23
|
+
value_class = Class.new(Enumerize::Value)
|
24
|
+
build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
|
25
|
+
attr.values.first.must_be_instance_of value_class
|
26
|
+
end
|
27
|
+
|
22
28
|
describe 'i18n scopes' do
|
23
29
|
it 'returns scopes from options' do
|
24
30
|
build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
|
data/test/value_test.rb
CHANGED
@@ -21,6 +21,12 @@ describe Enumerize::Value do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
it 'uses default translation from the "default" section if its present' do
|
25
|
+
store_translations(:en, :enumerize => {:defaults => {:attribute_name => {:test_value => "Common translation"}}}) do
|
26
|
+
value.text.must_be :==, "Common translation"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
24
30
|
it 'uses model specific translation' do
|
25
31
|
attr.i18n_scopes = ["enumerize.model_name.attribute_name"]
|
26
32
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nartimov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -97,8 +97,27 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
99
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.2.
|
100
|
+
rubygems_version: 2.2.2
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
|
104
|
-
test_files:
|
104
|
+
test_files:
|
105
|
+
- test/activerecord_test.rb
|
106
|
+
- test/attribute_map_test.rb
|
107
|
+
- test/attribute_test.rb
|
108
|
+
- test/base_test.rb
|
109
|
+
- test/formtastic_test.rb
|
110
|
+
- test/module_attributes_test.rb
|
111
|
+
- test/mongo_mapper_test.rb
|
112
|
+
- test/mongoid_test.rb
|
113
|
+
- test/multiple_test.rb
|
114
|
+
- test/predicates_test.rb
|
115
|
+
- test/rails_admin_test.rb
|
116
|
+
- test/rspec_matcher_test.rb
|
117
|
+
- test/rspec_spec.rb
|
118
|
+
- test/set_test.rb
|
119
|
+
- test/simple_form_test.rb
|
120
|
+
- test/support/mock_controller.rb
|
121
|
+
- test/support/view_test_helper.rb
|
122
|
+
- test/test_helper.rb
|
123
|
+
- test/value_test.rb
|