enumerize 0.10.1 → 0.11.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
2
  SHA1:
3
- metadata.gz: 7430100f0e35ac39085f08ca0dca223b4f101a86
4
- data.tar.gz: 5726bf780a70d9cfd67e6c51622c734abd7a9644
3
+ metadata.gz: e4e3454dcf7faa010477afaa0c7de0b49e3e7e7e
4
+ data.tar.gz: fe8517318865f32bfe0421493e2431f90ca568dc
5
5
  SHA512:
6
- metadata.gz: 1221db4e0a2377d4123c0d64f3b0fbcf63d549e15963d44562a96d4a68eac0017015fbba7ce4e56bdaf4f6904ca5ab99592fdad70813333cdd52032ae440902c
7
- data.tar.gz: dfd016c59f61900a641483df577bf793ce9f6b36f695958d6f9676ef49ed4f54d7cce57e1a2c9936ab8f7f63ee76f9932244d998474cf32677a55671339cc6d8
6
+ metadata.gz: 9e54b9a844cf53b5570f82b4da4fbf509e4f62ef0b947ee16776b0608d2dfdde6b49d56c401abc7a16ed242bf57c64bf51450279513e70c7b577a2cbf67a4c87
7
+ data.tar.gz: 0a607cef37922a25e47df3fa99b03ae4e86e06ced6b58d77c4faa91cce771ac373bdbad30244da1d052488bac2c7bfb6ab497669581430fa2253ac16e821e470
data/.gitignore CHANGED
@@ -12,6 +12,7 @@ _yardoc
12
12
  coverage
13
13
  doc/
14
14
  lib/bundler/man
