power_enum 0.8.4 → 0.8.5
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.
- data/README.md +13 -7
- data/lib/active_record/aggregations/has_enumerated.rb +46 -7
- metadata +3 -3
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Power Enum
|
2
2
|
|
3
|
-
https://github.com/albertosaurus/
|
3
|
+
https://github.com/albertosaurus/power\_enum
|
4
4
|
|
5
5
|
Enumerations for Rails 3.X Done Right.
|
6
6
|
|
@@ -12,9 +12,8 @@ It is particularly suitable for scenarios where your Rails application is not th
|
|
12
12
|
when it's used for analytics or reporting.
|
13
13
|
|
14
14
|
Power Enum is a fork of the Rails 3 modernization made by the fine folks at Protocool
|
15
|
-
https://github.com/protocool/enumerations\_mixin to the original plugin by Trevor Squires
|
16
|
-
|
17
|
-
test suite written to facilitate further development.
|
15
|
+
https://github.com/protocool/enumerations\_mixin to the original plugin by Trevor Squires. While many of the core ideas
|
16
|
+
remain, it has been reworked and a full test suite written to facilitate further development.
|
18
17
|
|
19
18
|
At it's most basic level, it allows you to say things along the lines of:
|
20
19
|
|
@@ -22,8 +21,7 @@ At it's most basic level, it allows you to say things along the lines of:
|
|
22
21
|
booking.status = :confirmed
|
23
22
|
booking = Booking.create( :status => :rejected )
|
24
23
|
|
25
|
-
Booking.
|
26
|
-
:conditions => ['status_id = ?', BookingStatus[:provisional].id]
|
24
|
+
Booking.where(:status_id => BookingStatus[:provisional])
|
27
25
|
|
28
26
|
BookingStatus.all.collect {|status|, [status.name, status.id]}
|
29
27
|
|
@@ -402,7 +400,7 @@ The `:on_lookup_failure` option in has\_enumerated is there because you may want
|
|
402
400
|
situations where the argument passed to `status=(arg)` is invalid. By default, an invalid value will cause an
|
403
401
|
ArgumentError to be raised.
|
404
402
|
|
405
|
-
Of course, this may not be optimal in your situation. In this case you can do one of
|
403
|
+
Of course, this may not be optimal in your situation. In this case you can do one of three things:
|
406
404
|
|
407
405
|
1) You can set it to 'validation\_error'. In this case, the invalid value will be cached and returned on
|
408
406
|
subsequent lookups, but the model will fail validation.
|
@@ -417,6 +415,14 @@ raise an exception, while in the case of a `:write` you don't have to return any
|
|
417
415
|
Note that there's enough information in the method signature that you can specify one method to handle all lookup
|
418
416
|
failures for all has\_enumerated fields if you happen to have more than one defined in your model.
|
419
417
|
|
418
|
+
3) (Since version 0.8.5) Give it a lambda function. In that case, the lambda needs to accept the ActiveRecord model as
|
419
|
+
its first argument, with the rest of the arguments being identical to the signature of the lookup handler instance
|
420
|
+
method.
|
421
|
+
|
422
|
+
:on_lookup_failure => lambda{ |record, op, attr, fk, cl_name, value|
|
423
|
+
# handle lookup failure
|
424
|
+
}
|
425
|
+
|
420
426
|
NOTE: A `nil` is always considered to be a valid value for `status=(arg)` since it's assumed you're trying to null out
|
421
427
|
the foreign key. The `:on_lookup_failure` will be bypassed.
|
422
428
|
|
@@ -32,7 +32,8 @@ module ActiveRecord
|
|
32
32
|
# [:on_lookup_failure]
|
33
33
|
# The :on_lookup_failure option in has_enumerated is there because you may want to create an error handler for
|
34
34
|
# situations where the argument passed to status=(arg) is invalid. By default, an invalid value will cause an
|
35
|
-
# ArgumentError to be raised. Since this may not be optimal in your situation, you can do one of
|
35
|
+
# ArgumentError to be raised. Since this may not be optimal in your situation, you can do one of three
|
36
|
+
# things:
|
36
37
|
#
|
37
38
|
# 1) You can set it to 'validation_error'. In this case, the invalid value will be cached and returned on
|
38
39
|
# subsequent lookups, but the model will fail validation.
|
@@ -45,6 +46,9 @@ module ActiveRecord
|
|
45
46
|
# failures for all has_enumerated fields if you happen to have more than one defined in your model.
|
46
47
|
# 'NOTE': A nil is always considered to be a valid value for status=(arg) since it's assumed you're trying to
|
47
48
|
# null out the foreign key. The :on_lookup_failure method will be bypassed.
|
49
|
+
# 3) You can give it a lambda function. In that case, the lambda needs to accept the ActiveRecord model as
|
50
|
+
# its first argument, with the rest of the arguments being identical to the signature of the lookup handler
|
51
|
+
# instance method.
|
48
52
|
# [:permit_empty_name]
|
49
53
|
# Setting this to 'true' disables automatic conversion of empty strings to nil. Default is 'false'.
|
50
54
|
# [:default]
|
@@ -64,6 +68,17 @@ module ActiveRecord
|
|
64
68
|
# :default => :unconfirmed,
|
65
69
|
# :create_cope => false
|
66
70
|
# end
|
71
|
+
#
|
72
|
+
# === Example 2
|
73
|
+
#
|
74
|
+
# class Booking < ActiveRecord::Base
|
75
|
+
# has_enumerated :booking_status,
|
76
|
+
# :class_name => 'BookingStatus',
|
77
|
+
# :foreign_key => 'status_id',
|
78
|
+
# :on_lookup_failure => lambda{ |record, op, attr, fk, cl_name, value|
|
79
|
+
# # handle lookup failure
|
80
|
+
# }
|
81
|
+
# end
|
67
82
|
def has_enumerated(part_id, options = {})
|
68
83
|
options.assert_valid_keys( :class_name,
|
69
84
|
:foreign_key,
|
@@ -78,18 +93,23 @@ module ActiveRecord
|
|
78
93
|
name = part_id.to_s
|
79
94
|
class_name = reflection.class_name
|
80
95
|
foreign_key = reflection.foreign_key
|
81
|
-
|
96
|
+
failure_opt = options[:on_lookup_failure]
|
82
97
|
empty_name = options[:permit_empty_name]
|
83
98
|
create_scope = options[:create_scope]
|
84
99
|
|
100
|
+
failure_handler = get_lookup_failure_handler(failure_opt)
|
101
|
+
|
102
|
+
class_attribute "has_enumerated_#{name}_error_handler"
|
103
|
+
self.send("has_enumerated_#{name}_error_handler=", failure_handler)
|
104
|
+
|
85
105
|
module_eval( <<-end_eval, __FILE__, __LINE__ )
|
86
106
|
def #{name}
|
87
107
|
if @invalid_enum_values && @invalid_enum_values.has_key?(:#{name})
|
88
108
|
return @invalid_enum_values[:#{name}]
|
89
109
|
end
|
90
110
|
rval = #{class_name}.lookup_id(self.#{foreign_key})
|
91
|
-
if rval.nil? && #{!
|
92
|
-
self.
|
111
|
+
if rval.nil? && #{!failure_handler.nil?}
|
112
|
+
self.class.has_enumerated_#{name}_error_handler.call(self, :read, #{name.inspect}, #{foreign_key.inspect}, #{class_name.inspect}, self.#{foreign_key})
|
93
113
|
else
|
94
114
|
rval
|
95
115
|
end
|
@@ -117,20 +137,22 @@ module ActiveRecord
|
|
117
137
|
end
|
118
138
|
|
119
139
|
if val.nil?
|
120
|
-
if #{
|
140
|
+
if #{failure_handler.nil?}
|
121
141
|
raise ArgumentError, "#{self.name}: #{name}= can't assign a #{class_name} for a value of (\#{arg.inspect})"
|
122
142
|
else
|
123
143
|
@invalid_enum_values.delete :#{name}
|
124
|
-
self.
|
144
|
+
self.class.has_enumerated_#{name}_error_handler.call(self, :write, #{name.inspect}, #{foreign_key.inspect}, #{class_name.inspect}, arg)
|
125
145
|
end
|
126
146
|
else
|
127
147
|
@invalid_enum_values.delete :#{name}
|
128
148
|
self.#{foreign_key} = val.id
|
129
149
|
end
|
130
150
|
end
|
151
|
+
|
152
|
+
alias_method :'#{name}_bak=', :'#{name}='
|
131
153
|
end_eval
|
132
154
|
|
133
|
-
if
|
155
|
+
if failure_opt.to_s == 'validation_error'
|
134
156
|
module_eval( <<-end_eval, __FILE__, __LINE__ )
|
135
157
|
validate do
|
136
158
|
if @invalid_enum_values && @invalid_enum_values.has_key?(:#{name})
|
@@ -192,6 +214,23 @@ module ActiveRecord
|
|
192
214
|
|
193
215
|
end #has_enumerated
|
194
216
|
|
217
|
+
def get_lookup_failure_handler(failure_opt)
|
218
|
+
if failure_opt.nil?
|
219
|
+
nil
|
220
|
+
else
|
221
|
+
case failure_opt
|
222
|
+
when Proc
|
223
|
+
failure_opt
|
224
|
+
else
|
225
|
+
lambda { |record, op, attr, fk, cl_name, value|
|
226
|
+
record.send(failure_opt.to_s, op, attr, fk, cl_name, value)
|
227
|
+
}
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
231
|
+
end
|
232
|
+
private :get_lookup_failure_handler
|
233
|
+
|
195
234
|
end #module MacroMethods
|
196
235
|
|
197
236
|
end #module HasEnumerated
|
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.8.
|
4
|
+
version: 0.8.5
|
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-08-
|
15
|
+
date: 2012-08-28 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
143
|
version: '0'
|
144
144
|
segments:
|
145
145
|
- 0
|
146
|
-
hash:
|
146
|
+
hash: -1049060871827763452
|
147
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
148
|
none: false
|
149
149
|
requirements:
|