power_enum 2.12.0 → 3.4.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 +5 -5
- data/README.markdown +16 -6
- data/lib/generators/enum/templates/rails31_migration.rb.erb +1 -1
- data/lib/power_enum/enumerated.rb +18 -3
- data/lib/power_enum/has_enumerated.rb +1 -11
- data/lib/power_enum/reflection.rb +36 -32
- metadata +22 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e64bc976c66ec97d00cd14baabeb0fa7c13afc66500bb8408bc9d1e884be30ee
|
4
|
+
data.tar.gz: 43f0a3fa296b90154d32b82a38d8ae207b0b8f400edc821ded353e341ddb5187
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b36b482f22cf0ddc61e817f77838a245a250a3c28771c0d0e39d8e23f29b782abd93fbb06c84fa6b009f66bdb510933b0e292a8ceeed60d190caeeeba24d05f2
|
7
|
+
data.tar.gz: 2731947e3bb7d5f9b91fffd726db29ab26451536fc60b8110886d2d8ed3402af9f8f0d10471bea00c83f36e4577476df2fb1c2cdebe106b74d684e80d7637d33
|
data/README.markdown
CHANGED
@@ -10,7 +10,8 @@ Enumerations for Rails Done Right.
|
|
10
10
|
|
11
11
|
## Versions
|
12
12
|
|
13
|
-
* PowerEnum
|
13
|
+
* PowerEnum 3.X (this version) supports Rails 4.2, Rails 5.X and Rails 6.0
|
14
|
+
* PowerEnum 2.X supports Rails 4.X and Rails 5.0
|
14
15
|
* PowerEnum 1.X supports Rails 3.1/3.2, available here: https://github.com/albertosaurus/power_enum
|
15
16
|
|
16
17
|
## What is this?:
|
@@ -50,10 +51,15 @@ See "How to use it" below for more information.
|
|
50
51
|
|
51
52
|
## Requirements
|
52
53
|
|
54
|
+
### PowerEnum 3.X
|
55
|
+
|
56
|
+
* Ruby 2.1 or later (JRuby should work but isn't extensively tested; Travis is being difficult).
|
57
|
+
* Rails 4.2, 5.0, 5.1, 5.2, 6.0
|
58
|
+
|
53
59
|
### PowerEnum 2.X
|
54
60
|
|
55
61
|
* Ruby 1.9.3, 2.0, JRuby 1.7+ (Ruby 1.9.3 or 2.0 required for development)
|
56
|
-
* Rails 4.0
|
62
|
+
* Rails 4.0, 4.1, 4.2, 5.0
|
57
63
|
|
58
64
|
## Installation
|
59
65
|
|
@@ -192,7 +198,7 @@ create_table :bookings do |t|
|
|
192
198
|
t.timestamps
|
193
199
|
end
|
194
200
|
```
|
195
|
-
|
201
|
+
|
196
202
|
There are two methods added to Rails migrations:
|
197
203
|
|
198
204
|
##### create\_enum(enum\_name, options = {}, &block)
|
@@ -279,7 +285,7 @@ Example:
|
|
279
285
|
```ruby
|
280
286
|
remove_enum :booking_status
|
281
287
|
```
|
282
|
-
|
288
|
+
|
283
289
|
is the equivalent of
|
284
290
|
|
285
291
|
```ruby
|
@@ -335,6 +341,10 @@ other value.
|
|
335
341
|
`BookingStatus.all` returns an array of all BookingStatus records that match the `:conditions` specified in
|
336
342
|
`acts_as_enumerated`, in the order specified by `:order`.
|
337
343
|
|
344
|
+
##### all_except(*items)
|
345
|
+
|
346
|
+
`BookingStatus.all_except(arg1, arg2)` returns an array of all BookingStatus records with the given items filtered out.
|
347
|
+
|
338
348
|
##### active
|
339
349
|
|
340
350
|
`BookingStatus.active` returns an array of all BookingStatus records that are marked active. See the `active?` instance
|
@@ -778,7 +788,7 @@ Execute the test setup script:
|
|
778
788
|
#### Manually (if required)
|
779
789
|
|
780
790
|
Go to the 'dummy' project:
|
781
|
-
|
791
|
+
|
782
792
|
cd ./spec/dummy
|
783
793
|
|
784
794
|
If this is your first time, create the test database
|
@@ -802,7 +812,7 @@ Go back to gem root directory:
|
|
802
812
|
* Initial Version Copyright (c) 2005 Trevor Squires
|
803
813
|
* Rails 3 Updates Copyright (c) 2010 Pivotal Labs
|
804
814
|
* Initial Test Suite Copyright (c) 2011 Sergey Potapov
|
805
|
-
* Subsequent Updates Copyright (c) 2011-
|
815
|
+
* Subsequent Updates Copyright (c) 2011-2020 Arthur Shagall
|
806
816
|
|
807
817
|
Released under the MIT License. See the LICENSE file for more details.
|
808
818
|
|
@@ -126,6 +126,7 @@ module PowerEnum::Enumerated
|
|
126
126
|
before_save :enumeration_model_update
|
127
127
|
before_destroy :enumeration_model_update
|
128
128
|
validates acts_enumerated_name_column, :presence => true, :uniqueness => true
|
129
|
+
validate :validate_enumeration_model_updates_permitted
|
129
130
|
|
130
131
|
define_method :__enum_name__ do
|
131
132
|
read_attribute(acts_enumerated_name_column).to_s
|
@@ -193,6 +194,11 @@ module PowerEnum::Enumerated
|
|
193
194
|
all.map { |item| item.name_sym }
|
194
195
|
end
|
195
196
|
|
197
|
+
# Returns all except for the given list
|
198
|
+
def all_except(*excluded)
|
199
|
+
all.find_all { |item| !(item === excluded) }
|
200
|
+
end
|
201
|
+
|
196
202
|
# Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is
|
197
203
|
# an enum instance. Passing in a list of arguments returns a list of
|
198
204
|
# enums. When called with no arguments, returns nil.
|
@@ -489,16 +495,25 @@ module PowerEnum::Enumerated
|
|
489
495
|
# and rather than completely disallow changes I make you jump
|
490
496
|
# through an extra hoop just in case you're defining your enumeration
|
491
497
|
# values in Migrations. I.e. set enumeration_model_updates_permitted = true
|
492
|
-
def enumeration_model_update
|
498
|
+
private def enumeration_model_update
|
493
499
|
if self.class.enumeration_model_updates_permitted
|
494
500
|
self.class.purge_enumerations_cache
|
495
501
|
true
|
496
502
|
else
|
497
503
|
# Ugh. This just seems hack-ish. I wonder if there's a better way.
|
504
|
+
if Rails.version =~ /^4\.2\.*/
|
505
|
+
false
|
506
|
+
else
|
507
|
+
throw(:abort)
|
508
|
+
end
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
# Validates that model updates are enabled.
|
513
|
+
private def validate_enumeration_model_updates_permitted
|
514
|
+
unless self.class.enumeration_model_updates_permitted
|
498
515
|
self.errors.add(self.class.name_column, "changes to acts_as_enumeration model instances are not permitted")
|
499
|
-
false
|
500
516
|
end
|
501
517
|
end
|
502
|
-
private :enumeration_model_update
|
503
518
|
end # module EnumInstanceMethods
|
504
519
|
end # module PowerEnum::Enumerated
|
@@ -125,17 +125,7 @@ module PowerEnum::HasEnumerated
|
|
125
125
|
def create_ar_reflection(part_id, options)
|
126
126
|
reflection = PowerEnum::Reflection::EnumerationReflection.new(part_id, options, self)
|
127
127
|
|
128
|
-
|
129
|
-
if self.respond_to? :_reflections=
|
130
|
-
if Rails.version =~ /^4\.2\.*/ || Rails.version =~ /^5\.0\.*/
|
131
|
-
self._reflections = self._reflections.merge(part_id.to_s => reflection)
|
132
|
-
else
|
133
|
-
self._reflections = self._reflections.merge(part_id => reflection)
|
134
|
-
end
|
135
|
-
|
136
|
-
else
|
137
|
-
self.reflections = self.reflections.merge(part_id => reflection)
|
138
|
-
end
|
128
|
+
self._reflections = self._reflections.merge(part_id.to_s => reflection)
|
139
129
|
reflection
|
140
130
|
end
|
141
131
|
private :create_ar_reflection
|
@@ -17,11 +17,7 @@ module PowerEnum::Reflection
|
|
17
17
|
# the reflection, otherwise returns nil.
|
18
18
|
# @return [PowerEnum::Reflection::EnumerationReflection]
|
19
19
|
def reflect_on_enumerated( enumerated )
|
20
|
-
key =
|
21
|
-
enumerated.to_s
|
22
|
-
else
|
23
|
-
enumerated.to_sym
|
24
|
-
end
|
20
|
+
key = enumerated.to_s
|
25
21
|
reflections[key].is_a?(PowerEnum::Reflection::EnumerationReflection) ? reflections[key] : nil
|
26
22
|
end
|
27
23
|
|
@@ -39,47 +35,48 @@ module PowerEnum::Reflection
|
|
39
35
|
attr_accessor :parent_reflection
|
40
36
|
|
41
37
|
# See ActiveRecord::Reflection::MacroReflection
|
42
|
-
def initialize(
|
43
|
-
|
44
|
-
super name, nil, options, active_record
|
45
|
-
else
|
46
|
-
super :has_enumerated, name, nil, options, active_record
|
47
|
-
end
|
38
|
+
def initialize(name, options, active_record)
|
39
|
+
super name, nil, options, active_record
|
48
40
|
end
|
49
41
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
42
|
+
def macro
|
43
|
+
:has_enumerated
|
44
|
+
end
|
54
45
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
46
|
+
def check_preloadable!
|
47
|
+
return unless scope
|
48
|
+
if scope.arity > 0
|
49
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
59
50
|
The association scope '#{name}' is instance dependent (the scope
|
60
51
|
block takes an argument). Preloading happens before the individual
|
61
52
|
instances are created. This means that there is no instance being
|
62
53
|
passed to the association scope. This will most likely result in
|
63
54
|
broken or incorrect behavior. Joining, Preloading and eager loading
|
64
55
|
of these associations is deprecated and will be removed in the future.
|
65
|
-
|
66
|
-
end
|
56
|
+
MSG
|
67
57
|
end
|
68
|
-
|
58
|
+
end
|
69
59
|
|
70
|
-
|
71
|
-
@active_record_primary_key ||= options[:primary_key] || active_record.primary_key
|
72
|
-
end
|
60
|
+
alias :check_eager_loadable! :check_preloadable!
|
73
61
|
|
74
|
-
|
75
|
-
|
76
|
-
|
62
|
+
def active_record_primary_key
|
63
|
+
@active_record_primary_key ||= options[:primary_key] || active_record.primary_key
|
64
|
+
end
|
77
65
|
|
78
|
-
|
66
|
+
alias_method :join_primary_key, :active_record_primary_key
|
79
67
|
|
80
|
-
|
81
|
-
|
82
|
-
|
68
|
+
def klass
|
69
|
+
@klass ||= active_record.send(:compute_type, class_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
def association_class
|
73
|
+
::ActiveRecord::Associations::HasOneAssociation
|
74
|
+
end
|
75
|
+
|
76
|
+
EnumJoinKeys = Struct.new(:key, :foreign_key)
|
77
|
+
|
78
|
+
def join_keys(*_)
|
79
|
+
EnumJoinKeys.new(active_record_primary_key, foreign_key)
|
83
80
|
end
|
84
81
|
|
85
82
|
# Returns the class name of the enum
|
@@ -92,6 +89,8 @@ of these associations is deprecated and will be removed in the future.
|
|
92
89
|
@foreign_key ||= (@options[:foreign_key] || "#{@name}_id").to_s
|
93
90
|
end
|
94
91
|
|
92
|
+
alias_method :join_foreign_key, :foreign_key
|
93
|
+
|
95
94
|
# Returns the name of the enum table
|
96
95
|
def table_name
|
97
96
|
@table_name ||= self.class.const_get(class_name).table_name
|
@@ -121,6 +120,11 @@ of these associations is deprecated and will be removed in the future.
|
|
121
120
|
false
|
122
121
|
end
|
123
122
|
|
123
|
+
# Always returns true.
|
124
|
+
def collection?
|
125
|
+
true
|
126
|
+
end
|
127
|
+
|
124
128
|
# In this case, returns [[]]
|
125
129
|
def conditions
|
126
130
|
[[]]
|
metadata
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trevor Squires
|
8
8
|
- Pivotal Labs
|
9
9
|
- Arthur Shagall
|
10
10
|
- Sergey Potapov
|
11
|
-
autorequire:
|
11
|
+
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2020-06-16 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
|
-
- - "
|
20
|
+
- - ">"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '1.
|
22
|
+
version: '1.7'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- - "
|
27
|
+
- - ">"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '1.
|
29
|
+
version: '1.7'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rake
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -61,60 +61,60 @@ dependencies:
|
|
61
61
|
requirements:
|
62
62
|
- - ">="
|
63
63
|
- !ruby/object:Gem::Version
|
64
|
-
version: '4.
|
64
|
+
version: '4.2'
|
65
65
|
- - "<"
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
67
|
+
version: '7'
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
71
71
|
requirements:
|
72
72
|
- - ">="
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
version: '4.
|
74
|
+
version: '4.2'
|
75
75
|
- - "<"
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version: '
|
77
|
+
version: '7'
|
78
78
|
- !ruby/object:Gem::Dependency
|
79
79
|
name: railties
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|
82
82
|
- - ">="
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
version: '4.
|
84
|
+
version: '4.2'
|
85
85
|
- - "<"
|
86
86
|
- !ruby/object:Gem::Version
|
87
|
-
version: '
|
87
|
+
version: '7'
|
88
88
|
type: :runtime
|
89
89
|
prerelease: false
|
90
90
|
version_requirements: !ruby/object:Gem::Requirement
|
91
91
|
requirements:
|
92
92
|
- - ">="
|
93
93
|
- !ruby/object:Gem::Version
|
94
|
-
version: '4.
|
94
|
+
version: '4.2'
|
95
95
|
- - "<"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: '
|
97
|
+
version: '7'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: activerecord
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
102
|
- - ">="
|
103
103
|
- !ruby/object:Gem::Version
|
104
|
-
version: '4.
|
104
|
+
version: '4.2'
|
105
105
|
- - "<"
|
106
106
|
- !ruby/object:Gem::Version
|
107
|
-
version: '
|
107
|
+
version: '7'
|
108
108
|
type: :runtime
|
109
109
|
prerelease: false
|
110
110
|
version_requirements: !ruby/object:Gem::Requirement
|
111
111
|
requirements:
|
112
112
|
- - ">="
|
113
113
|
- !ruby/object:Gem::Version
|
114
|
-
version: '4.
|
114
|
+
version: '4.2'
|
115
115
|
- - "<"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '7'
|
118
118
|
description: |
|
119
119
|
Power Enum allows you to treat instances of your ActiveRecord models as though they were an enumeration of values.
|
120
120
|
It allows you to cleanly solve many of the problems that the traditional Rails alternatives handle poorly if at all.
|
@@ -149,7 +149,7 @@ homepage: http://github.com/albertosaurus/power_enum_2
|
|
149
149
|
licenses:
|
150
150
|
- MIT
|
151
151
|
metadata: {}
|
152
|
-
post_install_message:
|
152
|
+
post_install_message:
|
153
153
|
rdoc_options: []
|
154
154
|
require_paths:
|
155
155
|
- lib
|
@@ -164,9 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: '0'
|
166
166
|
requirements: []
|
167
|
-
|
168
|
-
|
169
|
-
signing_key:
|
167
|
+
rubygems_version: 3.0.8
|
168
|
+
signing_key:
|
170
169
|
specification_version: 4
|
171
170
|
summary: Allows you to treat instances of your ActiveRecord models as though they
|
172
171
|
were an enumeration of values
|