enumerize 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f5a43e5546fd78ed42c35abfccc3314e52b7671f
4
- data.tar.gz: 727be0571656833f27ee73ff8d65d6455c7511e4
2
+ SHA256:
3
+ metadata.gz: 05a89bbe16f328c6b8dc7f741291a9579af4773ce619cefec6009ec53e6e9ebe
4
+ data.tar.gz: 774b68ae715671f57f5cf84b11527bd58a6f31f3fc0bed3b370467550f05d72c
5
5
  SHA512:
6
- metadata.gz: 88cfa3f67475bf8d18279b7f53124051237c3a5bfe302e88860f5c405834cd95ebe9cfca67eb0d721c52ca746a15458cf284d2a376b049ede0b6b69379f667a6
7
- data.tar.gz: f57a1bca5a058f2074d834968418865455f4e0ccf3aa2441064f05cb5901a4eb8ec4776cd2e95c938635a41659723679108d873caa338e8487879e91fbcafb25
6
+ metadata.gz: 20ffa27d5b0cf7364b3ee93ac9c9b1e769cc3c3fa0108925716eecc7c0702242ab420a0916e235be9fae4588c964fe5456b6571afe254a9de2dad5ecff24b168
7
+ data.tar.gz: 1eeb005d9dbaedd1bb28e4197c6eea6f95e201f20fbd0748d464f7aa45a0139428d1f2e037b9bf01f0192a13ad10125f8fa0e098a0d1f79b02298981e183c28e
@@ -6,13 +6,14 @@ services:
6
6
  gemfile:
7
7
  - Gemfile
8
8
  - Gemfile.rails42
9
- - Gemfile.rails51
9
+ - Gemfile.rails50
10
+ - Gemfile.rails52
10
11
  - Gemfile.mongo_mapper
11
12
  rvm:
12
13
  - 2.2.7
13
14
  - 2.3.3
14
15
  - 2.4.1
15
- - jruby-9.1.8.0
16
+ - jruby-9.1.14.0
16
17
  env:
17
18
  global:
18
19
  - DB_USER=postgres
@@ -23,7 +24,7 @@ env:
23
24
  matrix:
24
25
  fast_finish: true
25
26
  allow_failures:
26
- - rvm: jruby-9.1.8.0
27
+ - rvm: jruby-9.1.14.0
27
28
  exclude:
28
29
  - gemfile: Gemfile.mongo_mapper
29
30
  env: DB=postgresql
@@ -4,6 +4,20 @@
4
4
 
5
5
  ### bug fix
6
6
 
