mongoid-enum-i18n 0.2.5 → 0.2.9

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: 052aa901fd5927b0b43d7a2af5238bd93aaa8a43
4
- data.tar.gz: 4156cf8e480c110bb3e1bb576cfeb4671b660613
3
+ metadata.gz: cc39e71840d462cac737d0756eea80a8f831089e
4
+ data.tar.gz: c388c3055bd4af60a7549d71645511a40a242443
5
5
  SHA512:
6
- metadata.gz: a4b9fa2e8ec1f61645aa1dead56571585e284b602a8627609c535440d2a160b17e275637a9ecf4b24297f775a865d74d7732d7f01eecad3a3d40b369333967c3
7
- data.tar.gz: dfd098909d946ce1e625aae35006b9a49bbb1264f4384cc697824633d4df8585285efdf93e9e18b2730969220ad914408c7febe2199c9f787bdc27ae309c29a9
6
+ metadata.gz: 6ff488c1098984135d6d1a70da51395c909b2865a22e9ff1538e76b894e0fc29d78dc3da4f6428fef5ea0c8fabaeddd5b3396e003298bfc84faad69e0677aa0c
7
+ data.tar.gz: 6702eb2c30f6872a6df327cb0a8c2d56e68987d2180c42fcfdca31a6391b0188e7490ac609e84b5d18ea4d9a4ccc4edeb96b4d03cd10b0b3d7e841106a01f84f
data/.travis.yml CHANGED
@@ -12,3 +12,5 @@ rvm:
12
12
 
13
13
  services:
14
14
  - mongodb
15
+
16
+ sudo: false
data/Gemfile CHANGED
@@ -1,4 +1,16 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mongoid-enum.gemspec
4
+ gem 'mongoid', '>= 5.0.0.beta'
5
+
4
6
  gemspec
7
+
8
+ group :test do
9
+ gem 'guard'
10
+ gem 'rubocop'
11
+ gem 'guard-rspec'
12
+ gem 'guard-rubocop'
13
+ gem 'rspec', '>= 3.2.0'
14
+ gem 'codeclimate-test-reporter', require: nil
15
+ gem 'mongoid-rspec', git: 'https://github.com/nofxx/mongoid-rspec'
16
+ end
data/Guardfile ADDED
@@ -0,0 +1,22 @@
1
+ # Note: The cmd option is now required due to the increasing number of ways
2
+ # rspec may be run, below are examples of the most common uses.
3
+ # * bundler: 'bundle exec rspec'
4
+ # * bundler binstubs: 'bin/rspec'
5
+ # * spring: 'bin/rsspec' (This will use spring if running and you have
6
+ # installed the spring binstubs per the docs)
7
+ # * zeus: 'zeus rspec' (requires the server to be started separetly)
8
+ # * 'just' rspec: 'rspec'
9
+
10
+ # guard :rubocop do
11
+ guard :rubocop, all_on_start: false, keep_failed: false, cli: ['-D'] do
12
+ watch(%r{.+\.rb$})
13
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
14
+ end
15
+
16
+ guard :rspec, cmd: 'bundle exec rspec' do
17
+ watch(%r{^spec/.+_spec\.rb$})
18
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
19
+ watch(/^generators\/(.+)\.rb$/) { |_m| 'spec/schemaless/worker_spec' }
20
+
21
+ watch('spec/spec_helper.rb') { 'spec' }
22
+ end
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # Mongoid::Enum (::I18n alternative gem)
2
2
 
