mongoid_enum 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/mongoid/enum.rb +15 -2
- data/lib/mongoid/enum/invalid_key.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ac0771a33322e56736c4377c96af138033bd23a
|
4
|
+
data.tar.gz: 404023e3a923cce6ac08aee8a88fe13f522b26a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1f86793cab3fa90274bbf9f9c8018758ffcbfa4a886e00d1c144669d6605af22fed744b23918d33950a33bc10fca7006357c7bff7e0f2ba45e7f3f5209bfb29
|
7
|
+
data.tar.gz: eb1dd4b059b0b5f55ac8c57a21662d48111faaab8f33f5c25cec189b0340854c4b6bde6ec738d61d1a4e7bb028dbede175ccb8afb8377460ffe2f94b9a6d94ff
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# Mongoid::Enum
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/amw/mongoid_enum.svg?branch=master)](https://travis-ci.org/amw/mongoid_enum)
|
2
4
|
|
3
5
|
Enum implementation for Mongoid. Similar to that found in ActiveRecord.
|
4
6
|
|
@@ -52,6 +54,17 @@ Constant name is a pluralized field name set in SCREAMING\_SNAKE\_CASE.
|
|
52
54
|
Part::QUALITY_CONTROLS["passed"] # true
|
53
55
|
Part::QUALITY_CONTROLS[:failed] # false
|
54
56
|
|
57
|
+
You might prefer to use plural scopes if your field values are nouns:
|
58
|
+
|
59
|
+
class Attachment
|
60
|
+
include Mongoid::Document
|
61
|
+
include Mongoid::Enum
|
62
|
+
|
63
|
+
enum type: %w{image video}, _plural_scopes: true
|
64
|
+
end
|
65
|
+
|
66
|
+
Attachment.videos.count
|
67
|
+
|
55
68
|
Read more in [documentation](http://www.rubydoc.info/gems/mongoid_enum/Mongoid/Enum).
|
56
69
|
|
57
70
|
# Differences from ActiveRecord
|
data/lib/mongoid/enum.rb
CHANGED
@@ -57,6 +57,17 @@ module Mongoid # :nodoc:
|
|
57
57
|
# Conversation.where(status: [:active, :archived])
|
58
58
|
# Conversation.not.where(status: :active)
|
59
59
|
#
|
60
|
+
# If your field values are nouns you might prefer to have your scopes pluralized:
|
61
|
+
#
|
62
|
+
# class Attachment
|
63
|
+
# include Mongoid::Document
|
64
|
+
# include Mongoid::Enum
|
65
|
+
#
|
66
|
+
# enum type: %w{image video}, _plural_scopes: true
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# Attachment.videos.count
|
70
|
+
#
|
60
71
|
#
|
61
72
|
# Defining an enum automatically adds a validator on its field. Assigning values
|
62
73
|
# not included in enum definition will make the document invalid.
|
@@ -133,6 +144,7 @@ module Mongoid # :nodoc:
|
|
133
144
|
enum_prefix = definitions.delete(:_prefix)
|
134
145
|
enum_suffix = definitions.delete(:_suffix)
|
135
146
|
default_key = definitions.delete(:_default)
|
147
|
+
pluralize = definitions.delete(:_plural_scopes)
|
136
148
|
|
137
149
|
definitions.each do |name, values|
|
138
150
|
enum_values = ActiveSupport::HashWithIndifferentAccess.new
|
@@ -187,6 +199,7 @@ module Mongoid # :nodoc:
|
|
187
199
|
end
|
188
200
|
|
189
201
|
value_method_name = "#{prefix}#{key}#{suffix}"
|
202
|
+
scope_name = pluralize ? value_method_name.pluralize : value_method_name
|
190
203
|
|
191
204
|
# def active?() status == 0 end
|
192
205
|
klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
|
@@ -197,8 +210,8 @@ module Mongoid # :nodoc:
|
|
197
210
|
define_method("#{value_method_name}!") { update! name => key }
|
198
211
|
|
199
212
|
# scope :active, -> { where status: 0 }
|
200
|
-
klass.send(:detect_enum_conflict!, name,
|
201
|
-
klass.scope
|
213
|
+
klass.send(:detect_enum_conflict!, name, scope_name, true)
|
214
|
+
klass.scope scope_name, -> { klass.where name => key }
|
202
215
|
end
|
203
216
|
end
|
204
217
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Wróbel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -104,9 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
version: '0'
|
105
105
|
requirements: []
|
106
106
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
107
|
+
rubygems_version: 2.5.1
|
108
108
|
signing_key:
|
109
109
|
specification_version: 4
|
110
110
|
summary: Enum fields for Mongoid
|
111
111
|
test_files: []
|
112
|
-
has_rdoc:
|