mongoid-enum_attribute 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +2 -2
- data/lib/mongoid/enum_attribute.rb +12 -8
- data/lib/mongoid/enum_attribute/version.rb +1 -1
- data/mongoid-enum_attribute.gemspec +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 672283b84f349e0f148fe27f108986935c5b56919a571c306ed604c34ba05131
|
4
|
+
data.tar.gz: 2e0bc5be5274b6effb9975b802dea9abe6f1bdc95296da8e31877499dd57c5ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5dbe677cfbd240bbe3523c6c3ce401f19a488214303d7e32712445b9772c2c2d522001762cb41768da8d7cc23b4665daae966a492a9209b82ed3acd832ae836c
|
7
|
+
data.tar.gz: 34c01c435fed78d549d1140bcfae91fb9c70237e1c886fdc5d22a22cf7e21f93314308534835788dcb350b6b8ebe0e66aad8e8d2b7f36936923f1b3fbe35a45b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
|
|
25
25
|
```ruby
|
26
26
|
class Payment
|
27
27
|
include Mongoid::Document
|
28
|
-
include Mongoid::
|
28
|
+
include Mongoid::EnumAttribute
|
29
29
|
|
30
30
|
enum :status, [:pending, :approved, :declined]
|
31
31
|
end
|
@@ -101,4 +101,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
101
101
|
|
102
102
|
## Contributing
|
103
103
|
|
104
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tomasc/mongoid-enum_attribute.
|
@@ -8,11 +8,10 @@ module Mongoid
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def enum(name, values, options = {})
|
11
|
-
field_name = "#{Mongoid::EnumAttribute.configuration.field_name_prefix}#{name}"
|
12
11
|
options = default_options(values).merge(options)
|
13
12
|
|
14
13
|
set_values_constant(name, values)
|
15
|
-
create_field(
|
14
|
+
field_name = create_field(name, options)
|
16
15
|
|
17
16
|
create_validations(field_name, values, options)
|
18
17
|
define_value_scopes_and_accessors(name, field_name, values, options)
|
@@ -27,7 +26,9 @@ module Mongoid
|
|
27
26
|
required: true,
|
28
27
|
validate: true,
|
29
28
|
prefix: Mongoid::EnumAttribute.configuration.prefix,
|
30
|
-
suffix: Mongoid::EnumAttribute.configuration.suffix
|
29
|
+
suffix: Mongoid::EnumAttribute.configuration.suffix,
|
30
|
+
field_name_prefix: Mongoid::EnumAttribute.configuration.field_name_prefix,
|
31
|
+
field_name: nil
|
31
32
|
}
|
32
33
|
end
|
33
34
|
|
@@ -36,9 +37,12 @@ module Mongoid
|
|
36
37
|
const_set(const_name, values)
|
37
38
|
end
|
38
39
|
|
39
|
-
def create_field(
|
40
|
-
type = options[:multiple] && Array ||
|
40
|
+
def create_field(name, options)
|
41
|
+
type = options[:multiple] && Array || String
|
42
|
+
field_name = (options[:field_name] || "#{options[:field_name_prefix]}#{name}").to_s
|
43
|
+
raise "enum name #{name} and field_name option cannot be equal!" if name.to_s == field_name.to_s
|
41
44
|
field field_name, type: type, default: options[:default]
|
45
|
+
field_name
|
42
46
|
end
|
43
47
|
|
44
48
|
def create_validations(field_name, values, options)
|
@@ -53,7 +57,7 @@ module Mongoid
|
|
53
57
|
elsif options[:validate]
|
54
58
|
validates(
|
55
59
|
field_name,
|
56
|
-
inclusion: { in: values.map(&:
|
60
|
+
inclusion: { in: values.map(&:to_s) },
|
57
61
|
allow_nil: !options[:required]
|
58
62
|
)
|
59
63
|
end
|
@@ -100,7 +104,7 @@ module Mongoid
|
|
100
104
|
|
101
105
|
def define_string_field_accessor(name, field_name)
|
102
106
|
class_eval "def #{name}=(val) self.write_attribute(:#{field_name}, val && val.to_sym || nil) end"
|
103
|
-
class_eval "def #{name}() self.send(:#{field_name}) end"
|
107
|
+
class_eval "def #{name}() self.send(:#{field_name}).to_sym end"
|
104
108
|
end
|
105
109
|
|
106
110
|
def define_array_accessor(accessor_name, field_name, value)
|
@@ -109,7 +113,7 @@ module Mongoid
|
|
109
113
|
end
|
110
114
|
|
111
115
|
def define_string_accessor(accessor_name, field_name, value)
|
112
|
-
class_eval "def #{accessor_name}?() self.#{field_name} == :#{value} end"
|
116
|
+
class_eval "def #{accessor_name}?() self.#{field_name}.to_sym == :#{value} end"
|
113
117
|
class_eval "def #{accessor_name}!() update_attributes! :#{field_name} => :#{value} end"
|
114
118
|
end
|
115
119
|
end
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_dependency 'mongoid', '>= 5', '< 8'
|
23
23
|
|
24
|
-
spec.add_development_dependency "bundler", "
|
24
|
+
spec.add_development_dependency "bundler", ">= 1.16"
|
25
25
|
spec.add_development_dependency "minitest", "~> 5.0"
|
26
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
27
27
|
spec.add_development_dependency "database_cleaner"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-enum_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomáš Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -34,14 +34,14 @@ dependencies:
|
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '1.16'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '1.16'
|
47
47
|
- !ruby/object:Gem::Dependency
|
@@ -124,8 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
126
|
requirements: []
|
127
|
-
|
128
|
-
rubygems_version: 2.7.3
|
127
|
+
rubygems_version: 3.0.3
|
129
128
|
signing_key:
|
130
129
|
specification_version: 4
|
131
130
|
summary: Take on ActiveRecord::Enum in Mongoid
|