7
+ ## 2.2.0 (February 13, 2018)
8
+
9
+ ### enhancements
10
+
11
+ * Add integration with active_interaction. (by [@runephilosof](https://github.com/runephilosof))
12
+ * Allow using `plugin :enumerize` with Sequel. (by [@jnylen](https://github.com/jnylen))
13
+ * Support ActiveModel::Attributes from Rails 5.2. (by [@troter](https://github.com/troter))
14
+ * Support Sequel 5.2.0. (by [@troter](https://github.com/troter))
15
+
16
+ ### bug fix
17
+
18
+ * Fix RailsAdmin integration when enumerated field used on edit form and enumerated value wasn't set. (by [@nashby](https://github.com/nashby))
19
+ * Fallback to a raw passed value instead of nil if AR type can't find value in the attribute. (by [@nashby](https://github.com/nashby))
20
+
7
21
  ## 2.1.2 (May 18, 2017)
8
22
 
9
23
  ### bug fix
@@ -41,11 +55,11 @@
41
55
  * Drop support for Rails 4.0 and 4.1. Support only Rails 4.2 and newer. (by [@lest](https://github.com/lest))
42
56
  * Support Rails 5.0. (by [@nashby](https://github.com/nashby) and [@lest](https://github.com/lest))
43
57
  * Allow to pass enumerize values to `ActiveRecord#update_all` (by [@DmitryTsepelev](https://github.com/DmitryTsepelev) and [@ianwhite](https://github.com/ianwhite))
44
-
58
+
45
59
  ```ruby
46
60
  User.update_all(status: :blocked)
47
61
  ```
48
-
62
+
49
63
  ### bug fix
50
64
 
51
65
  * Rescue MissingAttributeError on attribute writing. (by [@embs](https://github.com/embs))
data/Gemfile CHANGED
@@ -1,7 +1,7 @@
1
1
  eval_gemfile('Gemfile.global')
2
2
 
3
3
  gem 'minitest', '~> 5.8'
4
- gem 'rails', '5.0.2', require: false
5
- gem 'activerecord-jdbcsqlite3-adapter', github: 'jruby/activerecord-jdbc-adapter', branch: 'rails-5', platform: :jruby
6
- gem 'activerecord-jdbcpostgresql-adapter', github: 'jruby/activerecord-jdbc-adapter', branch: 'rails-5', platform: :jruby
4
+ gem 'rails', '5.1.4', require: false
5
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 51.0', platform: :jruby
6
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0', platform: :jruby
7
7
  gem 'mongoid'
@@ -6,7 +6,7 @@ gem 'rake'
6
6
  gem 'rspec', :require => false
7
7
 
8
8
  gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
9
- gem 'pg', :platform => [:ruby, :mswin, :mingw]
9
+ gem 'pg', '~> 0.21.0', :platform => [:ruby, :mswin, :mingw]
10
10
  gem 'sequel'
11
11
 
12
12
  platforms :rbx do
@@ -2,6 +2,6 @@ eval_gemfile('Gemfile.global')
2
2
 
3
3
  gem 'minitest', '~> 5.8'
4
4
  gem 'rails', '4.2.8', :require => false
5
- gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
6
- gem 'activerecord-jdbcpostgresql-adapter', platform: :jruby
5
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3.0', platform: :jruby
6
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3.0', platform: :jruby
7
7
  gem 'mongo_mapper'
@@ -2,6 +2,6 @@ eval_gemfile('Gemfile.global')
2
2
 
3
3
  gem 'minitest', '~> 5.8'
4
4
  gem 'rails', '4.2.8', :require => false
5
- gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
6
- gem 'activerecord-jdbcpostgresql-adapter', platform: :jruby
5
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3.0', platform: :jruby
6
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 1.3.0', platform: :jruby
7
7
  gem 'mongoid', '~> 5.0'
@@ -0,0 +1,7 @@
1
+ eval_gemfile('Gemfile.global')
2
+
3
+ gem 'minitest', '~> 5.8'
4
+ gem 'rails', '5.0.6', require: false
5
+ gem 'activerecord-jdbcsqlite3-adapter', '~> 51.0', platform: :jruby
6
+ gem 'activerecord-jdbcpostgresql-adapter', '~> 51.0', platform: :jruby
7
+ gem 'mongoid'
@@ -1,7 +1,7 @@
1
1
  eval_gemfile('Gemfile.global')
2
2
 
3
3
  gem 'minitest', '~> 5.8'
4
- gem 'rails', '5.1.0.rc1', require: false
5
- gem 'activerecord-jdbcsqlite3-adapter', github: 'jruby/activerecord-jdbc-adapter', branch: 'rails-5', platform: :jruby
6
- gem 'activerecord-jdbcpostgresql-adapter', github: 'jruby/activerecord-jdbc-adapter', branch: 'rails-5', platform: :jruby
4
+ gem 'rails', '5.2.0.rc1', require: false
5
+ gem 'activerecord-jdbcsqlite3-adapter', github: 'jruby/activerecord-jdbc-adapter', branch: 'master', platform: :jruby
6
+ gem 'activerecord-jdbcpostgresql-adapter', github: 'jruby/activerecord-jdbc-adapter', branch: 'master', platform: :jruby
7
7
  gem 'mongoid', github: 'mongodb/mongoid'
data/README.md CHANGED
@@ -16,6 +16,10 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install enumerize
18
18
 
19
+ ## Supported Versions
20
+ - Ruby 2.2+
21
+ - Rails 4.2+
22
+
19
23
  ## Usage
20
24
 
21
25
  Basic:
@@ -315,6 +319,8 @@ and if you want it as radio buttons:
315
319
  <% end %>
316
320
  ```
317
321
 
322
+ Please note that Enumerize overwrites the I18n keys of SimpleForm collections. The enumerized keys are used instead of the SimpleForm ones for inputs concerning enumerized attributes. If you don't want this just pass `:collection` option to the `input` call.
323
+
318
324
  ### Formtastic
319
325
 
320
326
  If you are using Formtastic gem you also don't need to specify input type (`:select` by default) and collection:
@@ -12,6 +12,7 @@ module Enumerize
12
12
  autoload :Predicatable, 'enumerize/predicatable'
13
13
  autoload :ModuleAttributes, 'enumerize/module_attributes'
14
14
 
15
+ autoload :ActiveModelAttributesSupport, 'enumerize/activemodel'
15
16
  autoload :ActiveRecordSupport, 'enumerize/activerecord'
16
17
  autoload :SequelSupport, 'enumerize/sequel'
17
18
  autoload :MongoidSupport, 'enumerize/mongoid'
@@ -31,6 +32,10 @@ module Enumerize
31
32
  base.send :include, Enumerize::Base
32
33
  base.extend Enumerize::Predicates
33
34
 
35
+ if defined?(::ActiveModel::Attributes)
36
+ base.extend Enumerize::ActiveModelAttributesSupport
37
+ end
38
+
34
39
  if defined?(::ActiveRecord::Base)
35
40
  base.extend Enumerize::ActiveRecordSupport
36
41
  base.extend Enumerize::Scope::ActiveRecord
@@ -0,0 +1,45 @@
1
+ module Enumerize
2
+ module ActiveModelAttributesSupport
3
+ def enumerize(name, options={})
4
+ super
5
+
6
+ _enumerize_module.dependent_eval do
7
+ if self.included_modules.include? ::ActiveModel::Attributes
8
+ include InstanceMethods
9
+
10
+ attribute name, Enumerize::ActiveModelAttributesSupport::Type.new(enumerized_attributes[name])
11
+ end
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ # https://github.com/brainspec/enumerize/issues/74
17
+ def write_attribute(attr_name, value, *options)
18
+ if self.class.enumerized_attributes[attr_name]
19
+ _enumerized_values_for_validation[attr_name.to_s] = value
20
+ end
21
+
22
+ super
23
+ end
24
+ end
25
+
26
+ class Type < ActiveModel::Type::Value
27
+ def type
28
+ :enumerize
29
+ end
30
+
31
+ def initialize(attr)
32
+ @attr = attr
33
+ end
34
+
35
+ def serialize(value)
36
+ v = @attr.find_value(value)
37
+ v && v.value
38
+ end
39
+
40
+ def deserialize(value)
41
+ @attr.find_value(value)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -76,7 +76,7 @@ module Enumerize
76
76
 
77
77
  def serialize(value)
78
78
  v = @attr.find_value(value)
79
- v && v.value
79
+ (v && v.value) || value
80
80
  end
81
81
 
82
82
  alias type_cast_for_database serialize
@@ -5,7 +5,7 @@ module Enumerize
5
5
  module FormtasticFormBuilderExtension
6
6
 
7
7
  def input(method, options={})
8
- klass = object.class
8
+ klass = object.to_model.class
9
9
 
10
10
  if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[method])
11
11
  options[:collection] ||= attr.options
@@ -17,7 +17,7 @@ module Enumerize
17
17
  private
18
18
 
19
19
  def add_input_options_for_enumerized_attribute(attribute_name, options)
20
- klass = object.class
20
+ klass = object.to_model.class
21
21
 
22
22
  if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[attribute_name])
23
23
  options[:collection] ||= attr.options
@@ -5,7 +5,9 @@ module Enumerize
5
5
  module UniquenessValidator
6
6
 
7
7
  def validate_each(record, name, value)
8
- if record.class.respond_to?(:enumerized_attributes) && (attr = record.class.enumerized_attributes[name])
8
+ klass = record.to_model.class
9
+
10
+ if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[name])
9
11
  value = attr.find_value(value).try(:value)
10
12
  end
11
13
 
@@ -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}].values.map{|v| [v.text, v.value]}
10
+ self.class.enumerized_attributes[:#{name}].options
11
11
  end
12
12
  RUBY
13
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumerize
4
- VERSION = '2.1.2'
4
+ VERSION = '2.2.0'
5
5
  end
@@ -0,0 +1,16 @@
1
+ module Sequel
2
+ module Plugins
3
+ module Enumerize
4
+ # Depend on the def_dataset_method plugin
5
+ def self.apply(model)
6
+ model.plugin(:def_dataset_method) unless model.respond_to?(:def_dataset_method)
7
+ end
8
+
9
+ module InstanceMethods
10
+ def self.included(base)
11
+ base.extend ::Enumerize
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,112 @@
1
+ require 'test_helper'
2
+
3
+ if defined?(::ActiveModel::Attributes)
4
+
5
+ describe Enumerize do
6
+ class ActiveModelUser
7
+ include ActiveModel::Model
8
+ include ActiveModel::Attributes
9
+ extend Enumerize
10
+
11
+ attribute :name, :string
12
+ enumerize :sex, :in => %w[male female]
13
+ enumerize :role, :in => %w[admin user], :default => 'user'
14
+ enumerize :interests, :in => [:music, :sports, :dancing, :programming], :multiple => true
15
+ end
16
+
17
+ class InterestsRequiredActiveModelUser < ActiveModelUser
18
+ validates :interests, presence: true
19
+ end
20
+
21
+ let(:model) { ActiveModelUser }
22
+
23
+ it 'initialize value' do
24
+ user = model.new(:name => 'active_model_user', :sex => :male, :role => :user, :interests => [:music, :programming])
25
+ user.sex.must_equal 'male'
26
+ user.sex_text.must_equal 'Male'
27
+ user.role.must_equal 'user'
28
+ user.role_text.must_equal 'User'
29
+ user.interests.must_equal %w(music programming)
30
+ end
31
+
32
+ it 'sets nil if invalid value is passed' do
33
+ user = model.new
34
+ user.sex = :invalid
35
+ user.sex.must_be_nil
36
+ end
37
+
38
+ it 'stores value' do
39
+ user = model.new
40
+ user.sex = :female
41
+ user.sex.must_equal 'female'
42
+ end
43
+
44
+ it 'has default value' do
45
+ model.new.role.must_equal 'user'
46
+ end
47
+
48
+ it 'validates inclusion' do
49
+ user = model.new
50
+ user.role = 'wrong'
51
+ user.wont_be :valid?
52
+ end
53
+
54
+ it 'supports multiple attributes' do
55
+ user = ActiveModelUser.new
56
+ user.interests.must_be_instance_of Enumerize::Set
57
+ user.interests.must_be_empty
58
+ user.interests << :music
59
+ user.interests.must_equal %w(music)
60
+ user.interests << :sports
61
+ user.interests.must_equal %w(music sports)
62
+
63
+ user.interests = []
64
+ interests = user.interests
65
+ interests << :music
66
+ interests.must_equal %w(music)
67
+ interests << :dancing
68
+ interests.must_equal %w(music dancing)
69
+ end
70
+
71
+ it 'returns invalid multiple value for validation' do
72
+ user = ActiveModelUser.new
73
+ user.interests << :music
74
+ user.interests << :invalid
75
+ values = user.read_attribute_for_validation(:interests)
76
+ values.must_equal %w(music invalid)
77
+ end
78
+
79
+ it 'validates multiple attributes' do
80
+ user = ActiveModelUser.new
81
+ user.interests << :invalid
82
+ user.wont_be :valid?
83
+
84
+ user.interests = Object.new
85
+ user.wont_be :valid?
86
+
87
+ user.interests = ['music', '']
88
+ user.must_be :valid?
89
+ end
90
+
91
+ it 'validates presence with multiple attributes' do
92
+ user = InterestsRequiredActiveModelUser.new
93
+ user.interests = []
94
+ user.valid?
95
+
96
+ user.errors[:interests].wont_be :empty?
97
+
98
+ user.interests = ['']
99
+ user.valid?
100
+
101
+ user.errors[:interests].wont_be :empty?
102
+
103
+ user.interests = [:dancing, :programming]
104
+ user.valid?
105
+
106
+ user.errors[:interests].must_be_empty
107
+ end
108
+ end
109
+
110
+ else
111
+ # Skip
112
+ end
@@ -522,4 +522,12 @@ describe Enumerize::ActiveRecordSupport do
522
522
  user = YAML.load(User.create(status: :blocked).to_yaml)
523
523
  user.status.must_equal 'blocked'
524
524
  end
525
+
526
+ # https://github.com/brainspec/enumerize/issues/304
527
+ it "fallbacks to a raw passed value if AR type can't find value in the attribute" do
528
+ table = User.arel_table
529
+ sql = User.where(table[:account_type].matches '%foo%').to_sql
530
+
531
+ sql.must_include 'LIKE \'%foo%\''
532
+ end
525
533
  end
@@ -1,32 +1,32 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Enumerize::Base do
4
- let(:klass) do
4
+ let(:kklass) do
5
5
  Class.new do
6
6
  extend Enumerize
7
7
  end
8
8
  end
9
9
 
10
10
  let(:subklass) do
11
- Class.new(klass)
11
+ Class.new(kklass)
12
12
  end
13
13
 
14
- let(:object) { klass.new }
14
+ let(:object) { kklass.new }
15
15
 
16
16
  it 'returns nil when not set' do
17
- klass.enumerize(:foo, :in => [:a, :b])
17
+ kklass.enumerize(:foo, :in => [:a, :b])
18
18
  object.foo.must_be_nil
19
19
  end
20
20
 
21
21
  it 'returns value that was set' do
22
- klass.enumerize(:foo, :in => [:a, :b])
22
+ kklass.enumerize(:foo, :in => [:a, :b])
23
23
  object.foo = :a
24
24
  object.foo.must_equal 'a'
25
25
  end
26
26
 
27
27
  it 'returns translation' do
28
28
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
29
- klass.enumerize(:foo, :in => [:a, :b])
29
+ kklass.enumerize(:foo, :in => [:a, :b])
30
30
  object.foo = :a
31
31
  object.foo.text.must_equal 'a text'
32
32
  object.foo_text.must_equal 'a text'
@@ -36,13 +36,13 @@ describe Enumerize::Base do
36
36
 
37
37
  it 'returns nil as translation when value is nil' do
38
38
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
39
- klass.enumerize(:foo, :in => [:a, :b])
39
+ kklass.enumerize(:foo, :in => [:a, :b])
40
40
  object.foo_text.must_be_nil
41
41
  end
42
42
  end
43
43
 
44
44
  it 'scopes translation by i18n key' do
45
- def klass.model_name
45
+ def kklass.model_name
46
46
  name = "ExampleClass"
47
47
  def name.i18n_key
48
48
  'example_class'
@@ -52,7 +52,7 @@ describe Enumerize::Base do
52
52
  end
53
53
 
54
54
  store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
55
- klass.enumerize(:foo, :in => [:a, :b])
55
+ kklass.enumerize(:foo, :in => [:a, :b])
56
56
  object.foo = :a
57
57
  object.foo.text.must_equal 'a text scoped'
58
58
  object.foo_text.must_equal 'a text scoped'
@@ -61,88 +61,88 @@ describe Enumerize::Base do
61
61
 
62
62
  it 'returns humanized value if there are no translations' do
63
63
  store_translations(:en, :enumerize => {}) do
64
- klass.enumerize(:foo, :in => [:a, :b])
64
+ kklass.enumerize(:foo, :in => [:a, :b])
65
65
  object.foo = :a
66
66
  object.foo_text.must_equal 'A'
67
67
  end
68
68
  end
69
69
 
70
70
  it 'stores value as string' do
71
- klass.enumerize(:foo, :in => [:a, :b])
71
+ kklass.enumerize(:foo, :in => [:a, :b])
72
72
  object.foo = :a
73
73
  object.instance_variable_get(:@foo).must_be_instance_of String
74
74
  end
75
75
 
76
76
  it 'handles default value' do
77
- klass.enumerize(:foo, :in => [:a, :b], :default => :b)
77
+ kklass.enumerize(:foo, :in => [:a, :b], :default => :b)
78
78
  object.foo.must_equal 'b'
79
79
  end
80
80
 
81
81
  it 'handles default value with lambda' do
82
- klass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
82
+ kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
83
83
  object.foo.must_equal 'b'
84
84
  end
85
85
 
86
86
  it 'injects object instance into lamda default value' do
87
- klass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? klass })
87
+ kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? kklass })
88
88
  object.foo.must_equal 'b'
89
89
  end
90
90
 
91
91
  it 'raises exception on invalid default value' do
92
92
  proc {
93
- klass.enumerize(:foo, :in => [:a, :b], :default => :c)
93
+ kklass.enumerize(:foo, :in => [:a, :b], :default => :c)
94
94
  }.must_raise ArgumentError
95
95
  end
96
96
 
97
97
  it 'has enumerized attributes' do
98
- klass.enumerized_attributes.must_be_empty
99
- klass.enumerize(:foo, :in => %w[a b])
100
- klass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
98
+ kklass.enumerized_attributes.must_be_empty
99
+ kklass.enumerize(:foo, :in => %w[a b])
100
+ kklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
101
101
  end
102
102
 
103
103
  it "doesn't override existing method" do
104
- method = klass.method(:name)
105
- klass.enumerize(:name, :in => %w[a b], :default => 'a')
106
- klass.method(:name).must_equal method
104
+ method = kklass.method(:name)
105
+ kklass.enumerize(:name, :in => %w[a b], :default => 'a')
106
+ kklass.method(:name).must_equal method
107
107
  end
108
108
 
109
109
  it "inherits enumerized attributes from a parent class" do
110
- klass.enumerize(:foo, :in => %w[a b])
110
+ kklass.enumerize(:foo, :in => %w[a b])
111
111
  subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
112
112
  end
113
113
 
114
114
  it "inherits enumerized attributes from a grandparent class" do
115
- klass.enumerize(:foo, :in => %w[a b])
115
+ kklass.enumerize(:foo, :in => %w[a b])
116
116
  Class.new(subklass).enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
117
117
  end
118
118
 
119
119
  it "doesn't add enumerized attributes to parent class" do
120
- klass.enumerize(:foo, :in => %w[a b])
120
+ kklass.enumerize(:foo, :in => %w[a b])
121
121
  subklass.enumerize(:bar, :in => %w[c d])
122
122
 
123
- klass.enumerized_attributes[:bar].must_be_nil
123
+ kklass.enumerized_attributes[:bar].must_be_nil
124
124
  end
125
125
 
126
126
  it 'adds new parent class attributes to subclass' do
127
- subklass = Class.new(klass)
128
- klass.enumerize :foo, :in => %w[a b]
127
+ subklass = Class.new(kklass)
128
+ kklass.enumerize :foo, :in => %w[a b]
129
129
  subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
130
130
  end
131
131
 
132
132
  it 'stores nil value' do
133
- klass.enumerize(:foo, :in => [:a, :b])
133
+ kklass.enumerize(:foo, :in => [:a, :b])
134
134
  object.foo = nil
135
135
  object.instance_variable_get(:@foo).must_be_nil
136
136
  end
137
137
 
138
138
  it 'casts value to string for validation' do
139
- klass.enumerize(:foo, :in => [:a, :b])
139
+ kklass.enumerize(:foo, :in => [:a, :b])
140
140
  object.foo = :c
141
141
  object.read_attribute_for_validation(:foo).must_equal 'c'
142
142
  end
143
143
 
144
144
  it "doesn't cast nil to string for validation" do
145
- klass.enumerize(:foo, :in => [:a, :b])
145
+ kklass.enumerize(:foo, :in => [:a, :b])
146
146
  object.foo = nil
147
147
  object.read_attribute_for_validation(:foo).must_be_nil
148
148
  end
@@ -179,7 +179,7 @@ describe Enumerize::Base do
179
179
  end
180
180
 
181
181
  it 'stores hash values' do
182
- klass.enumerize(:foo, :in => {:a => 1, :b => 2})
182
+ kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
183
183
 
184
184
  object.foo = :a
185
185
  object.instance_variable_get(:@foo).must_equal 1
@@ -191,7 +191,7 @@ describe Enumerize::Base do
191
191
  end
192
192
 
193
193
  it 'returns custom value' do
194
- klass.enumerize(:foo, :in => {:a => 1, :b => 2})
194
+ kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
195
195
 
196
196
  object.foo = :a
197
197
  object.foo_value.must_equal 1
@@ -1,57 +1,57 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Enumerize::Base do
4
- let(:klass) do
4
+ let(:kklass) do
5
5
  Class.new do
6
6
  extend Enumerize
7
7
  end
8
8
  end
9
9
 
10
10
  let(:subklass) do
11
- Class.new(klass)
11
+ Class.new(kklass)
12
12
  end
13
13
 
14
- let(:object) { klass.new }
14
+ let(:object) { kklass.new }
15
15
 
16
16
  it 'returns [] when not set' do
17
- klass.enumerize :foos, in: %w(a b), multiple: true
17
+ kklass.enumerize :foos, in: %w(a b), multiple: true
18
18
  object.foos.must_equal []
19
19
  end
20
20
 
21
21
  it 'returns setted array' do
22
- klass.enumerize :foos, in: %w(a b c), multiple: true
22
+ kklass.enumerize :foos, in: %w(a b c), multiple: true
23
23
  object.foos = %w(a c)
24
24
  object.foos.must_equal %w(a c)
25
25
  end
26
26
 
27
27
  it 'sets default value as single value' do
28
- klass.enumerize :foos, in: %w(a b c), default: 'b', multiple: true
28
+ kklass.enumerize :foos, in: %w(a b c), default: 'b', multiple: true
29
29
  object.foos.must_equal %w(b)
30
30
  end
31
31
 
32
32
  it 'sets default value as array of one element' do
33
- klass.enumerize :foos, in: %w(a b c), default: %w(b), multiple: true
33
+ kklass.enumerize :foos, in: %w(a b c), default: %w(b), multiple: true
34
34
  object.foos.must_equal %w(b)
35
35
  end
36
36
 
37
37
  it 'sets default value as array of several elements' do
38
- klass.enumerize :foos, in: %w(a b c), default: %w(b c), multiple: true
38
+ kklass.enumerize :foos, in: %w(a b c), default: %w(b c), multiple: true
39
39
  object.foos.must_equal %w(b c)
40
40
  end
41
41
 
42
42
  it "doesn't define _text method" do
43
- klass.enumerize :foos, in: %w(a b c), multiple: true
43
+ kklass.enumerize :foos, in: %w(a b c), multiple: true
44
44
  object.wont_respond_to :foos_text
45
45
  end
46
46
 
47
47
  it "doesn't define _value method" do
48
- klass.enumerize :foos, in: %w(a b c), multiple: true
48
+ kklass.enumerize :foos, in: %w(a b c), multiple: true
49
49
  object.wont_respond_to :foos_value
50
50
  end
51
51
 
52
52
  it "cannot define multiple with scope" do
53
53
  assert_raises ArgumentError do
54
- klass.enumerize :foos, in: %w(a b c), multiple: true, scope: true
54
+ kklass.enumerize :foos, in: %w(a b c), multiple: true, scope: true
55
55
  end
56
56
  end
57
57
  end
@@ -1,48 +1,48 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Enumerize::Predicates do
4
- let(:klass) do
4
+ let(:kklass) do
5
5
  Class.new do
6
6
  extend Enumerize
7
7
  end
8
8
  end
9
9
 
10
- let(:object) { klass.new }
10
+ let(:object) { kklass.new }
11
11
 
12
12
  it 'creates predicate methods' do
13
- klass.enumerize(:foo, in: %w(a b), predicates: true)
13
+ kklass.enumerize(:foo, in: %w(a b), predicates: true)
14
14
  object.must_respond_to :a?
15
15
  object.must_respond_to :b?
16
16
  end
17
17
 
18
18
  it 'creates predicate methods when enumerized values have dash in it' do
19
- klass.enumerize(:foo, in: %w(foo-bar bar-foo), predicates: true)
19
+ kklass.enumerize(:foo, in: %w(foo-bar bar-foo), predicates: true)
20
20
  object.must_respond_to :foo_bar?
21
21
  object.must_respond_to :bar_foo?
22
22
  end
23
23
 
24
24
  it 'creates predicate methods on multiple attribute' do
25
- klass.enumerize(:foo, in: %w(a b), predicates: true, multiple: true)
25
+ kklass.enumerize(:foo, in: %w(a b), predicates: true, multiple: true)
26
26
  object.must_respond_to :a?
27
27
  object.must_respond_to :b?
28
28
  end
29
29
 
30
30
  it 'checks values' do
31
- klass.enumerize(:foo, in: %w(a b), predicates: true)
31
+ kklass.enumerize(:foo, in: %w(a b), predicates: true)
32
32
  object.foo = 'a'
33
33
  object.a?.must_equal true
34
34
  object.b?.must_equal false
35
35
  end
36
36
 
37
37
  it 'checks values on multiple attribute' do
38
- klass.enumerize(:foo, in: %w(a b), predicates: true, multiple: true)
38
+ kklass.enumerize(:foo, in: %w(a b), predicates: true, multiple: true)
39
39
  object.foo << :a
40
40
  object.a?.must_equal true
41
41
  object.b?.must_equal false
42
42
  end
43
43
 
44
44
  it 'prefixes methods' do
45
- klass.enumerize(:foo, in: %w(a b), predicates: { prefix: 'bar' })
45
+ kklass.enumerize(:foo, in: %w(a b), predicates: { prefix: 'bar' })
46
46
  object.wont_respond_to :a?
47
47
  object.wont_respond_to :b?
48
48
  object.must_respond_to :bar_a?
@@ -50,13 +50,13 @@ describe Enumerize::Predicates do
50
50
  end
51
51
 
52
52
  it 'accepts only option' do
53
- klass.enumerize(:foo, in: %w(a b), predicates: { only: :a })
53
+ kklass.enumerize(:foo, in: %w(a b), predicates: { only: :a })
54
54
  object.must_respond_to :a?
55
55
  object.wont_respond_to :b?
56
56
  end
57
57
 
58
58
  it 'accepts except option' do
59
- klass.enumerize(:foo, in: %w(a b), predicates: { except: :a })
59
+ kklass.enumerize(:foo, in: %w(a b), predicates: { except: :a })
60
60
  object.wont_respond_to :a?
61
61
  object.must_respond_to :b?
62
62
  end
@@ -1,25 +1,25 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class RailsAdminSpec < MiniTest::Spec
4
- let(:klass) do
4
+ let(:kklass) do
5
5
  Class.new do
6
6
  extend Enumerize
7
7
  end
8
8
  end
9
9
 
10
- let(:object) { klass.new }
10
+ let(:object) { kklass.new }
11
11
 
12
12
  it 'defines enum method' do
13
13
  store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
14
- klass.enumerize(:foo, in: [:a, :b])
14
+ kklass.enumerize(:foo, in: [:a, :b])
15
15
  object.foo_enum.must_equal [['a text', 'a'], ['b text', 'b']]
16
16
  end
17
17
  end
18
18
 
19
19
  it 'defines enum properly for custom values enumerations' do
20
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]]
21
+ kklass.enumerize(:foo, in: {:a => 1, :b => 2})
22
+ object.foo_enum.must_equal [['a text', 'a'], ['b text', 'b']]
23
23
  end
24
24
  end
25
25
  end
@@ -32,7 +32,7 @@ module SequelTest
32
32
  end
33
33
 
34
34
  class Document < Sequel::Model
35
- extend Enumerize
35
+ plugin :enumerize
36
36
  enumerize :visibility, :in => [:public, :private, :protected], :scope => true, :default => :public
37
37
  end
38
38
 
@@ -47,7 +47,7 @@ module SequelTest
47
47
  plugin :dirty
48
48
  plugin :defaults_setter
49
49
  plugin :validation_helpers
50
- extend Enumerize
50
+ plugin :enumerize
51
51
  include RoleEnum
52
52
 
53
53
  enumerize :sex, :in => [:male, :female]
@@ -75,7 +75,7 @@ module SequelTest
75
75
  end
76
76
 
77
77
  it 'saves value' do
78
- User.filter.delete
78
+ User.filter{ true }.delete
79
79
  user = User.new
80
80
  user.sex = :female
81
81
  user.save
@@ -83,7 +83,7 @@ module SequelTest
83
83
  end
84
84
 
85
85
  it 'loads value' do
86
- User.filter.delete
86
+ User.filter{ true }.delete
87
87
  User.create(:sex => :male)
88
88
  store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
89
89
  user = User.first
@@ -98,7 +98,7 @@ module SequelTest
98
98
  end
99
99
 
100
100
  it 'does not set default value for not selected attributes' do
101
- User.filter.delete
101
+ User.filter{ true }.delete
102
102
  User.create(:sex => :male)
103
103
 
104
104
  assert_equal [:id], User.select(:id).first.values.keys
@@ -109,7 +109,7 @@ module SequelTest
109
109
  User.new.values[:lambda_role].must_equal 'admin'
110
110
  end
111
111
  it 'uses after_initialize callback to set default value' do
112
- User.filter.delete
112
+ User.filter{ true }.delete
113
113
  User.create(sex: 'male', lambda_role: nil)
114
114
 
115
115
  user = User.where(:sex => 'male').first
@@ -193,7 +193,7 @@ module SequelTest
193
193
  end
194
194
 
195
195
  it 'stores custom values for multiple attributes' do
196
- User.filter.delete
196
+ User.filter{ true }.delete
197
197
 
198
198
  klass = Class.new(User)
199
199
  klass.enumerize :interests, in: { music: 0, sports: 1, dancing: 2, programming: 3}, multiple: true
@@ -208,7 +208,7 @@ module SequelTest
208
208
  end
209
209
 
210
210
  it 'adds scope' do
211
- User.filter.delete
211
+ User.filter{ true }.delete
212
212
 
213
213
  user_1 = User.create(status: :active, role: :admin)
214
214
  user_2 = User.create(status: :blocked)
@@ -238,7 +238,7 @@ module SequelTest
238
238
  end
239
239
 
240
240
  it 'supports defining enumerized attributes on abstract class' do
241
- Document.filter.delete
241
+ Document.filter{ true }.delete
242
242
 
243
243
  document = Document.new
244
244
  document.visibility = :protected
@@ -246,7 +246,7 @@ module SequelTest
246
246
  end
247
247
 
248
248
  it 'supports defining enumerized scopes on abstract class' do
249
- Document.filter.delete
249
+ Document.filter{ true }.delete
250
250
 
251
251
  document_1 = Document.create(visibility: :public)
252
252
  document_2 = Document.create(visibility: :private)
@@ -266,7 +266,7 @@ module SequelTest
266
266
  end
267
267
 
268
268
  it "doesn't update record" do
269
- Document.filter.delete
269
+ Document.filter{ true }.delete
270
270
 
271
271
  expected = Time.new(2010, 10, 10)
272
272
 
@@ -2,17 +2,17 @@ require 'test_helper'
2
2
  require 'yaml'
3
3
 
4
4
  describe Enumerize::Set do
5
- let(:klass) do
5
+ let(:kklass) do
6
6
  Class.new do
7
7
  extend Enumerize
8
8
  enumerize :foo, :in => %w(a b c), :multiple => true
9
9
  end
10
10
  end
11
11
 
12
- let(:object) { klass.new }
12
+ let(:object) { kklass.new }
13
13
 
14
14
  def build_set(values)
15
- @set = Enumerize::Set.new(object, klass.foo, values)
15
+ @set = Enumerize::Set.new(object, kklass.foo, values)
16
16
  end
17
17
 
18
18
  def set
@@ -39,7 +39,7 @@ describe Enumerize::Set do
39
39
  end
40
40
 
41
41
  it 'equals to other set' do
42
- set.must_equal Enumerize::Set.new(nil, klass.foo, %w(a))
42
+ set.must_equal Enumerize::Set.new(nil, kklass.foo, %w(a))
43
43
  end
44
44
 
45
45
  it 'equals to array' do
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: 2.1.2
4
+ version: 2.2.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: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2018-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -39,12 +39,14 @@ files:
39
39
  - Gemfile.global
40
40
  - Gemfile.mongo_mapper
41
41
  - Gemfile.rails42
42
- - Gemfile.rails51
42
+ - Gemfile.rails50
43
+ - Gemfile.rails52
43
44
  - MIT-LICENSE
44
45
  - README.md
45
46
  - Rakefile
46
47
  - enumerize.gemspec
47
48
  - lib/enumerize.rb
49
+ - lib/enumerize/activemodel.rb
48
50
  - lib/enumerize/activerecord.rb
49
51
  - lib/enumerize/attribute.rb
50
52
  - lib/enumerize/attribute_map.rb
@@ -68,8 +70,10 @@ files:
68
70
  - lib/enumerize/set.rb
69
71
  - lib/enumerize/value.rb
70
72
  - lib/enumerize/version.rb
73
+ - lib/sequel/plugins/enumerize.rb
71
74
  - spec/enumerize/integrations/rspec/matcher_spec.rb
72
75
  - spec/spec_helper.rb
76
+ - test/activemodel_test.rb
73
77
  - test/activerecord_test.rb
74
78
  - test/attribute_map_test.rb
75
79
  - test/attribute_test.rb
@@ -108,13 +112,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
112
  version: '0'
109
113
  requirements: []
110
114
  rubyforge_project:
111
- rubygems_version: 2.6.11
115
+ rubygems_version: 2.7.3
112
116
  signing_key:
113
117
  specification_version: 4
114
118
  summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
115
119
  test_files:
116
120
  - spec/enumerize/integrations/rspec/matcher_spec.rb
117
121
  - spec/spec_helper.rb
122
+ - test/activemodel_test.rb
118
123
  - test/activerecord_test.rb
119
124
  - test/attribute_map_test.rb
120
125
  - test/attribute_test.rb