power_enum 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -1
- data/lib/active_record/acts/enumerated.rb +25 -14
- data/lib/active_record/aggregations/has_enumerated.rb +9 -1
- metadata +5 -2
data/README.md
CHANGED
@@ -225,7 +225,7 @@ With that, your BookingStatus class will have the following methods defined:
|
|
225
225
|
|
226
226
|
#### Class Methods
|
227
227
|
|
228
|
-
##### [](
|
228
|
+
##### [](*args)
|
229
229
|
|
230
230
|
`BookingStatus[arg]` performs a lookup for the BookingStatus instance for the given arg. The arg value can be a
|
231
231
|
'string' or a :symbol, in which case the lookup will be against the BookingStatus.name field. Alternatively arg can be
|
@@ -242,6 +242,10 @@ The purpose of the `:on_lookup_failure` option is that a) under some circumstanc
|
|
242
242
|
action should be taken,
|
243
243
|
therefore b) a fallback action should be easily configurable.
|
244
244
|
|
245
|
+
As of version 0.8.0, you can pass in multiple arguments to `[]`. This returns a list of enums corresponding to the
|
246
|
+
passed in values. Duplicates are filtered out. For example `BookingStatus[arg1, arg2, arg3]` would be equivalent to
|
247
|
+
`[BookingStatus[arg1], BookingStatus[arg2], BookingStatus[arg3]]`.
|
248
|
+
|
245
249
|
##### all
|
246
250
|
|
247
251
|
`BookingStatus.all` returns an array of all BookingStatus records that match the `:conditions` specified in
|
@@ -417,6 +421,19 @@ recommended for obvious reasons.
|
|
417
421
|
|
418
422
|
As of version 0.5.5, it also aliases a pluralized version of the scope, i.e. `:with_statuses`
|
419
423
|
|
424
|
+
#### exclude\_enumerated\_attribute scope
|
425
|
+
|
426
|
+
As of version 0.8.0, a scope for the inverse of `with_enumerated_attribute` is created, unless the `:create_scope`
|
427
|
+
option is set to `false`. As a result, this allows us to do things like
|
428
|
+
|
429
|
+
Booking.exclude_status :received
|
430
|
+
|
431
|
+
This will give us all the Bookings where the status is a value other than `BookingStatus[:received]`.
|
432
|
+
|
433
|
+
NOTE: This will NOT pick up instances of Booking where status is nil.
|
434
|
+
|
435
|
+
A pluralized version of the scope is also created, so `Booking.exclude_statuses :received, :confirmed` is valid.
|
436
|
+
|
420
437
|
### ActiveRecord::Base Extensions
|
421
438
|
|
422
439
|
The following methods are added to ActiveRecord::Base as class methods.
|
@@ -527,3 +544,11 @@ And finally run tests:
|
|
527
544
|
* Subsequent Updates Copyright (c) 2011 Arthur Shagall
|
528
545
|
|
529
546
|
Released under the MIT License. See the LICENSE file for more details.
|
547
|
+
|
548
|
+
## Contributing
|
549
|
+
|
550
|
+
Contributions are welcome. However, before issuing a pull request, please make sure of the following:
|
551
|
+
|
552
|
+
* All specs are passing.
|
553
|
+
* Any new features have test coverage.
|
554
|
+
* Anything that breaks backward compatibility has a very good reason for doing so.
|
@@ -118,23 +118,34 @@ module ActiveRecord
|
|
118
118
|
end
|
119
119
|
|
120
120
|
# Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is
|
121
|
-
# an enum instance.
|
122
|
-
def [](
|
123
|
-
case
|
124
|
-
when
|
125
|
-
return_val = lookup_name(arg.id2name) and return return_val
|
126
|
-
when String
|
127
|
-
return_val = lookup_name(arg) and return return_val
|
128
|
-
when Fixnum
|
129
|
-
return_val = lookup_id(arg) and return return_val
|
130
|
-
when self
|
131
|
-
return arg
|
132
|
-
when nil
|
121
|
+
# an enum instance. Passing in a list of arguments returns a list of enums.
|
122
|
+
def [](*args)
|
123
|
+
case args.size
|
124
|
+
when 0
|
133
125
|
nil
|
126
|
+
when 1
|
127
|
+
arg = args.first
|
128
|
+
case arg
|
129
|
+
when Symbol
|
130
|
+
return_val = lookup_name(arg.id2name) and return return_val
|
131
|
+
when String
|
132
|
+
return_val = lookup_name(arg) and return return_val
|
133
|
+
when Fixnum
|
134
|
+
return_val = lookup_id(arg) and return return_val
|
135
|
+
when self
|
136
|
+
return arg
|
137
|
+
when nil
|
138
|
+
nil
|
139
|
+
else
|
140
|
+
raise TypeError, "#{self.name}[]: argument should be a String, Symbol or Fixnum but got a: #{arg.class.name}"
|
141
|
+
end
|
142
|
+
self.send((self.acts_enumerated_on_lookup_failure || :enforce_none), arg)
|
134
143
|
else
|
135
|
-
|
144
|
+
args.inject([]){ |buffer, item|
|
145
|
+
buffer << self[item]
|
146
|
+
buffer
|
147
|
+
}.uniq
|
136
148
|
end
|
137
|
-
self.send((self.acts_enumerated_on_lookup_failure || :enforce_none), arg)
|
138
149
|
end
|
139
150
|
|
140
151
|
# Enum lookup by id
|
@@ -51,7 +51,8 @@ module ActiveRecord
|
|
51
51
|
# Setting this option will generate an after_initialize callback to set a default value on the attribute
|
52
52
|
# unless a non-nil one already exists.
|
53
53
|
# [:create_scope]
|
54
|
-
# Setting this option to 'false' will disable automatically creating
|
54
|
+
# Setting this option to 'false' will disable automatically creating 'with_enum_attribute' and
|
55
|
+
# 'exclude_enum_attribute' scope.
|
55
56
|
#
|
56
57
|
# === Example
|
57
58
|
# class Booking < ActiveRecord::Base
|
@@ -171,12 +172,19 @@ module ActiveRecord
|
|
171
172
|
}
|
172
173
|
where(:#{foreign_key} => ids)
|
173
174
|
}
|
175
|
+
scope :exclude_#{name}, lambda {|*args|
|
176
|
+
ids = #{class_name}.all - args.map{ |arg|
|
177
|
+
n = #{class_name}[arg]
|
178
|
+
}
|
179
|
+
where(:#{foreign_key} => ids)
|
180
|
+
}
|
174
181
|
end_eval
|
175
182
|
|
176
183
|
if (name_p = name.pluralize) != name
|
177
184
|
module_eval( <<-end_eval, __FILE__, __LINE__)
|
178
185
|
class << self
|
179
186
|
alias_method :with_#{name_p}, :with_#{name}
|
187
|
+
alias_method :exclude_#{name_p}, :exclude_#{name}
|
180
188
|
end
|
181
189
|
end_eval
|
182
190
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-
|
15
|
+
date: 2012-07-21 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
@@ -157,6 +157,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
157
157
|
- - ! '>='
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
|
+
segments:
|
161
|
+
- 0
|
162
|
+
hash: 1073032458580919544
|
160
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
164
|
none: false
|
162
165
|
requirements:
|