enumerize 2.6.1 → 2.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07d568d0eb005ebea8b0ded7cb260d6c7792b1d676bd46f8441c7e33445e366c
4
- data.tar.gz: b811acb983e8fac641fb2b9f9340089e8fd6149b1431af7f12446cb5203c39e1
3
+ metadata.gz: 66cd752f1d50a6396a45acd14ddc4f80c34adab4fdb858c48743f9f505b7adfc
4
+ data.tar.gz: 387784c9b8c8f5e7c2134900643ddd9465b1159c630ba6f0192bd8599f8deb68
5
5
  SHA512:
6
- metadata.gz: b68609dbbc3eede78b640395ef59bf149f1bf00d7b55f2b03a1397430f4e8816bd6a19bdfe06151e28fb1a64a9595fe48893810b015899b00d4991af6539f5d7
7
- data.tar.gz: 3999ab755693815c52779ca0124f6aec629a1dd639c41d33330211b0948f6f9d4aa3d79a3f20713bb99a508afa5b7e2b776a0465fe0c3c5a3c250043bbdfc0aa
6
+ metadata.gz: eddef8e043dd39c124b4a3ea946ec0c5a4b820f48e8fc43c46a8a37e8261fce9b3681aa4a2b52e69807d4bea78e3f8a70626728ce360d3d25f1baab996570f11
7
+ data.tar.gz: 681e5978eed5021f1c660c81a0574e7c1891285dca0b1468b65c27538b95b320df2ffc486892b241506bb01f867ad18c7f4ccd6d59acc3977e1581c453e86b24
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
@@ -23,7 +23,7 @@ jobs:
23
23
  - 5432:5432
24
24
  options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
25
25
  mysql:
26
- image: mysql:5.6
26
+ image: mysql:5.7
27
27
  env:
28
28
  MYSQL_ROOT_PASSWORD: mysql
29
29
  ports:
@@ -32,12 +32,12 @@ jobs:
32
32
  strategy:
33
33
  fail-fast: false
34
34
  matrix:
35
- ruby-version: ['2.7', '3.0', '3.1']
35
+ ruby-version: ['3.0', '3.1', '3.2', '3.3']
36
+ test: ['minitest', 'rspec']
36
37
  gemfile:
37
- - Gemfile
38
- - Gemfile.rails60
39
38
  - Gemfile.rails61
40
39
  - Gemfile.rails70
40
+ - Gemfile.rails71
41
41
  - Gemfile.railsmaster
42
42
  - Gemfile.mongo_mapper
43
43
  db:
@@ -47,19 +47,18 @@ jobs:
47
47
  exclude:
48
48
  - gemfile: Gemfile.mongo_mapper
49
49
  db: postgresql
50
- - ruby-version: '3.0'
51
- gemfile: Gemfile
52
- - ruby-version: '3.1'
53
- gemfile: Gemfile
54
50
  - ruby-version: '3.0'
55
51
  gemfile: Gemfile.mongo_mapper
56
52
  - ruby-version: '3.1'
57
53
  gemfile: Gemfile.mongo_mapper
54
+ - ruby-version: '3.0'
55
+ gemfile: Gemfile.railsmaster
58
56
  env:
59
57
  BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
60
58
  DB: "${{ matrix.db }}"
59
+ TEST_FRAMEWORK: "${{ matrix.test }}"
61
60
  steps:
62
- - uses: actions/checkout@v2
61
+ - uses: actions/checkout@v4
63
62
  - name: Set up Ruby
64
63
  uses: ruby/setup-ruby@v1