3
- [![Build
4
- Status](https://travis-ci.org/thetron/mongoid-enum.png)](https://travis-ci.org/thetron/mongoid-enum)
5
- [![Code Climate](https://codeclimate.com/github/thetron/mongoid-enum.png)](https://codeclimate.com/github/thetron/mongoid-enum)
6
3
 
7
4
  Heavily inspired by [DHH's
8
5
  ActiveRecord::Enum](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5), this little library is
data/lib/mongoid/enum.rb CHANGED
@@ -18,26 +18,27 @@ module Mongoid
18
18
  # Main class method
19
19
  #
20
20
  def enum(field_name, values, options = {})
21
- options = default_options(values).merge(options)
21
+ options = default_options.merge(options)
22
22
 
23
23
  set_values_constant field_name, values
24
24
 
25
- create_field field_name, options
25
+ create_field field_name, options, values
26
26
  create_i18n_helper field_name, options
27
27
  create_values_helper field_name, options
28
28
 
29
29
  create_validations field_name, values, options
30
30
  define_value_scopes_and_accessors field_name, values, options
31
+ return unless options[:multiple]
32
+ define_array_field_writer field_name
31
33
  end
32
34
 
33
35
  private
34
36
 
35
- def default_options(values)
37
+ def default_options
36
38
  {
37
- :multiple => false,
38
- :default => values.first,
39
- :required => true,
40
- :validate => true
39
+ multiple: false,
40
+ required: true,
41
+ validate: true
41
42
  }
42
43
  end
43
44
 
@@ -46,21 +47,33 @@ module Mongoid
46
47
  const_set const_name, values
47
48
  end
48
49
 
49
- def create_field(field_name, options)
50
+ def create_field(field_name, options, values)
50
51
  type = options[:multiple] && Array || Symbol
51
- field field_name, :type => type, :default => options[:default]
52
+ default = \
53
+ if options.key?(:default)
54
+ options[:default]
55
+ else
56
+ options[:multiple] ? [] : values.first
57
+ end
58
+ field field_name, type: type, default: default
59
+ end
60
+
61
+ def define_array_field_writer(field_name)
62
+ define_method("#{field_name}=") do |vals|
63
+ write_attribute(field_name, Array(vals).compact.map(&:to_sym))
64
+ end
52
65
  end
53
66
 
54
67
  def create_validations(field_name, values, options)
55
68
  if options[:multiple] && options[:validate]
56
69
  validates field_name, :'mongoid/enum/validators/multiple' => {
57
- :in => values,
58
- :allow_nil => !options[:required]
70
+ in: values.map(&:to_sym),
71
+ allow_nil: !options[:required]
59
72
  }
60
73
  elsif validate
61
74
  validates field_name,
62
- :inclusion => { :in => values },
63
- :allow_nil => !options[:required]
75
+ inclusion: { in: values },
76
+ allow_nil: !options[:required]
64
77
  end
65
78
  end
66
79
 
@@ -74,7 +87,7 @@ module Mongoid
74
87
 
75
88
  def create_values_helper(field_name, options)
76
89
  return if options[:i18n].is_a?(FalseClass)
77
- define_method("#{field_name}_values") do
90
+ define_singleton_method("#{field_name}_values") do
78
91
  I18n.translate("mongoid.enums.#{model_name.to_s.underscore}."\
79
92
  "#{field_name}").map { |k, v| [v, k] }
80
93
  end
@@ -101,7 +114,9 @@ module Mongoid
101
114
 
102
115
  def define_string_accessor(field_name, value)
103
116
  class_eval "def #{value}?() self.#{field_name} == :#{value} end"
104
- class_eval "def #{value}!() update_attributes! :#{field_name} => :#{value} end"
117
+ define_method("#{value}!") do
118
+ update_attributes! :"#{field_name}" => :"#{value}"
119
+ end
105
120
  end
106
121
  end
107
122
  end
@@ -7,7 +7,7 @@ module Mongoid
7
7
  #
8
8
  class MultipleValidator < ActiveModel::EachValidator
9
9
  def validate_each(record, attribute, values)
10
- values = Array(values)
10
+ values = Array(values).map(&:to_sym)
11
11
 
12
12
  if options[:allow_nil]
13
13
  unless all_included?(values, options[:in])
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  # VERSION
3
3
  module Enum
4
- VERSION = '0.2.5'
4
+ VERSION = '0.2.9'
5
5
  end
6
6
  end
@@ -19,10 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "mongoid", "> 4.0.0"
22
-
23
- spec.add_development_dependency "bundler"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec"
26
- spec.add_development_dependency "guard-rspec"
27
- spec.add_development_dependency "mongoid-rspec"
28
22
  end
@@ -5,7 +5,10 @@ class User
5
5
  include Mongoid::Enum
6
6
 
7
7
  enum :status, [:awaiting_approval, :approved, :banned]
8
- enum :roles, [:author, :editor, :admin], multiple: true, default: [], required: false
8
+ enum :roles, [:author, :editor, :admin],
9
+ default: [],
10
+ multiple: true,
11
+ required: false
9
12
  end
10
13
 
11
14
  describe Mongoid::Enum do
@@ -52,6 +55,22 @@ describe Mongoid::Enum do
52
55
  end
53
56
  end
54
57
 
58
+ describe "'required' option" do
59
+ context 'when true' do
60
+ let(:instance) { User.new status: nil }
61
+ it 'is not valid with nil value' do
62
+ expect(instance).to_not be_valid
63
+ end
64
+ end
65
+
66
+ context 'when false' do
67
+ let(:instance) { User.new roles: nil }
68
+ it 'is valid with nil value' do
69
+ expect(instance).to be_valid
70
+ end
71
+ end
72
+ end
73
+
55
74
  describe 'constant' do
56
75
  it 'is set to the values' do
57
76
  expect(klass::STATUS).to eq values
@@ -60,6 +79,18 @@ describe Mongoid::Enum do
60
79
 
61
80
  describe 'accessors'do
62
81
  context 'when singular' do
82
+ describe 'setter' do
83
+ it 'accepts strings' do
84
+ instance.status = 'banned'
85
+ expect(instance.status).to eq :banned
86
+ end
87
+
88
+ it 'accepts symbols' do
89
+ instance.status = :banned
90
+ expect(instance.status).to eq :banned
91
+ end
92
+ end
93
+
63
94
  describe '{{value}}!' do
64
95
  it 'sets the value' do
65
96
  instance.save
@@ -87,6 +118,32 @@ describe Mongoid::Enum do
87
118
  end
88
119
 
89
120
  context 'when multiple' do
121
+ describe 'setter' do
122
+ it 'accepts strings' do
123
+ instance.roles = 'author'
124
+ expect(instance.roles).to eq [:author]
125
+ end
126
+
127
+ it 'accepts symbols' do
128
+ instance.roles << :author
129
+ expect(instance.roles).to eq [:author]
130
+ end
131
+
132
+ it 'accepts arrays of strings' do
133
+ instance.roles = %w( author editor )
134
+ instance.save
135
+ puts instance.errors.full_messages
136
+ instance.reload
137
+ expect(instance.roles).to include(:author)
138
+ expect(instance.roles).to include(:editor)
139
+ end
140
+
141
+ it 'accepts arrays of symbols' do
142
+ instance.roles = [:author, :editor]
143
+ expect(instance.roles).to include(:author)
144
+ expect(instance.roles).to include(:editor)
145
+ end
146
+ end
90
147
  describe '{{value}}!' do
91
148
  context 'when field is nil' do
92
149
  it 'creates an array containing the value' do
@@ -183,7 +240,7 @@ describe Mongoid::Enum do
183
240
  end
184
241
 
185
242
  it 'should have a helper to translate the values' do
186
- expect(instance.status_values)
243
+ expect(User.status_values)
187
244
  .to eq([['Awaiting Approval', :awaiting_approval]])
188
245
  end
189
246
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
2
 
3
- require 'database_cleaner'
4
3
  require 'mongoid'
5
4
  require 'mongoid-rspec'
6
5
  require 'mongoid/enum'
@@ -9,9 +8,9 @@ ENV['MONGOID_ENV'] = 'test'
9
8
 
10
9
  RSpec.configure do |config|
11
10
  config.include Mongoid::Matchers
12
- config.before(:suite) do
13
- DatabaseCleaner.strategy = :truncation
14
- end
11
+ # config.before(:suite) do
12
+ # DatabaseCleaner.strategy = :truncation
13
+ # end
15
14
 
16
15
  config.after(:each) do
17
16
  Mongoid.purge!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-enum-i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Bruning
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-19 00:00:00.000000000 Z
12
+ date: 2015-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -25,76 +25,6 @@ dependencies:
25
25
  - - ">"
26
26
  - !ruby/object:Gem::Version
27
27
  version: 4.0.0
28
- - !ruby/object:Gem::Dependency
29
- name: bundler
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: rake
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
- - !ruby/object:Gem::Dependency
57
- name: rspec
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: '0'
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- - !ruby/object:Gem::Dependency
71
- name: guard-rspec
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: mongoid-rspec
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
28
  description: Heavily inspired by DDH's ActiveRecord::Enum, this little library is
99
29
  there to help you cut down the cruft in your models and make the world a happier
100
30
  place at the same time.
@@ -109,6 +39,7 @@ files:
109
39
  - ".rspec"
110
40
  - ".travis.yml"
111
41
  - Gemfile
42
+ - Guardfile
112
43
  - LICENSE.txt
113
44
  - README.md
114
45
  - Rakefile
@@ -142,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
73
  version: '0'
143
74
  requirements: []
144
75
  rubyforge_project:
145
- rubygems_version: 2.4.7
76
+ rubygems_version: 2.4.8
146
77
  signing_key:
147
78
  specification_version: 4
148
79
  summary: Sweet enum sugar for your Mongoid documents
@@ -153,3 +84,4 @@ test_files:
153
84
  - spec/support/app.en.yml
154
85
  - spec/support/mongoid.yml
155
86
  - spec/support/mongoid4.yml
87
+ has_rdoc: