mongoid-enum 0.1.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 +15 -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 +144 -0
- data/Rakefile +1 -0
- data/lib/mongoid/enum.rb +41 -0
- data/lib/mongoid/enum/validators/multiple_validator.rb +29 -0
- data/lib/mongoid/enum/version.rb +5 -0
- data/mongoid-enum.gemspec +29 -0
- data/spec/mongoid/enum/validators/multiple_validator_spec.rb +80 -0
- data/spec/mongoid/enum_spec.rb +177 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/mongoid.yml +6 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmRmYzVkMTg5OTZiZjlmNDVkMjk2NDk1ODIxNDI0Zjc2NWZkNGNhMg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjU1MDU0MDJkZjE3YjIxM2M2MjY1NWE2NDNiN2UzNjMyOWY5Nzg4Nw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDU1Yjc2NmZjNTkxZTc1NTVkNjk2NjA0YTRkYjVjNzRjNDgyNzIzYzIzZmVl
|
10
|
+
NDNjZDQwMWJlZTNhZTI3OWYzYTg5M2NhMWRmZjNmYzFlYzM5MjY1MDc5NWY5
|
11
|
+
M2EyYmJjZjYwMThlZWI3Y2M1ZGUzZTA4YTUwNzc5ZWRlNWUxYzA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OGRmOWM1OGY2NDljNmUzOWJiNTEwMjA4NDBmODc0YjMyZDgzNmRkNWMyZDlj
|
14
|
+
MjVmNzE4MTY0Yzk0MDNhMjhjMWUwZGNkOGY3MDJhOGE3ODliZTY4OGVkZjA3
|
15
|
+
OTMxNWYwYTllOGRjZjA4YmRmNmJiNjk1ZGE5M2ZlYzVjNmIwMzE=
|
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,144 @@
|
|
1
|
+
# Mongoid::Enum
|
2
|
+
|
3
|
+
[](https://travis-ci.org/thetron/mongoid-enum)
|
5
|
+
|
6
|
+
Heavily inspired by [DHH's
|
7
|
+
ActiveRecord::Enum](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5), this little library is
|
8
|
+
just there to help you cut down the cruft in your models and make the
|
9
|
+
world a happier place at the same time.
|
10
|
+
|
11
|
+
A single line will get you fields, accessors, validations and scopes,
|
12
|
+
and a few other bits-and-bobs.
|
13
|
+
|
14
|
+
|
15
|
+
# Installation
|
16
|
+
|
17
|
+
Add this to your Gemfile:
|
18
|
+
|
19
|
+
gem "mongoid-enum"
|
20
|
+
|
21
|
+
And then run `bundle install`.
|
22
|
+
|
23
|
+
|
24
|
+
# Usage
|
25
|
+
|
26
|
+
````
|
27
|
+
class Payment
|
28
|
+
include Mongoid::Document
|
29
|
+
include Mongoid::Enum
|
30
|
+
|
31
|
+
enum :status, [:pending, :approved, :declined],
|
32
|
+
end
|
33
|
+
````
|
34
|
+
|
35
|
+
Aaaaaaand then you get things like:
|
36
|
+
|
37
|
+
````
|
38
|
+
payment = Payment.create
|
39
|
+
|
40
|
+
payment.status
|
41
|
+
# => :pending
|
42
|
+
|
43
|
+
payment.approved!
|
44
|
+
# => :approved
|
45
|
+
|
46
|
+
payment.pending?
|
47
|
+
# => :false
|
48
|
+
|
49
|
+
Payment.approved
|
50
|
+
# => Mongoid::Criteria for payments with status == :approved
|
51
|
+
|
52
|
+
````
|
53
|
+
|
54
|
+
# Features
|
55
|
+
|
56
|
+
## Field
|
57
|
+
|
58
|
+
Your enum value is stored as either a Symbol, or an Array (when storing
|
59
|
+
multiple values). The actual field name has a leading underscore (e.g.:
|
60
|
+
`_status`), but is also aliased with its actual name for you
|
61
|
+
convenience.
|
62
|
+
|
63
|
+
|
64
|
+
## Accessors
|
65
|
+
|
66
|
+
Your enums will get getters-and-setters with the same name. So using the
|
67
|
+
'Payment' example above:
|
68
|
+
|
69
|
+
````
|
70
|
+
payment.status = :declined
|
71
|
+
payment.status
|
72
|
+
# => :declined
|
73
|
+
````
|
74
|
+
|
75
|
+
And you also get bang(!) and query(?) methods for each of the values in
|
76
|
+
your enum (see [this example](#Usage).
|
77
|
+
|
78
|
+
|
79
|
+
## Constants
|
80
|
+
|
81
|
+
For each enum, you'll also get a constant named after it. This is to
|
82
|
+
help you elsewhere in your app, should you need to display, or leverage
|
83
|
+
the list of values. Using the above example:
|
84
|
+
|
85
|
+
````
|
86
|
+
Payment::STATUS
|
87
|
+
# => [:pending, :approved, :declined]
|
88
|
+
````
|
89
|
+
|
90
|
+
|
91
|
+
## Validations
|
92
|
+
|
93
|
+
Enum values are automatically validated against the list. You can
|
94
|
+
disable this behaviour (see (below)[#Options]).
|
95
|
+
|
96
|
+
|
97
|
+
## Scopes
|
98
|
+
|
99
|
+
A scope added for each of your enum's values. Using the example above,
|
100
|
+
you'd automatically get:
|
101
|
+
|
102
|
+
````
|
103
|
+
Payment.pending # => Mongoid::Criteria
|
104
|
+
Payment.approved # => Mongoid::Criteria
|
105
|
+
Payment.declined # => Mongoid::Criteria
|
106
|
+
````
|
107
|
+
|
108
|
+
|
109
|
+
# Options
|
110
|
+
|
111
|
+
## Default value
|
112
|
+
|
113
|
+
If not specified, the default will be the first in your list of values
|
114
|
+
(`:pending` in the example above). You can override this with the
|
115
|
+
`:default` option:
|
116
|
+
|
117
|
+
enum :roles, [:manager, :administrator], :default => ""
|
118
|
+
|
119
|
+
|
120
|
+
## Multiple values
|
121
|
+
|
122
|
+
Sometimes you'll need to store multiple values from your list, this
|
123
|
+
couldn't be easier:
|
124
|
+
|
125
|
+
enum, :roles => [:basic, :manager, :administrator], :multiple => true
|
126
|
+
|
127
|
+
user = User.create
|
128
|
+
user.roles << :basic
|
129
|
+
user.roles << :manager
|
130
|
+
user.save!
|
131
|
+
|
132
|
+
user.manager? # => true
|
133
|
+
user.administrator? # => false
|
134
|
+
user.roles # => [:basic, :manager]
|
135
|
+
|
136
|
+
|
137
|
+
## Validations
|
138
|
+
|
139
|
+
Validations are baked in by default, and ensure that the value(s) set in
|
140
|
+
your field are always from your list of options. If you need more
|
141
|
+
complex validations, or you just want to throw caution to the wind, you
|
142
|
+
can turn them off:
|
143
|
+
|
144
|
+
enum :status => [:up, :down], :validation => false
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mongoid/enum.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "mongoid/enum/version"
|
2
|
+
require "mongoid/enum/validators/multiple_validator"
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
module Enum
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
module ClassMethods
|
8
|
+
def enum(name, values, options = {})
|
9
|
+
field_name = :"_#{name}"
|
10
|
+
const_name = name.to_s.upcase
|
11
|
+
multiple = options[:multiple] || false
|
12
|
+
default = options[:default].nil? && values.first || options[:default]
|
13
|
+
required = options[:required].nil? || options[:required]
|
14
|
+
|
15
|
+
const_set const_name, values
|
16
|
+
|
17
|
+
type = multiple && Array || Symbol
|
18
|
+
field field_name, :type => type, :default => default
|
19
|
+
alias_attribute name, field_name
|
20
|
+
|
21
|
+
if multiple
|
22
|
+
validates field_name, :'mongoid/enum/validators/multiple' => { :in => values, :allow_nil => !required }
|
23
|
+
else
|
24
|
+
validates field_name, :inclusion => {:in => values}, :allow_nil => !required
|
25
|
+
end
|
26
|
+
|
27
|
+
values.each do |value|
|
28
|
+
scope value, where(field_name => value)
|
29
|
+
|
30
|
+
if multiple
|
31
|
+
class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
|
32
|
+
class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
|
33
|
+
else
|
34
|
+
class_eval "def #{value}?() self.#{field_name} == :#{value} end"
|
35
|
+
class_eval "def #{value}!() update_attributes! :#{field_name} => :#{value} end"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Enum
|
3
|
+
module Validators
|
4
|
+
class MultipleValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, values)
|
6
|
+
values = Array(values)
|
7
|
+
|
8
|
+
if options[:allow_nil]
|
9
|
+
add_error_message record, attribute if !all_included?(values, options[:in])
|
10
|
+
else
|
11
|
+
add_error_message record, attribute if values.empty? || !all_included?(values, options[:in])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_error_message(record, attribute)
|
16
|
+
record.errors[attribute] << (options[:message] || "is not in #{options[:in].join ", "}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def all_included?(values, allowed)
|
20
|
+
(values - allowed).empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.kind
|
24
|
+
:custom
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
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"
|
8
|
+
spec.version = Mongoid::Enum::VERSION
|
9
|
+
spec.authors = ["Nicholas Bruning"]
|
10
|
+
spec.email = ["nicholas@bruning.com.au"]
|
11
|
+
spec.description = %q{Heavily inspired by DDH's ActiveRecord::Enum, this little library is just 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 = ""
|
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_dependency "mongoid", "~>3.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
26
|
+
spec.add_development_dependency "guard-rspec", "~> 4.0.3"
|
27
|
+
spec.add_development_dependency "database_cleaner", "~> 1.2.0"
|
28
|
+
spec.add_development_dependency "mongoid-rspec", "~> 1.5.1"
|
29
|
+
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,177 @@
|
|
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}" }
|
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).to custom_validate(multiple_field_name).with_validator(Mongoid::Enum::Validators::MultipleValidator)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when not multiple" do
|
42
|
+
it "is a symbol" do
|
43
|
+
expect(klass).to have_field(field_name).of_type(Symbol)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "validates inclusion in values" do
|
47
|
+
expect(klass).to validate_inclusion_of(field_name).to_allow(values)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "constant" do
|
54
|
+
it "is set to the values" do
|
55
|
+
expect(klass::STATUS).to eq values
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "accessors"do
|
60
|
+
context "when singular" do
|
61
|
+
describe "{{value}}!" do
|
62
|
+
it "sets the value" do
|
63
|
+
instance.save
|
64
|
+
instance.banned!
|
65
|
+
expect(instance.status).to eq :banned
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "{{value}}?" do
|
70
|
+
context "when {{enum}} == {{value}}" do
|
71
|
+
it "returns true" do
|
72
|
+
instance.save
|
73
|
+
instance.banned!
|
74
|
+
expect(instance.banned?).to eq true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
context "when {{enum}} != {{value}}" do
|
78
|
+
it "returns false" do
|
79
|
+
instance.save
|
80
|
+
instance.banned!
|
81
|
+
expect(instance.approved?).to eq false
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when multiple" do
|
88
|
+
describe "{{value}}!" do
|
89
|
+
context "when field is nil" do
|
90
|
+
it "creates an array containing the value" do
|
91
|
+
instance.roles = nil
|
92
|
+
instance.save
|
93
|
+
instance.author!
|
94
|
+
expect(instance.roles).to eq [:author]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when field is not nil" do
|
99
|
+
it "appends the value" do
|
100
|
+
instance.save
|
101
|
+
instance.author!
|
102
|
+
instance.editor!
|
103
|
+
expect(instance.roles).to eq [:author, :editor]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "{{value}}?" do
|
109
|
+
context "when {{enum}} contains {{value}}" do
|
110
|
+
it "returns true" do
|
111
|
+
instance.save
|
112
|
+
instance.author!
|
113
|
+
instance.editor!
|
114
|
+
expect(instance.editor?).to be_true
|
115
|
+
expect(instance.author?).to be_true
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "when {{enum}} does not contain {{value}}" do
|
120
|
+
it "returns false" do
|
121
|
+
instance.save
|
122
|
+
expect(instance.author?).to be_false
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "scopes" do
|
130
|
+
context "when singular" do
|
131
|
+
it "returns the corresponding documents" do
|
132
|
+
instance.save
|
133
|
+
instance.banned!
|
134
|
+
expect(User.banned.to_a).to eq [instance]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when multiple" do
|
139
|
+
context "and only one document" do
|
140
|
+
it "returns that document" do
|
141
|
+
instance.save
|
142
|
+
instance.author!
|
143
|
+
instance.editor!
|
144
|
+
expect(User.author.to_a).to eq [instance]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "and more than one document" do
|
149
|
+
it "returns all documents with those values" do
|
150
|
+
instance.save
|
151
|
+
instance.author!
|
152
|
+
instance.editor!
|
153
|
+
instance2 = klass.create
|
154
|
+
instance2.author!
|
155
|
+
expect(User.author.to_a).to eq [instance, instance2]
|
156
|
+
expect(User.editor.to_a).to eq [instance]
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "default values" do
|
163
|
+
context "when not specified" do
|
164
|
+
it "uses the first value" do
|
165
|
+
instance.save
|
166
|
+
expect(instance.status).to eq values.first
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context "when specified" do
|
171
|
+
it "uses the specified value" do
|
172
|
+
instance.save
|
173
|
+
expect(instance.roles).to eq []
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$: << 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
|
+
DatabaseCleaner.clean
|
18
|
+
Mongoid.purge!
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Mongoid.load!(File.expand_path("../support/mongoid.yml", __FILE__), :test)
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Bruning
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.0.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.3
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.2.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.2.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: mongoid-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.5.1
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.5.1
|
111
|
+
description: Heavily inspired by DDH's ActiveRecord::Enum, this little library is
|
112
|
+
just there to help you cut down the cruft in your models and make the world a happier
|
113
|
+
place at the same time.
|
114
|
+
email:
|
115
|
+
- nicholas@bruning.com.au
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- .autotest
|
121
|
+
- .gitignore
|
122
|
+
- .rspec
|
123
|
+
- .travis.yml
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- lib/mongoid/enum.rb
|
129
|
+
- lib/mongoid/enum/validators/multiple_validator.rb
|
130
|
+
- lib/mongoid/enum/version.rb
|
131
|
+
- mongoid-enum.gemspec
|
132
|
+
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
133
|
+
- spec/mongoid/enum_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/support/mongoid.yml
|
136
|
+
homepage: ''
|
137
|
+
licenses:
|
138
|
+
- MIT
|
139
|
+
metadata: {}
|
140
|
+
post_install_message:
|
141
|
+
rdoc_options: []
|
142
|
+
require_paths:
|
143
|
+
- lib
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ! '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
requirements: []
|
155
|
+
rubyforge_project:
|
156
|
+
rubygems_version: 2.1.1
|
157
|
+
signing_key:
|
158
|
+
specification_version: 4
|
159
|
+
summary: Sweet enum sugar for your Mongoid documents
|
160
|
+
test_files:
|
161
|
+
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
162
|
+
- spec/mongoid/enum_spec.rb
|
163
|
+
- spec/spec_helper.rb
|
164
|
+
- spec/support/mongoid.yml
|