enumerize 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f08aba602317c54ffaf5f735e09a205a5d1a624c
4
+ data.tar.gz: 87f7613830f330b958dbd0a61f7a7ac89c262623
5
+ SHA512:
6
+ metadata.gz: a0226ef792ee39084f80286b886880003659bec15c62e1e98938048e6b3accb03b04330591e92d42a2d79f94fb874d80fe14395b4007d3576ac1285bb0b3ff77
7
+ data.tar.gz: 48448538be205b068c3c7f01ca68c6780e7e4d2fd0db6f353455ab1462f8db587e744633cd605c7872b77f6bf6ecd353e1ba076998ebc04108e04322ab024c48
data/.travis.yml CHANGED
@@ -9,11 +9,8 @@ gemfile:
9
9
  rvm:
10
10
  - 1.9.3
11
11
  - 2.0.0
12
+ - 2.1.0
12
13
  - jruby-19mode
13
- - rbx-19mode
14
- matrix:
15
- allow_failures:
16
- - rvm: jruby-19mode
17
14
  notifications:
18
15
  email:
19
16
  recipients:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## 0.8.0 (March 4, 2014) ##
2
+
3
+ ### enhancements
4
+ * Integration with SimpleForm's `input_field` (by [@nashby](https://github.com/nashby))
5
+ * Support multiple attributes in Active Record #becomes method (by [@lest](https://github.com/lest))
6
+ * Add ability to specify localization scope with `i18n_scope` option (by [@dreamfall](https://github.com/dreamfall))
7
+
8
+ ### bug fix
9
+ * Fix Rails Admin integration when custom values are used (by [@brenes](https://github.com/brenes))
10
+ * Fix RSpec integration using enumerize with Spring (by [@winston](https://github.com/winston))
11
+ * Return proper RSpec failure message for enumerized attribute with default value (by [@nashby](https://github.com/nashby))
12
+ * Return proper RSpec description for enumerized attribute without default value (by [@andreygerasimchuk](https://github.com/andreygerasimchuk))
13
+ * Do not try to set default value for not selected attributes (by [@nashby](https://github.com/nashby))
14
+ * Fix uniqueness validation with Active Record (by [@lest](https://github.com/lest))
15
+ * Fix loading of attributes with multiple: true in mongoid (by [glebtv](https://github.com/glebtv))
16
+ * Serialize value as scalar type (by [@ka8725](https://github.com/ka8725))
17
+
1
18
  ## 0.7.0 (August 21, 2013) ##
2
19
 
3
20
  ### enhancements
@@ -22,7 +39,7 @@
22
39
 
23
40
  ### enhancements
24
41
  * Use inclusion error message for invalid values (by [@lest](https://github.com/lest))
25
- * Add `:only` and `except` options to the `Attribute#options` method. (by [@thehappycoder](https://github.com/thehappycoder))
42
+ * Add `:only` and `except` options to the `Attribute#options` method. (by [@thehappycoder](https://github.com/thehappycoder) and [@randoum](https://github.com/randoum))
26
43
  * ActiveRecord scopes. (by [@lest](https://github.com/lest), [@banyan](https://github.com/banyan) and [@nashby](https://github.com/nashby))
27
44
  * Support for RailsAdmin (by [@drewda](https://github.com/drewda))
28
45
 
data/Gemfile CHANGED
@@ -10,6 +10,13 @@ gem 'rails', '~> 3.2.0', :require => false
10
10
  gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
11
  gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
12
 
13
+ platforms :rbx do
14
+ gem 'rubysl', '~> 2.0'
15
+ gem 'psych'
16
+ gem 'rubinius-developer_tools'
17
+ gem 'rubysl-test-unit'
18
+ end
19
+
13
20
  gem 'mongoid'
14
21
  gem 'mongo_mapper'
15
22
  gem 'simple_form'
data/Gemfile.rails4 CHANGED
@@ -6,11 +6,18 @@ gem 'rake'
6
6
  gem 'minitest', '~> 4.1'
7
7
  gem 'rspec', :require => false
8
8
 
9
- gem 'rails', '4.0.0', :require => false
9
+ gem 'rails', '4.0.1', :require => false
10
10
  gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
11
  gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
12
 
13
- gem 'mongoid', github: 'mongoid/mongoid'
13
+ platforms :rbx do
14
+ gem 'rubysl', '~> 2.0'
15
+ gem 'psych'
16
+ gem 'rubinius-developer_tools'
17
+ gem 'rubysl-test-unit'
18
+ end
19
+
20
+ gem 'mongoid', github: 'mongoid/mongoid', ref: 'f91feef0a0c6b83a1b878e154f1014536aa1c298'
14
21
  gem 'mongo_mapper', github: 'jnunemaker/mongomapper'
15
- gem 'simple_form', github: 'plataformatec/simple_form'
22
+ gem 'simple_form'
16
23
  gem 'formtastic', github: 'justinfrench/formtastic'
data/README.md CHANGED
@@ -28,6 +28,9 @@ class User
28
28
  end
29
29
  ```
30
30
 
31
+ Note that enumerized values are just identificators so if you want to use multi-word, etc. values you should use `I18n` feature.
32
+
33
+
31
34
  ActiveRecord:
32
35
 
33
36
  ```ruby
@@ -85,6 +88,30 @@ en:
85
88
  female: "Female"
86
89
  ```
87
90
 
91
+ 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:
92
+
93
+
94
+ ```ruby
95
+ class Person
96
+ extend Enumerize
97
+ extend ActiveModel::Naming
98
+
99
+ enumerize :sex, in: %w[male female], i18n_scope: "sex"
100
+ enumerize :color, in: %w[black white], i18n_scope: ["various.colors", "colors"]
101
+ end
102
+
103
+ # localization file
104
+ en:
105
+ sex:
106
+ male: "Male"
107
+ female: "Female"
108
+ various:
109
+ colors:
110
+ black: "Black"
111
+ colors:
112
+ white: "White"
113
+ ```
114
+
88
115
  Note that if you want to use I18n feature with plain Ruby object don't forget to extend it with `ActiveModel::Naming`:
89
116
 
90
117
  ```ruby
@@ -188,6 +215,9 @@ user = User.new
188
215
  user.role = :user
189
216
  user.role #=> 'user'
190
217
  user.role_value #=> 1
218
+
219
+ User.role.find_value(:user).value #=> 1
220
+ User.role.find_value(:admin).value #=> 2
191
221
  ```
192
222
 
193
223
  ActiveRecord scopes:
@@ -287,6 +317,21 @@ describe User do
287
317
  end
288
318
  ```
289
319
 
320
+ ### Minitest with Shoulda
321
+
322
+ You can use the RSpec matcher with shoulda in your tests by adding two lines in your `test_helper.rb` inside `class ActiveSupport::TestCase` definition:
323
+
324
+ ```ruby
325
+ class ActiveSupport::TestCase
326
+ ActiveRecord::Migration.check_pending!
327
+
328
+ require 'enumerize/integrations/rspec'
329
+ extend Enumerize::Integrations::RSpec
330
+
331
+ ...
332
+ end
333
+ ```
334
+
290
335
  ### Other Integrations
291
336
 
292
337
  Enumerize integrates with the following automatically:
@@ -1,26 +1,22 @@
1
1
  module Enumerize
2
- module ActiveRecord
2
+ module ActiveRecordSupport
3
3
  def enumerize(name, options={})
4
4
  super
5
5
 
6
- if options[:scope]
7
- _enumerize_module.dependent_eval do
8
- if defined?(::ActiveRecord::Base) && self < ::ActiveRecord::Base
6
+ _enumerize_module.dependent_eval do
7
+ if defined?(::ActiveRecord::Base) && self < ::ActiveRecord::Base
8
+ if options[:scope]
9
9
  _define_scope_methods!(name, options)
10
+ end
10
11
 
11
- class_eval do
12
- # https://github.com/brainspec/enumerize/issues/74
13
- def write_attribute(attr_name, value)
14
- _enumerized_values_for_validation[attr_name] = value
12
+ include InstanceMethods
15
13
 
16
- super
17
- end
14
+ # Since Rails use `allocate` method on models and initializes them with `init_with` method.
15
+ # This way `initialize` method is not being called, but `after_initialize` callback always gets triggered.
16
+ after_initialize :_set_default_value_for_enumerized_attributes
18
17
 
19
- # Since Rails use `allocate` method on models and initializes them with `init_with` method.
20
- # This way `initialize` method is not being called, but `after_initialize` callback always gets triggered.
21
- after_initialize :_set_default_value_for_enumerized_attributes
22
- end
23
- end
18
+ # https://github.com/brainspec/enumerize/issues/111
19
+ require 'enumerize/hooks/uniqueness'
24
20
  end
25
21
  end
26
22
  end
@@ -44,5 +40,28 @@ module Enumerize
44
40
  end
45
41
  end
46
42
  end
43
+
44
+ module InstanceMethods
45
+ # https://github.com/brainspec/enumerize/issues/74
46
+ def write_attribute(attr_name, value)
47
+ if self.class.enumerized_attributes[attr_name]
48
+ _enumerized_values_for_validation[attr_name] = value
49
+ end
50
+
51
+ super
52
+ end
53
+
54
+ # Support multiple enumerized attributes
55
+ def becomes(klass)
56
+ became = super
57
+ klass.enumerized_attributes.each do |attr|
58
+ if attr.is_a? Multiple
59
+ became.send("#{attr.name}=", send(attr.name))
60
+ end
61
+ end
62
+
63
+ became
64
+ end
65
+ end
47
66
  end
48
67
  end
@@ -1,6 +1,6 @@
1
1
  module Enumerize
2
2
  class Attribute
3
- attr_reader :name, :values, :default_value
3
+ attr_reader :name, :values, :default_value, :i18n_scope
4
4
 
5
5
  def initialize(klass, name, options={})
6
6
  raise ArgumentError, ':in option is required' unless options[:in]
@@ -13,6 +13,11 @@ module Enumerize
13
13
  @value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
14
14
  @value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
15
15
 
16
+ if options[:i18n_scope]
17
+ raise ArgumentError, ':i18n_scope option accepts only String or Array of strings' unless Array(options[:i18n_scope]).all? { |s| s.is_a?(String) }
18
+ @i18n_scope = options[:i18n_scope]
19
+ end
20
+
16
21
  if options[:default]
17
22
  @default_value = find_default_value(options[:default])
18
23
  raise ArgumentError, 'invalid default value' unless @default_value
@@ -31,8 +36,14 @@ module Enumerize
31
36
  @value_hash[value.to_s] unless value.nil?
32
37
  end
33
38
 
34
- def i18n_suffix
35
- @klass.model_name.i18n_key if @klass.respond_to?(:model_name)
39
+ def i18n_scopes
40
+ @i18n_scopes ||= if i18n_scope
41
+ scopes = Array(i18n_scope)
42
+ elsif @klass.respond_to?(:model_name)
43
+ scopes = ["enumerize.#{@klass.model_name.i18n_key}.#{name}"]
44
+ else
45
+ []
46
+ end
36
47
  end
37
48
 
38
49
  def options(options = {})
@@ -105,6 +116,8 @@ module Enumerize
105
116
  unless defined?(@_#{name}_enumerized_set)
106
117
  if defined?(super)
107
118
  self.#{name} = super
119
+ elsif respond_to?(:read_attribute)
120
+ self.#{name} = read_attribute(:#{name})
108
121
  else
109
122
  if defined?(@#{name})
110
123
  self.#{name} = @#{name}
@@ -78,7 +78,15 @@ module Enumerize
78
78
 
79
79
  def _set_default_value_for_enumerized_attributes
80
80
  self.class.enumerized_attributes.each do |attr|
81
- if !public_send(attr.name) && !_enumerized_values_for_validation.key?(attr.name)
81
+ # remove after dropping support for Rails 3.x
82
+ # https://github.com/brainspec/enumerize/issues/101
83
+ attr_value = begin
84
+ public_send(attr.name)
85
+ rescue ActiveModel::MissingAttributeError
86
+ nil
87
+ end
88
+
89
+ if !attr_value && !_enumerized_values_for_validation.key?(attr.name)
82
90
  value = attr.default_value
83
91
 
84
92
  if value.respond_to?(:call)
@@ -7,9 +7,22 @@ module Enumerize
7
7
 
8
8
  included do
9
9
  alias_method_chain :input, :enumerize
10
+ alias_method_chain :input_field, :enumerize
10
11
  end
11
12
 
12
13
  def input_with_enumerize(attribute_name, options={}, &block)
14
+ add_input_options_for_enumerized_attribute(attribute_name, options)
15
+ input_without_enumerize(attribute_name, options, &block)
16
+ end
17
+
18
+ def input_field_with_enumerize(attribute_name, options={})
19
+ add_input_options_for_enumerized_attribute(attribute_name, options)
20
+ input_field_without_enumerize(attribute_name, options)
21
+ end
22
+
23
+ private
24
+
25
+ def add_input_options_for_enumerized_attribute(attribute_name, options)
13
26
  klass = object.class
14
27
 
15
28
  if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[attribute_name])
@@ -19,8 +32,6 @@ module Enumerize
19
32
  options[:input_html] = options.fetch(:input_html, {}).merge(:multiple => true)
20
33
  end
21
34
  end
22
-
23
- input_without_enumerize(attribute_name, options, &block)
24
35
  end
25
36
  end
26
37
  end
@@ -0,0 +1,23 @@
1
+ require 'active_support/concern'
2
+
3
+ module Enumerize
4
+ module Hooks
5
+ module UniquenessValidator
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ alias_method_chain :validate_each, :enumerize
10
+ end
11
+
12
+ def validate_each_with_enumerize(record, name, value)
13
+ if record.class.respond_to?(:enumerized_attributes) && (attr = record.class.enumerized_attributes[name])
14
+ value = attr.find_value(value).try(:value)
15
+ end
16
+
17
+ validate_each_without_enumerize(record, name, value)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ ::ActiveRecord::Validations::UniquenessValidator.send :include, Enumerize::Hooks::UniquenessValidator
@@ -7,7 +7,7 @@ module Enumerize
7
7
 
8
8
  _enumerize_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
9
9
  def #{name}_enum
10
- self.class.enumerized_attributes[:#{name}].options
10
+ self.class.enumerized_attributes[:#{name}].values.map{|v| [v.text, v.value]}
11
11
  end
12
12
  RUBY
13
13
  end
@@ -22,7 +22,7 @@ module Enumerize
22
22
  message = " expected :#{attr} to allow value#{values.size == 1 ? nil : 's'}: #{quote_values(values)},"
23
23
  message += " but it allows #{quote_values(enumerized_values)} instead"
24
24
 
25
- if default
25
+ if default && !matches_default_value?
26
26
  message = " expected :#{attr} to have #{default.inspect} as default value,"
27
27
  message += " but it sets #{enumerized_default.inspect} instead"
28
28
  end
@@ -33,6 +33,8 @@ module Enumerize
33
33
  def description
34
34
  description = "enumerize :#{attr} in: #{quote_values(values)}"
35
35
  description += " with #{default.inspect} as default value" if default
36
+
37
+ description
36
38
  end
37
39
 
38
40
  def matches?(subject)
@@ -4,21 +4,23 @@ module Enumerize
4
4
  class Value < String
5
5
  include Predicatable
6
6
 
7
+ attr_reader :value
8
+
7
9
  def initialize(attr, name, value=nil)
8
10
  @attr = attr
9
- @value = value || name.to_s
11
+ @value = value.nil? ? name.to_s : value
10
12
 
11
13
  super(name.to_s)
12
14
  end
13
15
 
14
- def value
15
- @value
16
- end
17
-
18
16
  def text
19
17
  I18n.t(i18n_keys[0], :default => i18n_keys[1..-1])
20
18
  end
21
19
 
20
+ def encode_with(coder)
21
+ coder.represent_object(self.class.superclass, @value)
22
+ end
23
+
22
24
  private
23
25
 
24
26
  def define_query_method(value)
@@ -31,19 +33,15 @@ module Enumerize
31
33
 
32
34
  def i18n_keys
33
35
  @i18n_keys ||= begin
34
- i18n_keys = []
35
- i18n_keys << i18n_scope(i18n_suffix)
36
- i18n_keys << i18n_scope
36
+ i18n_keys = i18n_scopes
37
+ i18n_keys << [:"enumerize.#{@attr.name}.#{self}"]
37
38
  i18n_keys << self.humanize # humanize value if there are no translations
39
+ i18n_keys.flatten
38
40
  end
39
41
  end
40
42
 
41
- def i18n_scope(suffix = nil)
42
- :"enumerize.#{suffix}#{@attr.name}.#{self}"
43
- end
44
-
45
- def i18n_suffix
46
- "#{@attr.i18n_suffix}." if @attr.i18n_suffix
43
+ def i18n_scopes
44
+ @attr.i18n_scopes.map { |s| :"#{s}.#{self}" }
47
45
  end
48
46
  end
49
47
  end
@@ -1,3 +1,3 @@
1
1
  module Enumerize
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/enumerize.rb CHANGED
@@ -8,11 +8,12 @@ module Enumerize
8
8
  autoload :Set, 'enumerize/set'
9
9
  autoload :Base, 'enumerize/base'
10
10
  autoload :Module, 'enumerize/module'
11
- autoload :ActiveRecord, 'enumerize/activerecord'
12
11
  autoload :Predicates, 'enumerize/predicates'
13
12
  autoload :Predicatable, 'enumerize/predicatable'
14
13
  autoload :ModuleAttributes, 'enumerize/module_attributes'
15
14
 
15
+ autoload :ActiveRecordSupport, 'enumerize/activerecord'
16
+
16
17
  def self.included(base)
17
18
  ActiveSupport::Deprecation.warn '`include Enumerize` was deprecated. Please use `extend Enumerize`.', caller
18
19
  extended(base)
@@ -21,7 +22,7 @@ module Enumerize
21
22
  def self.extended(base)
22
23
  base.send :include, Enumerize::Base
23
24
  base.extend Enumerize::Predicates
24
- base.extend Enumerize::ActiveRecord
25
+ base.extend Enumerize::ActiveRecordSupport
25
26
 
26
27
  if defined?(::RailsAdmin)
27
28
  require 'enumerize/integrations/rails_admin'
@@ -49,7 +50,10 @@ module Enumerize
49
50
  rescue LoadError
50
51
  end
51
52
 
52
- if defined?(::RSpec)
53
+ begin
54
+ require 'rspec/matchers'
55
+ rescue LoadError
56
+ else
53
57
  require 'enumerize/integrations/rspec'
54
58
  end
55
59
  end
@@ -21,6 +21,7 @@ ActiveRecord::Base.connection.instance_eval do
21
21
 
22
22
  create_table :documents do |t|
23
23
  t.string :visibility
24
+ t.timestamps
24
25
  end
25
26
  end
26
27
 
@@ -54,7 +55,11 @@ class User < ActiveRecord::Base
54
55
  enumerize :account_type, :in => [:basic, :premium]
55
56
  end
56
57
 
57
- describe Enumerize::ActiveRecord do
58
+ class UniqStatusUser < User
59
+ validates :status, uniqueness: true
60
+ end
61
+
62
+ describe Enumerize::ActiveRecordSupport do
58
63
  it 'sets nil if invalid value is passed' do
59
64
  user = User.new
60
65
  user.sex = :invalid
@@ -84,6 +89,12 @@ describe Enumerize::ActiveRecord do
84
89
  User.new.attributes['role'].must_equal 'user'
85
90
  end
86
91
 
92
+ it 'does not set default value for not selected attributes' do
93
+ User.delete_all
94
+ User.create!(:sex => :male)
95
+ User.select(:id).collect(&:id)
96
+ end
97
+
87
98
  it 'has default value with lambda' do
88
99
  User.new.lambda_role.must_equal 'admin'
89
100
  User.new.attributes['lambda_role'].must_equal 'admin'
@@ -189,6 +200,8 @@ describe Enumerize::ActiveRecord do
189
200
  end
190
201
 
191
202
  it 'adds scope' do
203
+ User.delete_all
204
+
192
205
  user_1 = User.create!(status: :active, role: :admin)
193
206
  user_2 = User.create!(status: :blocked)
194
207
 
@@ -217,15 +230,68 @@ describe Enumerize::ActiveRecord do
217
230
  end
218
231
 
219
232
  it 'supports defining enumerized attributes on abstract class' do
233
+ Document.delete_all
234
+
220
235
  document = Document.new
221
236
  document.visibility = :protected
222
237
  document.visibility.must_equal 'protected'
223
238
  end
224
239
 
225
240
  it 'supports defining enumerized scopes on abstract class' do
241
+ Document.delete_all
242
+
226
243
  document_1 = Document.create!(visibility: :public)
227
244
  document_2 = Document.create!(visibility: :private)
228
245
 
229
246
  Document.with_visibility(:public).must_equal [document_1]
230
247
  end
248
+
249
+ it 'validates uniqueness' do
250
+ user = User.new
251
+ user.status = :active
252
+ user.save!
253
+
254
+ user = UniqStatusUser.new
255
+ user.status = :active
256
+ user.valid?
257
+
258
+ user.errors[:status].wont_be :empty?
259
+ end
260
+
261
+ it 'supports multiple attributes in #becomes' do
262
+ User.delete_all
263
+
264
+ uniq_user = UniqStatusUser.new
265
+ uniq_user.interests = [:sports, :dancing]
266
+ uniq_user.sex = :male
267
+ uniq_user.save!
268
+
269
+ user = uniq_user.becomes(User)
270
+
271
+ user.sex.must_equal uniq_user.sex
272
+ user.interests.must_equal uniq_user.interests
273
+ end
274
+
275
+ it "doesn't update record" do
276
+ Document.delete_all
277
+
278
+ expected = Time.utc(2010, 10, 10)
279
+
280
+ document = Document.new
281
+ document.updated_at = expected
282
+ document.save!
283
+
284
+ document = Document.last
285
+ document.save!
286
+
287
+ assert_equal expected, document.updated_at
288
+ end
289
+
290
+ it 'changes from dirty should be serialized as scalar values' do
291
+ user = User.create(:status => :active)
292
+ user.status = :blocked
293
+
294
+ expected = ActiveSupport::HashWithIndifferentAccess.new(status: [1, 2]).to_yaml
295
+ assert_equal expected, user.changes.to_yaml
296
+ end
231
297
  end
@@ -19,6 +19,17 @@ describe Enumerize::Attribute do
19
19
  attr.name.must_equal :foo
20
20
  end
21
21
 
22
+ describe 'i18n scopes' do
23
+ it 'returns scopes from options' do
24
+ build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
25
+ attr.i18n_scopes.must_equal %w[bar buzz]
26
+ end
27
+
28
+ it 'accepts only string scopes' do
29
+ proc { build_attr nil, 'foo', :in => %w[a b], :i18n_scope => [%w[bar buzz], "bar.buzz"] }.must_raise ArgumentError
30
+ end
31
+ end
32
+
22
33
  describe 'options for select' do
23
34
  it 'returns all options for select' do
24
35
  store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
@@ -67,4 +78,35 @@ describe Enumerize::Attribute do
67
78
  attr.find_value(2).must_equal 'b'
68
79
  end
69
80
  end
81
+
82
+ describe 'values hash with zero' do
83
+ before do
84
+ build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
85
+ end
86
+
87
+ it 'returns hash keys as values' do
88
+ attr.values.must_equal %w[a b c]
89
+ end
90
+
91
+ it 'finds values by hash values' do
92
+ attr.find_value(1).must_equal 'a'
93
+ attr.find_value(2).must_equal 'b'
94
+ attr.find_value(0).must_equal 'c'
95
+ end
96
+ end
97
+
98
+ describe 'boolean values hash' do
99
+ before do
100
+ build_attr nil, :foo, :in => {:a => true, :b => false}
101
+ end
102
+
103
+ it 'returns hash keys as values' do
104
+ attr.values.must_equal %w[a b]
105
+ end
106
+
107
+ it 'finds values by hash values' do
108
+ attr.find_value(true).must_equal 'a'
109
+ attr.find_value(false).must_equal 'b'
110
+ end
111
+ end
70
112
  end
data/test/mongoid_test.rb CHANGED
@@ -19,6 +19,7 @@ describe Enumerize do
19
19
  field :role
20
20
  enumerize :sex, :in => %w[male female]
21
21
  enumerize :role, :in => %w[admin user], :default => 'user'
22
+ enumerize :mult, :in => %w[one two three four], :multiple => true
22
23
  end
23
24
 
24
25
  before { $VERBOSE = nil }
@@ -67,4 +68,12 @@ describe Enumerize do
67
68
  user.sex = :female
68
69
  user.sex.must_equal 'female'
69
70
  end
71
+
72
+ it 'loads multiple properly' do
73
+ model.delete_all
74
+
75
+ model.create!(:mult => ['one', 'two'])
76
+ user = model.first
77
+ user.mult.to_a.must_equal ['one', 'two']
78
+ end
70
79
  end
@@ -15,4 +15,11 @@ class RailsAdminSpec < MiniTest::Spec
15
15
  object.foo_enum.must_equal [['a text', 'a'], ['b text', 'b']]
16
16
  end
17
17
  end
18
+
19
+ it 'defines enum properly for custom values enumerations' do
20
+ store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
21
+ klass.enumerize(:foo, in: {:a => 1, :b => 2})
22
+ object.foo_enum.must_equal [['a text', 1], ['b text', 2]]
23
+ end
24
+ end
18
25
  end
@@ -15,6 +15,22 @@ describe Enumerize::Integrations::RSpec do
15
15
  let(:should) { Should.new }
16
16
  let(:object) { klass.new }
17
17
 
18
+ describe '#description' do
19
+ before do
20
+ klass.enumerize(:sex, :in => [:male, :female])
21
+ end
22
+
23
+ it 'returns description without default value' do
24
+ matcher = should.enumerize(:sex).in(:male, :female)
25
+ matcher.description.must_equal 'enumerize :sex in: "female", "male"'
26
+ end
27
+
28
+ it 'returns description with default value' do
29
+ matcher = should.enumerize(:sex).in(:male, :female).with_default(:male)
30
+ matcher.description.must_equal 'enumerize :sex in: "female", "male" with "male" as default value'
31
+ end
32
+ end
33
+
18
34
  describe '#matches?' do
19
35
  before do
20
36
  klass.enumerize(:sex, :in => [:male, :female])
@@ -49,5 +65,12 @@ describe Enumerize::Integrations::RSpec do
49
65
  expected = ' expected :sex to have "foo" as default value, but it sets "male" instead'
50
66
  matcher.failure_message.must_equal expected
51
67
  end
68
+
69
+ it 'returns failure message for ivalid :in option with default value' do
70
+ matcher = should.enumerize(:sex).in(:bar).with_default(:male)
71
+ matcher.subject = object
72
+ expected = ' expected :sex to allow value: "bar", but it allows "female", "male" instead'
73
+ matcher.failure_message.must_equal expected
74
+ end
52
75
  end
53
76
  end
@@ -44,6 +44,15 @@ class SimpleFormSpec < MiniTest::Spec
44
44
  let(:user) { User.new }
45
45
  let(:post) { Post.new }
46
46
 
47
+ it 'renders select with enumerized values using input_field' do
48
+ concat(simple_form_for(user) do |f|
49
+ f.input_field(:sex)
50
+ end)
51
+
52
+ assert_select 'select option[value=male]'
53
+ assert_select 'select option[value=female]'
54
+ end
55
+
47
56
  it 'renders select with enumerized values' do
48
57
  concat(simple_form_for(user) do |f|
49
58
  f.input(:sex)
data/test/value_test.rb CHANGED
@@ -13,7 +13,7 @@ describe Enumerize::Value do
13
13
  end
14
14
 
15
15
  describe 'translation' do
16
- let(:attr) { Struct.new(:values, :name, :i18n_suffix).new([], "attribute_name", "model_name") }
16
+ let(:attr) { Struct.new(:values, :name, :i18n_scopes).new([], "attribute_name", []) }
17
17
 
18
18
  it 'uses common translation' do
19
19
  store_translations(:en, :enumerize => {:attribute_name => {:test_value => "Common translation"}}) do
@@ -22,12 +22,16 @@ describe Enumerize::Value do
22
22
  end
23
23
 
24
24
  it 'uses model specific translation' do
25
+ attr.i18n_scopes = ["enumerize.model_name.attribute_name"]
26
+
25
27
  store_translations(:en, :enumerize => {:model_name => {:attribute_name => {:test_value => "Model Specific translation"}}}) do
26
28
  value.text.must_be :==, "Model Specific translation"
27
29
  end
28
30
  end
29
31
 
30
32
  it 'uses model specific translation rather than common translation' do
33
+ attr.i18n_scopes = ["enumerize.model_name.attribute_name"]
34
+
31
35
  store_translations(:en, :enumerize => {:attribute_name => {:test_value => "Common translation"}, :model_name => {:attribute_name => {:test_value => "Model Specific translation"}}}) do
32
36
  value.text.must_be :==, "Model Specific translation"
33
37
  end
@@ -38,6 +42,22 @@ describe Enumerize::Value do
38
42
  value.text.must_be :==, "Test value"
39
43
  end
40
44
  end
45
+
46
+ it 'uses specified in options translation scope' do
47
+ attr.i18n_scopes = ["other.scope"]
48
+
49
+ store_translations(:en, :other => {:scope => {:test_value => "Scope specific translation"}}) do
50
+ value.text.must_be :==, "Scope specific translation"
51
+ end
52
+ end
53
+
54
+ it 'uses first found translation scope from options' do
55
+ attr.i18n_scopes = ["nonexistent.scope", "other.scope"]
56
+
57
+ store_translations(:en, :other => {:scope => {:test_value => "Scope specific translation"}}) do
58
+ value.text.must_be :==, "Scope specific translation"
59
+ end
60
+ end
41
61
  end
42
62
 
43
63
  describe 'boolean methods comparison' do
@@ -78,4 +98,10 @@ describe Enumerize::Value do
78
98
  value.wont_respond_to :some_method?
79
99
  end
80
100
  end
101
+
102
+ describe 'serialization' do
103
+ it 'should be serialized to yaml as string value' do
104
+ assert_equal YAML.dump('test_value'), YAML.dump(value)
105
+ end
106
+ end
81
107
  end
metadata CHANGED
@@ -1,32 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
5
- prerelease:
4
+ version: 0.8.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sergey Nartimov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-21 00:00:00.000000000 Z
11
+ date: 2014-03-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.2'
20
- none: false
21
20
  type: :runtime
21
+ prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.2'
27
- none: false
28
- prerelease: false
29
- name: activesupport
30
27
  description: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper
31
28
  support
32
29
  email: team@brainspec.com
@@ -34,8 +31,8 @@ executables: []
34
31
  extensions: []
35
32
  extra_rdoc_files: []
36
33
  files:
37
- - .gitignore
38
- - .travis.yml
34
+ - ".gitignore"
35
+ - ".travis.yml"
39
36
  - CHANGELOG.md
40
37
  - Gemfile
41
38
  - Gemfile.rails4
@@ -51,6 +48,7 @@ files:
51
48
  - lib/enumerize/form_helper.rb
52
49
  - lib/enumerize/hooks/formtastic.rb
53
50
  - lib/enumerize/hooks/simple_form.rb
51
+ - lib/enumerize/hooks/uniqueness.rb
54
52
  - lib/enumerize/integrations/rails_admin.rb
55
53
  - lib/enumerize/integrations/rspec.rb
56
54
  - lib/enumerize/integrations/rspec/matcher.rb
@@ -82,29 +80,25 @@ files:
82
80
  - test/value_test.rb
83
81
  homepage: https://github.com/brainspec/enumerize
84
82
  licenses: []
83
+ metadata: {}
85
84
  post_install_message:
86
85
  rdoc_options: []
87
86
  require_paths:
88
87
  - lib
89
88
  required_ruby_version: !ruby/object:Gem::Requirement
90
89
  requirements:
91
- - - ! '>='
90
+ - - ">="
92
91
  - !ruby/object:Gem::Version
93
92
  version: 1.9.3
94
- none: false
95
93
  required_rubygems_version: !ruby/object:Gem::Requirement
96
94
  requirements:
97
- - - ! '>='
95
+ - - ">="
98
96
  - !ruby/object:Gem::Version
99
- hash: 2859816307703102675
100
97
  version: '0'
101
- segments:
102
- - 0
103
- none: false
104
98
  requirements: []
105
99
  rubyforge_project:
106
- rubygems_version: 1.8.23
100
+ rubygems_version: 2.2.0
107
101
  signing_key:
108
- specification_version: 3
102
+ specification_version: 4
109
103
  summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
110
104
  test_files: []