15
+ log/*
15
16
  pkg
16
17
  rdoc
17
18
  spec/reports
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.5
data/.travis.yml CHANGED
@@ -5,7 +5,7 @@ before_install:
5
5
  - gem install bundler
6
6
  gemfile:
7
7
  - Gemfile
8
- - Gemfile.rails4
8
+ - Gemfile.rails32
9
9
  rvm:
10
10
  - 1.9.3
11
11
  - 2.0.0
@@ -15,7 +15,12 @@ rvm:
15
15
  matrix:
16
16
  exclude:
17
17
  - rvm: 2.2
18
- gemfile: Gemfile
18
+ gemfile: Gemfile.rails32
19
+ include:
20
+ - rvm: 2.2
21
+ gemfile: Gemfile.rails40
22
+ - rvm: 2.2
23
+ gemfile: Gemfile.mongo_mapper
19
24
  notifications:
20
25
  email:
21
26
  recipients:
data/CHANGELOG.md CHANGED
@@ -4,6 +4,16 @@
4
4
 
5
5
  ### bug fix
6
6
 
7
+ ## 0.11.0 (March 29, 2015) ##
8
+
9
+ ### enhancements
10
+ * Add ability to set default value for enumerized field with multiple type. (by [@nashby](https://github.com/nashby))
11
+ * Support Rails 4.2. (by [@lest](https://github.com/lest))
12
+
13
+ ### bug fix
14
+ * Use Mongoid's `:in` method for generated scopes, fix chained scopes. (by [@nashby](https://github.com/nashby))
15
+ * Use `after_initialize` callback to set default value in Mongoid documents. (by [@nashby](https://github.com/nashby))
16
+
7
17
  ## 0.10.1 (March 4, 2015) ##
8
18
 
9
19
  ### bug fix
data/Gemfile CHANGED
@@ -3,10 +3,10 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'rake'
6
- gem 'minitest', '~> 4.1'
6
+ gem 'minitest', '~> 5.5.1'
7
7
  gem 'rspec', :require => false
8
8
 
9
- gem 'rails', '~> 3.2.0', :require => false
9
+ gem 'rails', '4.2.0', :require => false
10
10
  gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
11
  gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
12
 
@@ -18,6 +18,5 @@ platforms :rbx do
18
18
  end
19
19
 
20
20
  gem 'mongoid'
21
- gem 'mongo_mapper'
22
21
  gem 'simple_form'
23
22
  gem 'formtastic'
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'minitest', '~> 5.5.1'
7
+ gem 'rspec', :require => false
8
+
9
+ gem 'rails', '4.2.0', :require => false
10
+ gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
+ gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
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
+
20
+ gem 'mongo_mapper'
21
+ gem 'simple_form'
22
+ gem 'formtastic'
data/Gemfile.rails32 ADDED
@@ -0,0 +1,23 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'minitest', '~> 4.1'
7
+ gem 'rspec', :require => false
8
+
9
+ gem 'rails', '~> 3.2.0', :require => false
10
+ gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
11
+ gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
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
+
20
+ gem 'mongoid'
21
+ gem 'mongo_mapper'
22
+ gem 'simple_form'
23
+ gem 'formtastic'
@@ -17,7 +17,6 @@ platforms :rbx do
17
17
  gem 'rubysl-test-unit'
18
18
  end
19
19
 
20
- gem 'mongoid', github: 'mongoid/mongoid', ref: 'f91feef0a0c6b83a1b878e154f1014536aa1c298'
21
- gem 'mongo_mapper', github: 'jnunemaker/mongomapper'
20
+ gem 'mongoid'
22
21
  gem 'simple_form'
23
- gem 'formtastic', github: 'justinfrench/formtastic'
22
+ gem 'formtastic'
data/lib/enumerize.rb CHANGED
@@ -13,6 +13,7 @@ module Enumerize
13
13
  autoload :ModuleAttributes, 'enumerize/module_attributes'
14
14
 
15
15
  autoload :ActiveRecordSupport, 'enumerize/activerecord'
16
+ autoload :MongoidSupport, 'enumerize/mongoid'
16
17
 
17
18
  module Scope
18
19
  autoload :ActiveRecord, 'enumerize/scope/activerecord'
@@ -27,9 +28,16 @@ module Enumerize
27
28
  def self.extended(base)
28
29
  base.send :include, Enumerize::Base
29
30
  base.extend Enumerize::Predicates
30
- base.extend Enumerize::ActiveRecordSupport
31
- base.extend Enumerize::Scope::ActiveRecord if defined?(::ActiveRecord::Base)
32
- base.extend Enumerize::Scope::Mongoid if defined?(::Mongoid::Document)
31
+
32
+ if defined?(::ActiveRecord::Base)
33
+ base.extend Enumerize::ActiveRecordSupport
34
+ base.extend Enumerize::Scope::ActiveRecord
35
+ end
36
+
37
+ if defined?(::Mongoid::Document)
38
+ base.extend Enumerize::MongoidSupport
39
+ base.extend Enumerize::Scope::Mongoid
40
+ end
33
41
 
34
42
  if defined?(::RailsAdmin)
35
43
  require 'enumerize/integrations/rails_admin'
@@ -4,7 +4,7 @@ module Enumerize
4
4
  super
5
5
 
6
6
  _enumerize_module.dependent_eval do
7
- if defined?(::ActiveRecord::Base) && self < ::ActiveRecord::Base
7
+ if self < ::ActiveRecord::Base
8
8
  include InstanceMethods
9
9
 
10
10
  # Since Rails use `allocate` method on models and initializes them with `init_with` method.
@@ -134,6 +134,14 @@ module Enumerize
134
134
  end
135
135
 
136
136
  module Multiple
137
+ def find_default_value(value)
138
+ if value.respond_to?(:call)
139
+ value
140
+ else
141
+ find_values(*value)
142
+ end
143
+ end
144
+
137
145
  def define_methods!(mod)
138
146
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
139
147
  def #{name}
@@ -8,8 +8,9 @@ module Enumerize
8
8
  end
9
9
 
10
10
  class << base
11
- if (method_defined?(:inherited) || private_method_defined?(:inherited)) && (not method_defined? :inherited_without_enumerized)
11
+ if (method_defined?(:inherited) || private_method_defined?(:inherited)) && !private_method_defined?(:inherited_without_enumerized)
12
12
  alias_method :inherited_without_enumerized, :inherited
13
+ private :inherited_without_enumerized
13
14
  end
14
15
 
15
16
  alias_method :inherited, :inherited_with_enumerized
@@ -98,7 +99,9 @@ module Enumerize
98
99
  next
99
100
  end
100
101
 
101
- if !attr_value && !_enumerized_values_for_validation.key?(attr.name.to_s)
102
+ value_for_validation = _enumerized_values_for_validation[attr.name.to_s]
103
+
104
+ if (!attr_value || attr_value.empty?) && (!value_for_validation || value_for_validation.empty?)
102
105
  value = attr.default_value
103
106
 
104
107
  if value.respond_to?(:call)
@@ -1,7 +1,9 @@
1
1
  # backport of https://github.com/rails/rails/commit/3be9e8a0c2187744b6c9879ca2836cef5ebed693
2
2
  if defined?(ActionView::Helpers::InstanceTag)
3
- ActionView::Helpers::InstanceTag.class_eval do
4
- def self.check_box_checked?(value, checked_value)
3
+ ActionView::Helpers::InstanceTag.singleton_class.class_eval do
4
+ remove_method :check_box_checked?
5
+
6
+ def check_box_checked?(value, checked_value)
5
7
  case value
6
8
  when TrueClass, FalseClass
7
9
  value
@@ -0,0 +1,13 @@
1
+ module Enumerize
2
+ module MongoidSupport
3
+ def enumerize(name, options={})
4
+ super
5
+
6
+ _enumerize_module.dependent_eval do
7
+ if self < ::Mongoid::Document
8
+ after_initialize :_set_default_value_for_enumerized_attributes
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -20,12 +20,7 @@ module Enumerize
20
20
 
21
21
  define_singleton_method scope_name do |*values|
22
22
  values = enumerized_attributes[name].find_values(*values).map(&:value)
23
-
24
- if values.size == 1
25
- where(name => values.first)
26
- else
27
- where(name.in => values)
28
- end
23
+ self.in(name => values)
29
24
  end
30
25
 
31
26
  if options[:scope] == true
data/lib/enumerize/set.rb CHANGED
@@ -57,6 +57,10 @@ module Enumerize
57
57
  "#<Enumerize::Set {#{join(', ')}}>"
58
58
  end
59
59
 
60
+ def encode_with(coder)
61
+ coder.represent_object(Array, @values)
62
+ end
63
+
60
64
  private
61
65
 
62
66
  def predicate_call(value)
@@ -1,3 +1,3 @@
1
1
  module Enumerize
2
- VERSION = "0.10.1"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -15,13 +15,13 @@ ActiveRecord::Base.connection.instance_eval do
15
15
  t.string :lambda_role
16
16
  t.string :name
17
17
  t.string :interests
18
- t.string :status
18
+ t.integer :status
19
19
  t.string :account_type, :default => :basic
20
20
  end
21
21
 
22
22
  create_table :documents do |t|
23
23
  t.string :visibility
24
- t.timestamps
24
+ t.timestamps null: true
25
25
  end
26
26
  end
27
27
 
@@ -154,7 +154,7 @@ describe Enumerize::ActiveRecordSupport do
154
154
 
155
155
  it "uses persisted value for validation if it hasn't been set" do
156
156
  user = User.create! :sex => :male
157
- User.find(user).read_attribute_for_validation(:sex).must_equal 'male'
157
+ User.find(user.id).read_attribute_for_validation(:sex).must_equal 'male'
158
158
  end
159
159
 
160
160
  it 'is valid with empty string assigned' do
@@ -336,7 +336,6 @@ describe Enumerize::ActiveRecordSupport do
336
336
  user = User.create(:status => :active)
337
337
  user.status = :blocked
338
338
 
339
- expected = ActiveSupport::HashWithIndifferentAccess.new(status: [1, 2]).to_yaml
340
- assert_equal expected, user.changes.to_yaml
339
+ assert_equal [1, 2], YAML.load(user.changes.to_yaml)[:status]
341
340
  end
342
341
  end
@@ -1,16 +1,7 @@
1
1
  require 'test_helper'
2
- require 'rails'
3
-
4
- module Formtastic
5
- module Helpers
6
- module InputHelper
7
- remove_method :input_class
8
- def input_class(as)
9
- input_class_with_const_defined(as)
10
- end
11
- end
12
- end
13
- end
2
+
3
+ Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
4
+ Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
14
5
 
15
6
  class FormtasticSpec < MiniTest::Spec
16
7
  include ViewTestHelper
@@ -1,5 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
+ begin
4
+
3
5
  silence_warnings do
4
6
  require 'mongo_mapper'
5
7
  end
@@ -66,3 +68,7 @@ describe Enumerize do
66
68
  user.sex.must_equal 'female'
67
69
  end
68
70
  end
71
+
72
+ rescue LoadError
73
+ # Skip
74
+ end
data/test/mongoid_test.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
+ begin
4
+
3
5
  silence_warnings do
4
6
  require 'mongoid'
5
7
  end
@@ -17,9 +19,10 @@ describe Enumerize do
17
19
 
18
20
  field :sex
19
21
  field :role
20
- enumerize :sex, :in => %w[male female], scope: true
21
- enumerize :role, :in => %w[admin user], :default => 'user', scope: :having_role
22
- enumerize :mult, :in => %w[one two three four], :multiple => true
22
+ enumerize :sex, :in => %w[male female], scope: true
23
+ enumerize :status, :in => %w[notice warning error], scope: true
24
+ enumerize :role, :in => %w[admin user], :default => 'user', scope: :having_role
25
+ enumerize :mult, :in => %w[one two three four], :multiple => true
23
26
  end
24
27
 
25
28
  before { $VERBOSE = nil }
@@ -55,6 +58,14 @@ describe Enumerize do
55
58
  model.new.role.must_equal 'user'
56
59
  end
57
60
 
61
+ it 'uses after_initialize callback to set default value' do
62
+ model.delete_all
63
+ model.create!(sex: 'male', role: nil)
64
+
65
+ user = model.where(sex: 'male').first
66
+ user.role.must_equal 'user'
67
+ end
68
+
58
69
  it 'validates inclusion' do
59
70
  user = model.new
60
71
  user.role = 'wrong'
@@ -95,9 +106,24 @@ describe Enumerize do
95
106
  model.having_role(:user).to_a.must_equal [user_2]
96
107
  end
97
108
 
109
+ it 'chains scopes' do
110
+ model.delete_all
111
+
112
+ user_1 = model.create!(status: :notice)
113
+ user_2 = model.create!(status: :warning)
114
+ user_3 = model.create!(status: :error)
115
+
116
+ model.with_status(:notice, :warning).with_status(:notice, :error).to_a.must_equal [user_1]
117
+ model.with_status(:notice, :warning).union.with_status(:notice, :error).to_a.must_equal [user_1, user_2, user_3]
118
+ end
119
+
98
120
  it 'ignores not enumerized values that passed to the scope method' do
99
121
  model.delete_all
100
122
 
101
123
  model.with_sex(:foo).must_equal []
102
124
  end
103
125
  end
126
+
127
+ rescue LoadError
128
+ # Skip
129
+ end
@@ -24,6 +24,21 @@ describe Enumerize::Base do
24
24
  object.foos.must_equal %w(a c)
25
25
  end
26
26
 
27
+ it 'sets default value as single value' do
28
+ klass.enumerize :foos, in: %w(a b c), default: 'b', multiple: true
29
+ object.foos.must_equal %w(b)
30
+ end
31
+
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
34
+ object.foos.must_equal %w(b)
35
+ end
36
+
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
39
+ object.foos.must_equal %w(b c)
40
+ end
41
+
27
42
  it "doesn't define _text method" do
28
43
  klass.enumerize :foos, in: %w(a b c), multiple: true
29
44
  object.wont_respond_to :foos_text
data/test/rspec_spec.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rails'
1
2
  require 'enumerize'
2
3
  require 'rspec'
3
4
 
data/test/set_test.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'yaml'
2
3
 
3
4
  describe Enumerize::Set do
4
5
  let(:klass) do
@@ -147,4 +148,11 @@ describe Enumerize::Set do
147
148
  set.wont_respond_to :some_method?
148
149
  end
149
150
  end
151
+
152
+ describe 'serialization' do
153
+ it 'is serialized to yaml as array' do
154
+ set << :a
155
+ assert_equal YAML.dump(%w(a)), YAML.dump(set)
156
+ end
157
+ end
150
158
  end
@@ -1,6 +1,5 @@
1
1
  require 'test_helper'
2
2
  require 'simple_form/version'
3
- require 'rails'
4
3
 
5
4
  class SimpleFormSpec < MiniTest::Spec
6
5
  include ViewTestHelper
@@ -1,6 +1,10 @@
1
1
  require 'active_support/concern'
2
2
  require 'active_support/testing/setup_and_teardown'
3
3
 
4
+ if defined?(ActionView::RoutingUrlFor)
5
+ ActionView::RoutingUrlFor.send(:include, ActionDispatch::Routing::UrlFor)
6
+ end
7
+
4
8
  module ViewTestHelper
5
9
  extend ActiveSupport::Concern
6
10
 
data/test/test_helper.rb CHANGED
@@ -2,12 +2,29 @@ require 'minitest/autorun'
2
2
  require 'minitest/spec'
3
3
  require 'active_support/core_ext/kernel/reporting'
4
4
  require 'active_model'
5
-
6
- $VERBOSE=true
5
+ require 'rails'
7
6
 
8
7
  module RailsAdmin
9
8
  end
10
9
 
10
+ require 'simple_form'
11
+ SimpleForm.setup {}
12
+
13
+ require 'formtastic'
14
+
15
+ module EnumerizeTest
16
+ class Application < Rails::Application
17
+ config.active_support.deprecation = :stderr
18
+ config.active_support.test_order = :random
19
+ config.eager_load = false
20
+ config.secret_key_base = 'secret'
21
+ end
22
+ end
23
+
24
+ EnumerizeTest::Application.initialize!
25
+
26
+ $VERBOSE=true
27
+
11
28
  require 'enumerize'
12
29
 
13
30
  Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
data/test/value_test.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'yaml'
2
3
 
3
4
  describe Enumerize::Value do
4
5
  let(:attr) { Struct.new(:values).new([]) }
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.10.1
4
+ version: 0.11.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: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -32,10 +32,13 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - ".gitignore"
35
+ - ".ruby-version"
35
36
  - ".travis.yml"
36
37
  - CHANGELOG.md
37
38
  - Gemfile
38
- - Gemfile.rails4
39
+ - Gemfile.mongo_mapper
40
+ - Gemfile.rails32
41
+ - Gemfile.rails40
39
42
  - MIT-LICENSE
40
43
  - README.md
41
44
  - Rakefile
@@ -54,6 +57,7 @@ files:
54
57
  - lib/enumerize/integrations/rspec/matcher.rb
55
58
  - lib/enumerize/module.rb
56
59
  - lib/enumerize/module_attributes.rb
60
+ - lib/enumerize/mongoid.rb
57
61
  - lib/enumerize/predicatable.rb
58
62
  - lib/enumerize/predicates.rb
59
63
  - lib/enumerize/scope/activerecord.rb