mongoid-enum-i18n 0.2.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 +7 -0
- data/.autotest +0 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +186 -0
- data/Rakefile +1 -0
- data/lib/mongoid/enum.rb +108 -0
- data/lib/mongoid/enum/validators/multiple_validator.rb +36 -0
- data/lib/mongoid/enum/version.rb +6 -0
- data/mongoid-enum-i18n.gemspec +28 -0
- data/spec/mongoid/enum/validators/multiple_validator_spec.rb +80 -0
- data/spec/mongoid/enum_spec.rb +190 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/app.en.yml +6 -0
- data/spec/support/mongoid.yml +6 -0
- data/spec/support/mongoid4.yml +6 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6a69606cebd3d16441548a69f3516ee127d23c8
|
4
|
+
data.tar.gz: 2f47b16256c02ff531691c80f974e1a4dd313880
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2877e10e11e0df2bdddeaf9f0c50194ae14fa5c4aab0f31cb929460ab18ddcac5d047183fc890af045fd06a1aa17f193b33d79e24851b7e806fa141a73b16cdb
|
7
|
+
data.tar.gz: 65d02647878c9e7f54e543817e1aea86d88be356d46233edcde6a0187406cde0ad45383e589356fe6ceb6d5af14bb36ab0183e19adc67aeaa2658d1632764d32
|
data/.autotest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Nicholas Bruning
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
# Mongoid::Enum (::I18n alternative gem)
|
2
|
+
|
3
|
+
[](https://travis-ci.org/thetron/mongoid-enum)
|
5
|
+
[](https://codeclimate.com/github/thetron/mongoid-enum)
|
6
|
+
|
7
|
+
Heavily inspired by [DHH's
|
8
|
+
ActiveRecord::Enum](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5), this little library is
|
9
|
+
there to help you cut down the cruft in your models and make the
|
10
|
+
world a happier place at the same time.
|
11
|
+
|
12
|
+
A single line will get you fields, accessors, validations and scopes,
|
13
|
+
and a few other bits-and-bobs.
|
14
|
+
|
15
|
+
|
16
|
+
# Installation
|
17
|
+
|
18
|
+
Add this to your Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem "mongoid-enum"
|
22
|
+
```
|
23
|
+
|
24
|
+
And then run `bundle install`.
|
25
|
+
|
26
|
+
|
27
|
+
# Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Payment
|
31
|
+
include Mongoid::Document
|
32
|
+
include Mongoid::Enum
|
33
|
+
|
34
|
+
enum :status, [:pending, :approved, :declined]
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Aaaaaaand then you get things like:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
payment = Payment.create
|
42
|
+
|
43
|
+
payment.status
|
44
|
+
# => :pending
|
45
|
+
|
46
|
+
payment.approved!
|
47
|
+
# => :approved
|
48
|
+
|
49
|
+
payment.pending?
|
50
|
+
# => :false
|
51
|
+
|
52
|
+
Payment.approved
|
53
|
+
# => Mongoid::Criteria for payments with status == :approved
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
# Features
|
58
|
+
|
59
|
+
## Field
|
60
|
+
|
61
|
+
Your enum value is stored as either a Symbol, or an Array (when storing
|
62
|
+
multiple values). The actual field name has a leading underscore (e.g.:
|
63
|
+
`_status`), but is also aliased with its actual name for you
|
64
|
+
convenience.
|
65
|
+
|
66
|
+
|
67
|
+
## Accessors
|
68
|
+
|
69
|
+
Your enums will get getters-and-setters with the same name. So using the
|
70
|
+
`Payment` example above:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
payment.status = :declined
|
74
|
+
payment.status
|
75
|
+
# => :declined
|
76
|
+
```
|
77
|
+
|
78
|
+
And you also get bang(!) and query(?) methods for each of the values in
|
79
|
+
your enum (see [this example](#usage).
|
80
|
+
|
81
|
+
|
82
|
+
## Constants
|
83
|
+
|
84
|
+
For each enum, you'll also get a constant named after it. This is to
|
85
|
+
help you elsewhere in your app, should you need to display, or leverage
|
86
|
+
the list of values. Using the above example:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
Payment::STATUS
|
90
|
+
# => [:pending, :approved, :declined]
|
91
|
+
```
|
92
|
+
|
93
|
+
|
94
|
+
## Validations
|
95
|
+
|
96
|
+
Enum values are automatically validated against the list. You can
|
97
|
+
disable this behaviour (see [below](#options)).
|
98
|
+
|
99
|
+
|
100
|
+
## Scopes
|
101
|
+
|
102
|
+
A scope added for each of your enum's values. Using the example above,
|
103
|
+
you'd automatically get:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
Payment.pending # => Mongoid::Criteria
|
107
|
+
Payment.approved # => Mongoid::Criteria
|
108
|
+
Payment.declined # => Mongoid::Criteria
|
109
|
+
```
|
110
|
+
|
111
|
+
|
112
|
+
# Options
|
113
|
+
|
114
|
+
## Default value
|
115
|
+
|
116
|
+
If not specified, the default will be the first in your list of values
|
117
|
+
(`:pending` in the example above). You can override this with the
|
118
|
+
`:default` option:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
enum :roles, [:manager, :administrator], :default => ""
|
122
|
+
```
|
123
|
+
|
124
|
+
## Multiple values
|
125
|
+
|
126
|
+
Sometimes you'll need to store multiple values from your list, this
|
127
|
+
couldn't be easier:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
enum :roles, [:basic, :manager, :administrator], :multiple => true
|
131
|
+
|
132
|
+
# ...
|
133
|
+
|
134
|
+
user = User.create
|
135
|
+
user.roles << :basic
|
136
|
+
user.roles << :manager
|
137
|
+
user.save!
|
138
|
+
|
139
|
+
user.manager? # => true
|
140
|
+
user.administrator? # => false
|
141
|
+
user.roles # => [:basic, :manager]
|
142
|
+
```
|
143
|
+
|
144
|
+
Since the underlying datatype for storing values is an array, if you
|
145
|
+
need to specify default(s), ensure you use an array:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
enum :roles, [:noob, :author, :editor], :multiple => true, :default => [:author, :editor] # two defaults
|
149
|
+
enum :roles, [:noob, :author, :editor], :multiple => true, :default => [] # no default
|
150
|
+
```
|
151
|
+
|
152
|
+
## Validations
|
153
|
+
|
154
|
+
Validations are baked in by default, and ensure that the value(s) set in
|
155
|
+
your field are always from your list of options. If you need more
|
156
|
+
complex validations, or you just want to throw caution to the wind, you
|
157
|
+
can turn them off:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
enum :status, [:up, :down], :validate => false
|
161
|
+
```
|
162
|
+
|
163
|
+
## I18n
|
164
|
+
|
165
|
+
Two helpers are available, `#enum_i18`n:
|
166
|
+
|
167
|
+
```
|
168
|
+
Payment#status_i18n # => "Pending"
|
169
|
+
```
|
170
|
+
|
171
|
+
And `#enum_values`, good for drop downs:
|
172
|
+
|
173
|
+
```
|
174
|
+
Payment#status_values # => [["Pending, :pending]....
|
175
|
+
```
|
176
|
+
|
177
|
+
While/if the author implements/merges the i18n functionality, you may use
|
178
|
+
an alternative gem: `mongoid-enum-i18n`
|
179
|
+
|
180
|
+
|
181
|
+
# Issues and Feature Requests
|
182
|
+
|
183
|
+
If you have any problems, or you have a suggestion, please [submit an
|
184
|
+
issue](https://github.com/thetron/mongoid-enum/issues) (and a failing
|
185
|
+
test, if you can). Pull requests and feature requests are alwasy welcome
|
186
|
+
and greatly appreciated.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mongoid/enum.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'mongoid/enum/version'
|
2
|
+
require 'mongoid/enum/validators/multiple_validator'
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
# Mongoid Enum
|
6
|
+
module Enum
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
#
|
9
|
+
# Class Methods
|
10
|
+
#
|
11
|
+
# class Model
|
12
|
+
# include Mongoid::Enum
|
13
|
+
#
|
14
|
+
# enum :status, in: %i( waiting approved dismissed )
|
15
|
+
#
|
16
|
+
module ClassMethods
|
17
|
+
#
|
18
|
+
# Main class method
|
19
|
+
#
|
20
|
+
def enum(field_name, values, options = {})
|
21
|
+
options = default_options(values).merge(options)
|
22
|
+
|
23
|
+
set_values_constant field_name, values
|
24
|
+
|
25
|
+
create_field field_name, options
|
26
|
+
create_i18n_helper field_name, options
|
27
|
+
create_values_helper field_name, options
|
28
|
+
|
29
|
+
create_validations field_name, values, options
|
30
|
+
define_value_scopes_and_accessors field_name, values, options
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def default_options(values)
|
36
|
+
{
|
37
|
+
:multiple => false,
|
38
|
+
:default => values.first,
|
39
|
+
:required => true,
|
40
|
+
:validate => true
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_values_constant(name, values)
|
45
|
+
const_name = name.to_s.upcase
|
46
|
+
const_set const_name, values
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_field(field_name, options)
|
50
|
+
type = options[:multiple] && Array || Symbol
|
51
|
+
field field_name, :type => type, :default => options[:default]
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_validations(field_name, values, options)
|
55
|
+
if options[:multiple] && options[:validate]
|
56
|
+
validates field_name, :'mongoid/enum/validators/multiple' => {
|
57
|
+
:in => values,
|
58
|
+
:allow_nil => !options[:required]
|
59
|
+
}
|
60
|
+
elsif validate
|
61
|
+
validates field_name,
|
62
|
+
:inclusion => { :in => values },
|
63
|
+
:allow_nil => !options[:required]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_i18n_helper(field_name, options)
|
68
|
+
return if options[:i18n].is_a?(FalseClass)
|
69
|
+
define_method("#{field_name}_i18n") do
|
70
|
+
I18n.translate("mongoid.enums.#{model_name.to_s.underscore}."\
|
71
|
+
"#{field_name}.#{self[field_name]}")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def create_values_helper(field_name, options)
|
76
|
+
return if options[:i18n].is_a?(FalseClass)
|
77
|
+
define_method("#{field_name}_values") do
|
78
|
+
I18n.translate("mongoid.enums.#{model_name.to_s.underscore}."\
|
79
|
+
"#{field_name}").map { |k, v| [v, k] }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def define_value_scopes_and_accessors(field_name, values, options)
|
84
|
+
values.each do |value|
|
85
|
+
unless options[:scope].is_a?(FalseClass)
|
86
|
+
scope value, -> { where(field_name => value) }
|
87
|
+
end
|
88
|
+
|
89
|
+
if options[:multiple]
|
90
|
+
define_array_accessor(field_name, value)
|
91
|
+
else
|
92
|
+
define_string_accessor(field_name, value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def define_array_accessor(field_name, value)
|
98
|
+
class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
|
99
|
+
class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
|
100
|
+
end
|
101
|
+
|
102
|
+
def define_string_accessor(field_name, value)
|
103
|
+
class_eval "def #{value}?() self.#{field_name} == :#{value} end"
|
104
|
+
class_eval "def #{value}!() update_attributes! :#{field_name} => :#{value} end"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Enum
|
3
|
+
module Validators
|
4
|
+
# Mongoid Enum
|
5
|
+
#
|
6
|
+
# Multiple Enums Validator
|
7
|
+
#
|
8
|
+
class MultipleValidator < ActiveModel::EachValidator
|
9
|
+
def validate_each(record, attribute, values)
|
10
|
+
values = Array(values)
|
11
|
+
|
12
|
+
if options[:allow_nil]
|
13
|
+
unless all_included?(values, options[:in])
|
14
|
+
add_error_message record, attribute
|
15
|
+
end
|
16
|
+
elsif values.empty? || !all_included?(values, options[:in])
|
17
|
+
add_error_message record, attribute
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_error_message(record, attribute)
|
22
|
+
record.errors[attribute] <<
|
23
|
+
(options[:message] || "is not in #{options[:in].join ', '}")
|
24
|
+
end
|
25
|
+
|
26
|
+
def all_included?(values, allowed)
|
27
|
+
(values - allowed).empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.kind
|
31
|
+
:custom
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid/enum/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid-enum-i18n"
|
8
|
+
spec.version = Mongoid::Enum::VERSION
|
9
|
+
spec.authors = ["Nicholas Bruning", "Marcos Piccinini"]
|
10
|
+
spec.email = ["nicholas@bruning.com.au"]
|
11
|
+
spec.description = %q{Heavily inspired by DDH's ActiveRecord::Enum, this little library is there to help you cut down the cruft in your models and make the world a happier place at the same time.}
|
12
|
+
spec.summary = %q{Sweet enum sugar for your Mongoid documents}
|
13
|
+
spec.homepage = "https://github.com/thetron/mongoid-enum"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "mongoid", "~> 5"
|
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
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe Mongoid::Enum::Validators::MultipleValidator do
|
5
|
+
subject { Mongoid::Enum::Validators::MultipleValidator }
|
6
|
+
let(:values) { [:lorem, :ipsum, :dolor, :sit] }
|
7
|
+
let(:attribute) { :word }
|
8
|
+
let(:record) { OpenStruct.new(:errors => { attribute => [] }, attribute => values.first) }
|
9
|
+
let(:allow_nil) { false }
|
10
|
+
let(:validator) { subject.new(attributes: attribute, in: values, allow_nil: allow_nil) }
|
11
|
+
|
12
|
+
describe '.validate_each' do
|
13
|
+
context 'when allow_nil: true' do
|
14
|
+
let(:allow_nil) { true }
|
15
|
+
|
16
|
+
context 'and value is nil' do
|
17
|
+
before(:each) { validator.validate_each(record, attribute, nil) }
|
18
|
+
it 'validates' do
|
19
|
+
expect(record.errors[attribute].empty?).to be true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'and value is []' do
|
24
|
+
before(:each) { validator.validate_each(record, attribute, []) }
|
25
|
+
it 'validates' do
|
26
|
+
expect(record.errors[attribute].empty?).to be true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when allow_nil: false' do
|
32
|
+
context 'and value is nil' do
|
33
|
+
before(:each) { validator.validate_each(record, attribute, nil) }
|
34
|
+
it "won't validate" do
|
35
|
+
expect(record.errors[attribute].any?).to be true
|
36
|
+
expect(record.errors[attribute]).to eq ["is not in #{values.join ', '}"]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
context 'and value is []' do
|
40
|
+
before(:each) { validator.validate_each(record, attribute, []) }
|
41
|
+
it "won't validate" do
|
42
|
+
expect(record.errors[attribute].any?).to be true
|
43
|
+
expect(record.errors[attribute]).to eq ["is not in #{values.join ', '}"]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when value is included' do
|
49
|
+
let(:allow_nil) { rand(2).zero? }
|
50
|
+
before(:each) { validator.validate_each(record, attribute, [values.sample]) }
|
51
|
+
it 'validates' do
|
52
|
+
expect(record.errors[attribute].empty?).to be true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when value is not included' do
|
57
|
+
let(:allow_nil) { rand(2).zero? }
|
58
|
+
before(:each) { validator.validate_each(record, attribute, [:amet]) }
|
59
|
+
it "won't validate" do
|
60
|
+
expect(record.errors[attribute].any?).to be true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when multiple values included' do
|
65
|
+
let(:allow_nil) { rand(2).zero? }
|
66
|
+
before(:each) { validator.validate_each(record, attribute, [values.first, values.last]) }
|
67
|
+
it 'validates' do
|
68
|
+
expect(record.errors[attribute].empty?).to be true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when one value is not included 'do
|
73
|
+
let(:allow_nil) { rand(2).zero? }
|
74
|
+
before(:each) { validator.validate_each(record, attribute, [values.first, values.last, :amet]) }
|
75
|
+
it "won't validate" do
|
76
|
+
expect(record.errors[attribute].any?).to be true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::Enum
|
6
|
+
|
7
|
+
enum :status, [:awaiting_approval, :approved, :banned]
|
8
|
+
enum :roles, [:author, :editor, :admin], multiple: true, default: [], required: false
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Mongoid::Enum do
|
12
|
+
let(:klass) { User }
|
13
|
+
let(:instance) { User.new }
|
14
|
+
let(:alias_name) { :status }
|
15
|
+
let(:field_name) { alias_name.to_sym }
|
16
|
+
let(:values) { [:awaiting_approval, :approved, :banned] }
|
17
|
+
let(:multiple_field_name) { :roles }
|
18
|
+
|
19
|
+
describe 'field' do
|
20
|
+
it 'is defined' do
|
21
|
+
expect(klass).to have_field(field_name)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'is aliased' do
|
25
|
+
expect(instance).to respond_to alias_name
|
26
|
+
expect(instance).to respond_to :"#{alias_name}="
|
27
|
+
expect(instance).to respond_to :"#{alias_name}?"
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'type' do
|
31
|
+
context 'when multiple' do
|
32
|
+
it 'is an array' do
|
33
|
+
expect(klass).to have_field(multiple_field_name).of_type(Array)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'validates using a custom validator' do
|
37
|
+
expect(klass)
|
38
|
+
.to custom_validate(multiple_field_name)
|
39
|
+
.with_validator(Mongoid::Enum::Validators::MultipleValidator)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when not multiple' do
|
44
|
+
it 'is a symbol' do
|
45
|
+
expect(klass).to have_field(field_name).of_type(Symbol)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'validates inclusion in values' do
|
49
|
+
expect(klass).to validate_inclusion_of(field_name).to_allow(values)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'constant' do
|
56
|
+
it 'is set to the values' do
|
57
|
+
expect(klass::STATUS).to eq values
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'accessors'do
|
62
|
+
context 'when singular' do
|
63
|
+
describe '{{value}}!' do
|
64
|
+
it 'sets the value' do
|
65
|
+
instance.save
|
66
|
+
instance.banned!
|
67
|
+
expect(instance.status).to eq :banned
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '{{value}}?' do
|
72
|
+
context 'when {{enum}} == {{value}}' do
|
73
|
+
it 'returns true' do
|
74
|
+
instance.save
|
75
|
+
instance.banned!
|
76
|
+
expect(instance.banned?).to eq true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
context 'when {{enum}} != {{value}}' do
|
80
|
+
it 'returns false' do
|
81
|
+
instance.save
|
82
|
+
instance.banned!
|
83
|
+
expect(instance.approved?).to eq false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'when multiple' do
|
90
|
+
describe '{{value}}!' do
|
91
|
+
context 'when field is nil' do
|
92
|
+
it 'creates an array containing the value' do
|
93
|
+
instance.roles = nil
|
94
|
+
instance.save
|
95
|
+
instance.author!
|
96
|
+
expect(instance.roles).to eq [:author]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when field is not nil' do
|
101
|
+
it 'appends the value' do
|
102
|
+
instance.save
|
103
|
+
instance.author!
|
104
|
+
instance.editor!
|
105
|
+
expect(instance.roles).to eq [:author, :editor]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe '{{value}}?' do
|
111
|
+
context 'when {{enum}} contains {{value}}' do
|
112
|
+
it 'returns true' do
|
113
|
+
instance.save
|
114
|
+
instance.author!
|
115
|
+
instance.editor!
|
116
|
+
expect(instance.editor?).to be true
|
117
|
+
expect(instance.author?).to be true
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when {{enum}} does not contain {{value}}' do
|
122
|
+
it 'returns false' do
|
123
|
+
instance.save
|
124
|
+
expect(instance.author?).to be false
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe 'scopes' do
|
132
|
+
context 'when singular' do
|
133
|
+
it 'returns the corresponding documents' do
|
134
|
+
instance.save
|
135
|
+
instance.banned!
|
136
|
+
expect(User.banned.to_a).to eq [instance]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when multiple' do
|
141
|
+
context 'and only one document' do
|
142
|
+
it 'returns that document' do
|
143
|
+
instance.save
|
144
|
+
instance.author!
|
145
|
+
instance.editor!
|
146
|
+
expect(User.author.to_a).to eq [instance]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'and more than one document' do
|
151
|
+
it 'returns all documents with those values' do
|
152
|
+
instance.save
|
153
|
+
instance.author!
|
154
|
+
instance.editor!
|
155
|
+
instance2 = klass.create
|
156
|
+
instance2.author!
|
157
|
+
expect(User.author.to_a).to eq [instance, instance2]
|
158
|
+
expect(User.editor.to_a).to eq [instance]
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe 'default values' do
|
165
|
+
context 'when not specified' do
|
166
|
+
it 'uses the first value' do
|
167
|
+
instance.save
|
168
|
+
expect(instance.status).to eq values.first
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context 'when specified' do
|
173
|
+
it 'uses the specified value' do
|
174
|
+
instance.save
|
175
|
+
expect(instance.roles).to eq []
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe 'i18n' do
|
181
|
+
it 'should have a helper to translate the enum' do
|
182
|
+
expect(instance.status_i18n).to eq('Awaiting Approval')
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'should have a helper to translate the values' do
|
186
|
+
expect(instance.status_values)
|
187
|
+
.to eq([['Awaiting Approval', :awaiting_approval]])
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'database_cleaner'
|
4
|
+
require 'mongoid'
|
5
|
+
require 'mongoid-rspec'
|
6
|
+
require 'mongoid/enum'
|
7
|
+
|
8
|
+
ENV['MONGOID_ENV'] = 'test'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.include Mongoid::Matchers
|
12
|
+
config.before(:suite) do
|
13
|
+
DatabaseCleaner.strategy = :truncation
|
14
|
+
end
|
15
|
+
|
16
|
+
config.after(:each) do
|
17
|
+
Mongoid.purge!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
I18n.load_path << 'spec/support/app.en.yml'
|
22
|
+
I18n.default_locale = :en
|
23
|
+
|
24
|
+
Mongo::Logger.logger.level = Logger::INFO if Mongoid::VERSION >= '5'
|
25
|
+
|
26
|
+
Mongoid.load!(File.expand_path('../support/mongoid.yml', __FILE__), :test)
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-enum-i18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Bruning
|
8
|
+
- Marcos Piccinini
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-07-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '5'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '5'
|
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
|
+
description: Heavily inspired by DDH's ActiveRecord::Enum, this little library is
|
99
|
+
there to help you cut down the cruft in your models and make the world a happier
|
100
|
+
place at the same time.
|
101
|
+
email:
|
102
|
+
- nicholas@bruning.com.au
|
103
|
+
executables: []
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".autotest"
|
108
|
+
- ".gitignore"
|
109
|
+
- ".rspec"
|
110
|
+
- ".travis.yml"
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- lib/mongoid/enum.rb
|
116
|
+
- lib/mongoid/enum/validators/multiple_validator.rb
|
117
|
+
- lib/mongoid/enum/version.rb
|
118
|
+
- mongoid-enum-i18n.gemspec
|
119
|
+
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
120
|
+
- spec/mongoid/enum_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
- spec/support/app.en.yml
|
123
|
+
- spec/support/mongoid.yml
|
124
|
+
- spec/support/mongoid4.yml
|
125
|
+
homepage: https://github.com/thetron/mongoid-enum
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.4.7
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: Sweet enum sugar for your Mongoid documents
|
149
|
+
test_files:
|
150
|
+
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
151
|
+
- spec/mongoid/enum_spec.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
- spec/support/app.en.yml
|
154
|
+
- spec/support/mongoid.yml
|
155
|
+
- spec/support/mongoid4.yml
|