65
64
  with:
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,35 @@
1
+ ## 2.8.0 (March 17, 2024)
2
+
3
+ ### bug fix
4
+
5
+ * Fix a bug where the value of a false boolean attribute was not properly serialized because it was
6
+ treated as ruby's false in condition and that condition was never met. (by [@nashby](https://github.com/nashby))
7
+ * Make `Enumerize::Value#as_json` return a string, not an instance of `Enumerize::Value` (by [@nashby](https://github.com/nashby))
8
+ * Fix attribute type casting when Rails master is used (by [@nashby](https://github.com/nashby))
9
+ * Fix bug with `ActiveRecord#reload` when enumerized attribute is an instance of `ActiveRecord::Store` and it's nil. (by [@4ndypanda](https://github.com/4ndypanda))
10
+ * Fix attribute type casting when `ActiveRecord#dup` is used (by [@mihyaeru21](https://github.com/mihyaeru21))
11
+
12
+ ### enchancements
13
+
14
+ * Support only Ruby 3+ and Rails 6.1+. (by [@nashby](https://github.com/nashby))
15
+ * Allows the original options specified in enumerize to be retrieved from Enumerize::Attribute. (by [@okoshi-f](https://github.com/okoshi-f))
16
+
17
+ ## 2.7.0 (July 7, 2023)
18
+
19
+ ### bug fix
20
+
21
+ * Warn about already defined predicate methods only when we generate predicate methods with `predicate: true` (by [@nashby](https://github.com/nashby))
22
+ * Define `Type#cast` instead of deserialize to fix serialization issue. (by [@nashby](https://github.com/nashby))
23
+ * Fix `Undefined method 'enumerize' for RSpec::ExampleGroups` (by [@softwaregravy](https://github.com/softwaregravy))
24
+
25
+ ### enchancements
26
+
27
+ * Add support for procs as `i18n_scope` option value. (by [@nashby](https://github.com/nashby))
28
+
29
+ ```ruby
30
+ enumerize :color, in: %w[green blue], i18n_scope: proc { |value| "color" }
31
+ ```
32
+
1
33
  ## 2.6.1 (March 17, 2023)
2
34
 
3
35
  ### bug fix
data/Gemfile.global CHANGED
@@ -3,9 +3,12 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'rake'
6
- gem 'rspec', :require => false
7
6
 
8
- gem 'pg', '~> 1.2.3', :platform => [:ruby, :mswin, :mingw]
7
+ if ENV['TEST_FRAMEWORK'] == 'rspec'
8
+ gem 'rspec', :require => false
9
+ end
10
+
11
+ gem 'pg', '~> 1.5.3', :platform => [:ruby, :mswin, :mingw]
9
12
  gem 'sequel'
10
13
  gem 'simple_form'
11
14
  gem 'formtastic'
data/Gemfile.mongo_mapper CHANGED
@@ -1,6 +1,6 @@
1
1
  eval_gemfile('Gemfile.global')
2
2
 
3
3
  gem 'minitest', '~> 5.8'
4
- gem 'rails', '~> 5.2.4', :require => false
4
+ gem 'rails', github: 'rails/rails', branch: '7-0-stable', require: false
5
5
  gem 'mongo_mapper'
6
- gem 'sqlite3', '~> 1.3.6', :platform => [:ruby, :mswin, :mingw]
6
+ gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
data/Gemfile.rails70 CHANGED
@@ -3,7 +3,6 @@ eval_gemfile('Gemfile.global')
3
3
  gem 'minitest', '~> 5.8'
4
4
  gem 'rails', github: 'rails/rails', branch: '7-0-stable', require: false
5
5
 
6
- # TODO: Mongoid doesn't support Rails 7 yet. Uncomment when it's fixed https://jira.mongodb.org/browse/MONGOID-5193
7
- # gem 'mongoid', github: 'mongodb/mongoid'
6
+ gem 'mongoid', github: 'mongodb/mongoid'
8
7
 
9
8
  gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
@@ -1,6 +1,8 @@
1
1
  eval_gemfile('Gemfile.global')
2
2
 
3
3
  gem 'minitest', '~> 5.8'
4
- gem 'rails', '~> 6.0.0', require: false
4
+ gem 'rails', github: 'rails/rails', branch: '7-1-stable', require: false
5
+
5
6
  gem 'mongoid', github: 'mongodb/mongoid'
7
+
6
8
  gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
data/README.md CHANGED
@@ -46,8 +46,8 @@ Or install it yourself as:
46
46
 
47
47
  ## Supported Versions
48
48
 
49
- - Ruby 2.7+
50
- - Rails 5.2+
49
+ - Ruby 3.0+
50
+ - Rails 6.1+
51
51
 
52
52
  ## Usage
53
53
 
@@ -161,6 +161,7 @@ class Person
161
161
 
162
162
  enumerize :status, in: %w[student employed retired], i18n_scope: "status"
163
163
  enumerize :roles, in: %w[user admin], i18n_scope: ["user.roles", "roles"]
164
+ enumerize :color, in: %w[green blue], i18n_scope: proc { |value| "color" }
164
165
  end
165
166
 
166
167
  # localization file
data/Rakefile CHANGED
@@ -4,7 +4,6 @@
4
4
  require "bundler/gem_tasks"
5
5
 
6
6
  require 'rake/testtask'
7
- require 'rspec/core/rake_task'
8
7
 
9
8
  Rake::TestTask.new do |t|
10
9
  t.libs << 'test'
@@ -12,6 +11,11 @@ Rake::TestTask.new do |t|
12
11
  t.verbose = true
13
12
  end
14
13
 
15
- RSpec::Core::RakeTask.new
14
+ if ENV['TEST_FRAMEWORK'] == 'rspec'
15
+ require 'rspec/core/rake_task'
16
+ RSpec::Core::RakeTask.new
16
17
 
17
- task :default => [:test, :spec]
18
+ task :default => [:spec]
19
+ else
20
+ task :default => [:test]
21
+ end
@@ -21,7 +21,13 @@ module Enumerize
21
21
  require 'enumerize/hooks/uniqueness'
22
22
 
23
23
  unless options[:multiple]
24
- if ::ActiveRecord.version >= ::Gem::Version.new("7.0.0.alpha")
24
+ if ::ActiveRecord.version >= ::Gem::Version.new("7.2.0.alpha")
25
+ attribute(name)
26
+
27
+ decorate_attributes([name]) do |_, subtype|
28
+ Type.new(enumerized_attributes[name], subtype)
29
+ end
30
+ elsif ::ActiveRecord.version >= ::Gem::Version.new("7.0.0.alpha")
25
31
  attribute(name) do |subtype|
26
32
  Type.new(enumerized_attributes[name], subtype)
27
33
  end
@@ -74,7 +80,9 @@ module Enumerize
74
80
  end
75
81
 
76
82
  if store_attr.present?
77
- reloaded.send("#{attr.name}=", reloaded.send(store_attr).with_indifferent_access[attr.name])
83
+ unless reloaded.send(store_attr).nil?
84
+ reloaded.send("#{attr.name}=", reloaded.send(store_attr).with_indifferent_access[attr.name])
85
+ end
78
86
  else
79
87
  reloaded.send("#{attr.name}=", reloaded[attr.name])
80
88
  end
@@ -110,16 +118,18 @@ module Enumerize
110
118
 
111
119
  def serialize(value)
112
120
  v = @attr.find_value(value)
113
- (v && v.value) || value
114
- end
115
-
116
- alias type_cast_for_database serialize
121
+ return value unless v
117
122
 
118
- def deserialize(value)
119
- @attr.find_value(value)
123
+ v.value
120
124
  end
121
125
 
122
- alias type_cast_from_database deserialize
126
+ def cast(value)
127
+ if value.is_a?(::Enumerize::Value)
128
+ value
129
+ else
130
+ @attr.find_value(@subtype.cast(value))
131
+ end
132
+ end
123
133
 
124
134
  def as_json(options = nil)
125
135
  {attr: @attr.name}.as_json(options)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Enumerize
4
4
  class Attribute
5
- attr_reader :klass, :name, :values, :default_value, :i18n_scope, :skip_validations_value
5
+ attr_reader :klass, :name, :values, :default_value, :i18n_scope, :skip_validations_value, :arguments
6
6
 
7
7
  def initialize(klass, name, options={})
8
8
  raise ArgumentError, ':in option is required' unless options[:in]
@@ -12,11 +12,8 @@ module Enumerize
12
12
 
13
13
  @klass = klass
14
14
  @name = name.to_sym
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
15
+ @i18n_scope = options[:i18n_scope]
16
+ @arguments = options
20
17
 
21
18
  value_class = options.fetch(:value_class, Value)
22
19
  @values = Array(options[:in]).map { |v| value_class.new(self, *v).freeze }
@@ -69,8 +69,22 @@ module Enumerize
69
69
  end
70
70
 
71
71
  def build(klass)
72
+ warn_on_already_defined_methods
73
+
72
74
  klass.delegate(*names, to: @attr.name, prefix: @options[:prefix], allow_nil: true)
73
75
  end
76
+
77
+ def warn_on_already_defined_methods
78
+ names.each do |name|
79
+ method_name = [@options[:prefix], name].compact.join('_')
80
+
81
+ if @attr.klass.respond_to?(method_name)
82
+ warn(
83
+ "Predicate method `#{name}` is already defined as #{@attr.klass.name}##{name}. Use enumerize's :prefix option to avoid it"
84
+ )
85
+ end
86
+ end
87
+ end
74
88
  end
75
89
  end
76
90
  end
@@ -10,16 +10,16 @@ module Enumerize
10
10
  attr_reader :value
11
11
 
12
12
  def initialize(attr, name, value=nil)
13
- if self.class.method_defined?("#{name}?")
14
- warn("It's not recommended to use `#{name}` as a field value since `#{name}?` is defined. (#{attr.klass.name}##{attr.name})")
15
- end
16
-
17
13
  @attr = attr
18
14
  @value = value.nil? ? name.to_s : value
19
15
 
20
16
  super(name.to_s)
21
17
 
22
- @i18n_keys = @attr.i18n_scopes.map { |s| :"#{s}.#{self}" }
18
+ @i18n_keys = @attr.i18n_scopes.map do |s|
19
+ scope = Utils.call_if_callable(s, @value)
20
+
21
+ :"#{scope}.#{self}"
22
+ end
23
23
  @i18n_keys << :"enumerize.defaults.#{@attr.name}.#{self}"
24
24
  @i18n_keys << :"enumerize.#{@attr.name}.#{self}"
25
25
  @i18n_keys << ActiveSupport::Inflector.humanize(ActiveSupport::Inflector.underscore(self)) # humanize value if there are no translations
@@ -38,6 +38,10 @@ module Enumerize
38
38
  coder.represent_object(self.class.superclass, @value)
39
39
  end
40
40
 
41
+ def as_json(*)
42
+ to_s
43
+ end
44
+
41
45
  private
42
46
 
43
47
  def predicate_call(value)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumerize
4
- VERSION = '2.6.1'
4
+ VERSION = '2.8.0'
5
5
  end
data/lib/enumerize.rb CHANGED
@@ -82,7 +82,7 @@ module Enumerize
82
82
  end
83
83
 
84
84
  begin
85
- require 'rspec'
85
+ require 'rspec/matchers'
86
86
  rescue LoadError
87
87
  else
88
88
  require 'enumerize/integrations/rspec'
@@ -4,7 +4,7 @@ require 'test_helper'
4
4
 
5
5
  if defined?(::ActiveModel::Attributes)
6
6
 
7
- class ActiveModelTest < MiniTest::Spec
7
+ class ActiveModelTest < Minitest::Spec
8
8
  class ActiveModelUser
9
9
  include ActiveModel::Model
10
10
  include ActiveModel::Attributes
@@ -3,7 +3,6 @@
3
3
  require 'test_helper'
4
4
  require 'active_record'
5
5
  require 'logger'
6
-
7
6
  db = (ENV['DB'] || 'sqlite3').to_sym
8
7
 
9
8
  silence_warnings do
@@ -60,6 +59,9 @@ silence_warnings do
60
59
  end
61
60
 
62
61
  ActiveRecord::Base.connection.instance_eval do
62
+ ActiveRecord::Migration.drop_table :users, if_exists: true
63
+ ActiveRecord::Migration.drop_table :documents, if_exists: true
64
+
63
65
  create_table :users do |t|
64
66
  t.string :sex
65
67
  t.string :role
@@ -71,6 +73,8 @@ ActiveRecord::Base.connection.instance_eval do
71
73
  t.integer :skill
72
74
  t.string :account_type, :default => :basic
73
75
  t.string :foo
76
+ t.boolean :newsletter_subscribed, default: true
77
+ t.json :store_accessor_store_with_no_defaults
74
78
  end
75
79
 
76
80
  create_table :documents do |t|
@@ -117,6 +121,10 @@ class User < ActiveRecord::Base
117
121
  enumerize :skill, :in => { noob: 0, casual: 1, pro: 2 }, scope: :shallow
118
122
 
119
123
  enumerize :account_type, :in => [:basic, :premium]
124
+ enumerize :newsletter_subscribed, in: { subscribed: true, unsubscribed: false }
125
+
126
+ store_accessor :store_accessor_store_with_no_defaults, [:origin]
127
+ enumerize :origin, in: [:browser, :app]
120
128
 
121
129
  # There is no column for relationship enumeration for testing purposes: model
122
130
  # should not be broken even if the associated column does not exist yet.
@@ -139,7 +147,7 @@ class SkipValidationsUser < ActiveRecord::Base
139
147
  include SkipValidationsEnum
140
148
  end
141
149
 
142
- class DoNotSkipValidationsUser < ActiveRecord::Base
150
+ class DoNotSkipValidationsUser < ActiveRecord::Base
143
151
  self.table_name = "users"
144
152
  include DoNotSkipValidationsEnum
145
153
  end
@@ -154,7 +162,7 @@ class SkipValidationsLambdaWithParamUser < ActiveRecord::Base
154
162
  include SkipValidationsLambdaWithParamEnum
155
163
  end
156
164
 
157
- class ActiveRecordTest < MiniTest::Spec
165
+ class ActiveRecordTest < Minitest::Spec
158
166
  it 'sets nil if invalid value is passed' do
159
167
  user = User.new
160
168
  user.sex = :invalid
@@ -194,6 +202,15 @@ class ActiveRecordTest < MiniTest::Spec
194
202
  expect(user.language).must_equal 'en'
195
203
  end
196
204
 
205
+ it 'returns nil if store column is nil, uses .store_accessor, and has no default values for store\'s attributes' do
206
+ User.delete_all
207
+ user = User.create!
208
+ user.update_column(:store_accessor_store_with_no_defaults, nil)
209
+ user.reload
210
+ expect(user.store_accessor_store_with_no_defaults).must_be_nil
211
+ expect(user.origin).must_be_nil
212
+ end
213
+
197
214
  it 'has default value' do
198
215
  expect(User.new.role).must_equal 'user'
199
216
  expect(User.new.attributes['role']).must_equal 'user'
@@ -249,6 +266,15 @@ class ActiveRecordTest < MiniTest::Spec
249
266
  expect(user.interests).must_equal %w[music dancing]
250
267
  end
251
268
 
269
+ it 'has enumerized values in active record attributes after reload' do
270
+ User.delete_all
271
+ user = User.new
272
+ user.status = :blocked
273
+ user.save!
274
+ user.reload
275
+ expect(user.attributes["status"]).must_equal 'blocked'
276
+ end
277
+
252
278
  it 'validates inclusion when using write_attribute with string attribute' do
253
279
  user = User.new
254
280
  user.send(:write_attribute, 'role', 'wrong')
@@ -645,6 +671,28 @@ class ActiveRecordTest < MiniTest::Spec
645
671
  expect(sql).must_include 'LIKE \'%foo%\''
646
672
  end
647
673
 
674
+ it 'supports boolean column as enumerized field' do
675
+ User.delete_all
676
+
677
+ User.create!(newsletter_subscribed: true)
678
+ expect(User.find_by(newsletter_subscribed: true).newsletter_subscribed).must_equal 'subscribed'
679
+
680
+ User.create!(newsletter_subscribed: false)
681
+ expect(User.find_by(newsletter_subscribed: false).newsletter_subscribed).must_equal 'unsubscribed'
682
+ end
683
+
684
+ it 'has same value with original object when created by #dup' do
685
+ user1 = User.new(skill: :casual)
686
+ user2 = user1.dup
687
+ expect(user2.skill).must_equal 'casual'
688
+ end
689
+
690
+ it 'has same value with original object when created by #clone' do
691
+ user1 = User.new(skill: :casual)
692
+ user2 = user1.clone
693
+ expect(user2.skill).must_equal 'casual'
694
+ end
695
+
648
696
  if Rails::VERSION::MAJOR >= 6
649
697
  it 'supports AR#insert_all' do
650
698
  User.delete_all
@@ -3,7 +3,7 @@
3
3
  require 'test_helper'
4
4
 
5
5
  module Enumerize
6
- class AttributeMapTest < MiniTest::Spec
6
+ class AttributeMapTest < Minitest::Spec
7
7
  subject { AttributeMap.new }
8
8
 
9
9
  def make_attr(name)
@@ -52,7 +52,7 @@ module Enumerize
52
52
 
53
53
  it 'updates dependants' do
54
54
  attr = make_attr(:a)
55
- dependant = MiniTest::Mock.new
55
+ dependant = Minitest::Mock.new
56
56
  dependant.expect(:<<, nil, [attr])
57
57
  subject.add_dependant dependant
58
58
  subject << attr
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class AttributeTest < MiniTest::Spec
5
+ class AttributeTest < Minitest::Spec
6
6
  def attr
7
7
  @attr ||= nil
8
8
  end
@@ -37,9 +37,12 @@ class AttributeTest < MiniTest::Spec
37
37
  build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
38
38
  expect(attr.i18n_scopes).must_equal %w[bar buzz]
39
39
  end
40
+ end
40
41
 
41
- it 'accepts only string scopes' do
42
- expect(proc { build_attr nil, 'foo', :in => %w[a b], :i18n_scope => [%w[bar buzz], "bar.buzz"] }).must_raise ArgumentError
42
+ describe 'arguments' do
43
+ it 'returns arguments' do
44
+ build_attr nil, :foo, :in => [:a, :b], :scope => true
45
+ expect(attr.arguments).must_equal({:in => [:a, :b], :scope => true})
43
46
  end
44
47
  end
45
48
 
data/test/base_test.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class BaseTest < MiniTest::Spec
5
+ class BaseTest < Minitest::Spec
6
6
  let(:kklass) do
7
7
  Class.new do
8
8
  extend Enumerize
@@ -5,7 +5,7 @@ require 'test_helper'
5
5
  Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
6
6
  Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
7
7
 
8
- class FormtasticSpec < MiniTest::Spec
8
+ class FormtasticSpec < Minitest::Spec
9
9
  include ViewTestHelper
10
10
  include Formtastic::Helpers::FormHelper
11
11
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class ModuleAttributesSpec < MiniTest::Spec
5
+ class ModuleAttributesSpec < Minitest::Spec
6
6
  it 'inherits attribute from the module' do
7
7
  mod = Module.new do
8
8
  extend Enumerize
@@ -10,7 +10,7 @@ end
10
10
 
11
11
  MongoMapper.connection = Mongo::Client.new(['localhost:27017'], database: 'enumerize-test-suite-of-mongomapper')
12
12
 
13
- class MongoMapperTest < MiniTest::Spec
13
+ class MongoMapperTest < Minitest::Spec
14
14
  class MongoMapperUser
15
15
  include MongoMapper::Document
16
16
  extend Enumerize
data/test/mongoid_test.rb CHANGED
@@ -13,7 +13,7 @@ Mongoid.configure do |config|
13
13
  config.options = { use_utc: true, include_root_in_json: true }
14
14
  end
15
15
 
16
- class MongoidTest < MiniTest::Spec
16
+ class MongoidTest < Minitest::Spec
17
17
  class MongoidUser
18
18
  include Mongoid::Document
19
19
  extend Enumerize
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class MultipleTest < MiniTest::Spec
5
+ class MultipleTest < Minitest::Spec
6
6
  let(:kklass) do
7
7
  Class.new do
8
8
  extend Enumerize
@@ -2,10 +2,12 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class PredicatesTest < MiniTest::Spec
5
+ class PredicatesTest < Minitest::Spec
6
6
  let(:kklass) do
7
7
  Class.new do
8
8
  extend Enumerize
9
+
10
+ def self.c?; end
9
11
  end
10
12
  end
11
13
 
@@ -62,4 +64,22 @@ class PredicatesTest < MiniTest::Spec
62
64
  expect(object).wont_respond_to :a?
63
65
  expect(object).must_respond_to :b?
64
66
  end
67
+
68
+ it 'warns if predicate method is already defined' do
69
+ assert_output(nil, /`c\?` is already defined/) do
70
+ kklass.enumerize(:bar, in: %w(a b c), predicates: true)
71
+ end
72
+ end
73
+
74
+ it 'does not warn if predicate has prefix and does not collide with defined method' do
75
+ assert_output(nil, '') do
76
+ kklass.enumerize(:bar, in: %w(a b c), predicates: { prefix: 'bar' })
77
+ end
78
+ end
79
+
80
+ it 'does not warn if predicate method is already defined but enumerize does not generate predicates' do
81
+ assert_output(nil, '') do
82
+ kklass.enumerize(:bar, in: %w(a b c))
83
+ end
84
+ end
65
85
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'test_helper'
4
4
 
5
- class RailsAdminSpec < MiniTest::Spec
5
+ class RailsAdminSpec < Minitest::Spec
6
6
  let(:kklass) do
7
7
  Class.new do
8
8
  extend Enumerize
data/test/sequel_test.rb CHANGED
@@ -5,7 +5,7 @@ require 'sequel'
5
5
  require 'logger'
6
6
  require 'jdbc/sqlite3' if RUBY_PLATFORM == 'java'
7
7
 
8
- class SequelTest < MiniTest::Spec
8
+ class SequelTest < Minitest::Spec
9
9
  silence_warnings do
10
10
  DB = if RUBY_PLATFORM == 'java'
11
11
  Sequel.connect('jdbc:sqlite::memory:')
data/test/set_test.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'test_helper'
4
4
  require 'yaml'
5
5
 
6
- class SetTest < MiniTest::Spec
6
+ class SetTest < Minitest::Spec
7
7
  let(:kklass) do
8
8
  Class.new do
9
9
  extend Enumerize
@@ -3,7 +3,7 @@
3
3
  require 'test_helper'
4
4
  require 'simple_form/version'
5
5
 
6
- class SimpleFormSpec < MiniTest::Spec
6
+ class SimpleFormSpec < Minitest::Spec
7
7
  include ViewTestHelper
8
8
  include SimpleForm::ActionViewExtensions::FormHelper
9
9
 
data/test/test_helper.rb CHANGED
@@ -4,6 +4,7 @@ require 'minitest/autorun'
4
4
  require 'minitest/spec'
5
5
  require 'active_support/core_ext/kernel/reporting'
6
6
  require 'active_model'
7
+ require 'active_job'
7
8
  require 'rails'
8
9
  begin
9
10
  require 'mongoid'
@@ -56,6 +57,6 @@ module MiscHelpers
56
57
  end
57
58
  end
58
59
 
59
- class MiniTest::Spec
60
+ class Minitest::Spec
60
61
  include MiscHelpers
61
62
  end
data/test/value_test.rb CHANGED
@@ -3,11 +3,14 @@
3
3
  require 'test_helper'
4
4
  require 'yaml'
5
5
 
6
- class ValueTest < MiniTest::Spec
6
+ class ValueTest < Minitest::Spec
7
7
  class Model
8
8
  end
9
9
 
10
10
  class Attr < Struct.new(:values, :name, :i18n_scopes, :klass)
11
+ def value?(value)
12
+ values.include?(value)
13
+ end
11
14
  end
12
15
 
13
16
  let(:attr) { Attr.new([], "attribute_name", [], Model) }
@@ -84,6 +87,14 @@ class ValueTest < MiniTest::Spec
84
87
  expect(val.text).must_be :==, "Scope specific translation"
85
88
  end
86
89
  end
90
+
91
+ it 'allows to pass a proc as i18n_scopes param' do
92
+ attr.i18n_scopes = [proc { |v| "other.scope.#{v}" }]
93
+
94
+ store_translations(:en, :other => {:scope => {:"1" => {:test_value => "Scope specific translation"}}}) do
95
+ expect(val.text).must_be :==, "Scope specific translation"
96
+ end
97
+ end
87
98
  end
88
99
 
89
100
  describe 'boolean methods comparison' do
@@ -150,9 +161,12 @@ class ValueTest < MiniTest::Spec
150
161
  it 'no output if undefined boolean method' do
151
162
  assert_silent() { Enumerize::Value.new(attr, 'test_value') }
152
163
  end
164
+ end
153
165
 
154
- it 'error output if defined boolean method' do
155
- assert_output(nil, /`empty\?` is defined/) { Enumerize::Value.new(attr, 'empty') }
166
+ describe '#as_json' do
167
+ it 'returns String object, not Value object' do
168
+ expect(val.as_json.class).must_equal String
169
+ expect(val.as_json).must_equal 'test_value'
156
170
  end
157
171
  end
158
172
  end
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.6.1
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Nartimov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-17 00:00:00.000000000 Z
11
+ date: 2024-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -31,16 +31,17 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - ".github/dependabot.yml"
34
35
  - ".github/workflows/ruby.yml"
35
36
  - ".gitignore"
36
37
  - ".rspec"
38
+ - ".tool-versions"
37
39
  - CHANGELOG.md
38
- - Gemfile
39
40
  - Gemfile.global
40
41
  - Gemfile.mongo_mapper
41
- - Gemfile.rails60
42
42
  - Gemfile.rails61
43
43
  - Gemfile.rails70
44
+ - Gemfile.rails71
44
45
  - Gemfile.railsmaster
45
46
  - MIT-LICENSE
46
47
  - README.md
@@ -99,7 +100,7 @@ homepage: https://github.com/brainspec/enumerize
99
100
  licenses:
100
101
  - MIT
101
102
  metadata: {}
102
- post_install_message:
103
+ post_install_message:
103
104
  rdoc_options: []
104
105
  require_paths:
105
106
  - lib
@@ -114,8 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  - !ruby/object:Gem::Version
115
116
  version: '0'
116
117
  requirements: []
117
- rubygems_version: 3.1.6
118
- signing_key:
118
+ rubygems_version: 3.4.7
119
+ signing_key:
119
120
  specification_version: 4
120
121
  summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
121
122
  test_files:
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- eval_gemfile('Gemfile.global')
2
-
3
- gem 'minitest', '~> 5.8'
4
- gem 'rails', '~> 5.2.4', require: false
5
- gem 'mongoid'
6
- gem 'sqlite3', '~> 1.5', :platform => [:ruby, :mswin, :mingw]