power_enum 0.6.3 → 0.7.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.
- data/README.md +6 -2
- data/lib/active_record/aggregations/has_enumerated.rb +43 -9
- metadata +18 -2
data/README.md
CHANGED
@@ -385,8 +385,12 @@ The `:on_lookup_failure` option in has\_enumerated is there because you may want
|
|
385
385
|
situations where the argument passed to `status=(arg)` is invalid. By default, an invalid value will cause an
|
386
386
|
ArgumentError to be raised.
|
387
387
|
|
388
|
-
Of course, this may not be optimal in your situation. In this case you can
|
389
|
-
|
388
|
+
Of course, this may not be optimal in your situation. In this case you can do one of two thigs:
|
389
|
+
|
390
|
+
1) You can set it to 'validation\_error'. In this case, the invalid value will be cached and returned on
|
391
|
+
subsequent lookups, but the model will fail validation.
|
392
|
+
|
393
|
+
2) Specify an *instance* method to be called in the case of a lookup failure. The method signature is as follows:
|
390
394
|
|
391
395
|
your_lookup_handler(operation, name, name_foreign_key, acts_enumerated_class_name, lookup_value)
|
392
396
|
|
@@ -32,8 +32,12 @@ 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
|
36
|
-
#
|
35
|
+
# ArgumentError to be raised. Since this may not be optimal in your situation, you can do one of two things:
|
36
|
+
#
|
37
|
+
# 1) You can set it to 'validation_error'. In this case, the invalid value will be cached and returned on
|
38
|
+
# subsequent lookups, but the model will fail validation.
|
39
|
+
# 2) You can specify an instance method to be called in the case of a lookup failure. The method signature is
|
40
|
+
# as follows:
|
37
41
|
# <tt>your_lookup_handler(operation, name, name_foreign_key, acts_enumerated_class_name, lookup_value)</tt>
|
38
42
|
# The 'operation' arg will be either :read or :write. In the case of :read you are expected to return
|
39
43
|
# something or raise an exception, while in the case of a :write you don't have to return anything. Note that
|
@@ -79,14 +83,20 @@ module ActiveRecord
|
|
79
83
|
|
80
84
|
module_eval( <<-end_eval, __FILE__, __LINE__ )
|
81
85
|
def #{name}
|
86
|
+
if @invalid_enum_values && @invalid_enum_values.has_key?(:#{name})
|
87
|
+
return @invalid_enum_values[:#{name}]
|
88
|
+
end
|
82
89
|
rval = #{class_name}.lookup_id(self.#{foreign_key})
|
83
90
|
if rval.nil? && #{!failure.nil?}
|
84
|
-
|
91
|
+
self.send(#{failure.inspect}, :read, #{name.inspect}, #{foreign_key.inspect}, #{class_name.inspect}, self.#{foreign_key})
|
92
|
+
else
|
93
|
+
rval
|
85
94
|
end
|
86
|
-
|
87
|
-
end
|
95
|
+
end
|
88
96
|
|
89
97
|
def #{name}=(arg)
|
98
|
+
@invalid_enum_values ||= {}
|
99
|
+
|
90
100
|
#{!empty_name ? 'arg = nil if arg.blank?' : ''}
|
91
101
|
case arg
|
92
102
|
when #{class_name}
|
@@ -99,22 +109,46 @@ module ActiveRecord
|
|
99
109
|
val = #{class_name}.lookup_id(arg)
|
100
110
|
when nil
|
101
111
|
self.#{foreign_key} = nil
|
112
|
+
@invalid_enum_values.delete :#{name}
|
102
113
|
return nil
|
103
|
-
else
|
104
|
-
raise TypeError, "#{self.name}: #{name}= argument must be a #{class_name}, String, Symbol or Fixnum but got a: \#{arg.class.name}"
|
114
|
+
else
|
115
|
+
raise TypeError, "#{self.name}: #{name}= argument must be a #{class_name}, String, Symbol or Fixnum but got a: \#{arg.class.name}"
|
105
116
|
end
|
106
117
|
|
107
|
-
if val.nil?
|
118
|
+
if val.nil?
|
108
119
|
if #{failure.nil?}
|
109
120
|
raise ArgumentError, "#{self.name}: #{name}= can't assign a #{class_name} for a value of (\#{arg.inspect})"
|
121
|
+
else
|
122
|
+
@invalid_enum_values.delete :#{name}
|
123
|
+
self.send(#{failure.inspect}, :write, #{name.inspect}, #{foreign_key.inspect}, #{class_name.inspect}, arg)
|
110
124
|
end
|
111
|
-
self.send(#{failure.inspect}, :write, #{name.inspect}, #{foreign_key.inspect}, #{class_name.inspect}, arg)
|
112
125
|
else
|
126
|
+
@invalid_enum_values.delete :#{name}
|
113
127
|
self.#{foreign_key} = val.id
|
114
128
|
end
|
115
129
|
end
|
116
130
|
end_eval
|
117
131
|
|
132
|
+
if failure.to_s == 'validation_error'
|
133
|
+
module_eval( <<-end_eval, __FILE__, __LINE__ )
|
134
|
+
validate do
|
135
|
+
if @invalid_enum_values && @invalid_enum_values.has_key?(:#{name})
|
136
|
+
errors.add(:#{name}, "is invalid")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def validation_error(operation, name, name_foreign_key, acts_enumerated_class_name, lookup_value)
|
141
|
+
@invalid_enum_values ||= {}
|
142
|
+
if operation == :write
|
143
|
+
@invalid_enum_values[name.to_sym] = lookup_value
|
144
|
+
else
|
145
|
+
nil
|
146
|
+
end
|
147
|
+
end
|
148
|
+
private :validation_error
|
149
|
+
end_eval
|
150
|
+
end
|
151
|
+
|
118
152
|
enumerated_attributes << name
|
119
153
|
|
120
154
|
if options.has_key?(:default)
|
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.7.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-06-13 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
@@ -94,6 +94,22 @@ dependencies:
|
|
94
94
|
- - '='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 0.2.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
97
113
|
description: ! 'Power Enum allows you to treat instances of your ActiveRecord models
|
98
114
|
as though they were an enumeration of values.
|
99
115
|
|