power_enum 0.4.4 → 0.4.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 +5 -2
- data/lib/active_record/aggregations/has_enumerated.rb +9 -4
- data/lib/power_enum.rb +1 -0
- data/lib/power_enum/reflection.rb +29 -0
- metadata +13 -12
data/README.md
CHANGED
@@ -279,12 +279,15 @@ the `has_enumerated` macro behaves more like an aggregation than an association.
|
|
279
279
|
class Booking < ActiveRecord::Base
|
280
280
|
has_enumerated :status, :class_name => 'BookingStatus',
|
281
281
|
:foreign_key => 'status_id',
|
282
|
-
:on_lookup_failure => :optional_instance_method
|
282
|
+
:on_lookup_failure => :optional_instance_method,
|
283
|
+
:permit_empty_name => true #Setting this to true disables automatic conversion of empty strings to nil. Default is false.
|
283
284
|
end
|
284
285
|
|
285
286
|
By default, the foreign key is interpreted to be the name of your has\_enumerated field (in this case 'booking_status') plus '\_id'. Since we
|
286
287
|
chose to make the column name 'status\_id' for the sake of brevity, we must explicitly designate it. Additionally, the default value for
|
287
|
-
`:class_name` is the camel-ized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below.
|
288
|
+
`:class_name` is the camel-ized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below. `:permit_empty_name`
|
289
|
+
is an optional flag to disable automatic conversion of empty strings to nil. It is typically desirable to have `booking.update_attributes(:status => '')`
|
290
|
+
assign status_id to a nil rather than raise an Error, as you'll be often calling `update_attributes` with form data.
|
288
291
|
|
289
292
|
With that, your Booking class will have the following methods defined:
|
290
293
|
|
@@ -20,12 +20,16 @@ module ActiveRecord
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def has_enumerated(part_id, options = {})
|
23
|
-
options.assert_valid_keys(:class_name, :foreign_key, :on_lookup_failure)
|
23
|
+
options.assert_valid_keys(:class_name, :foreign_key, :on_lookup_failure, :permit_empty_name)
|
24
|
+
|
25
|
+
reflection = PowerEnum::Reflection::EnumerationReflection.new(part_id, options, self)
|
26
|
+
self.reflections.merge! part_id => reflection
|
24
27
|
|
25
28
|
name = part_id.to_s
|
26
|
-
class_name =
|
27
|
-
foreign_key =
|
29
|
+
class_name = reflection.class_name
|
30
|
+
foreign_key = reflection.foreign_key
|
28
31
|
failure = options[:on_lookup_failure]
|
32
|
+
empty_name = options[:permit_empty_name]
|
29
33
|
|
30
34
|
module_eval <<-end_eval
|
31
35
|
def #{name}
|
@@ -36,7 +40,8 @@ module ActiveRecord
|
|
36
40
|
return rval
|
37
41
|
end
|
38
42
|
|
39
|
-
def #{name}=(arg)
|
43
|
+
def #{name}=(arg)
|
44
|
+
#{!empty_name ? 'arg = nil if arg.blank?' : ''}
|
40
45
|
case arg
|
41
46
|
when #{class_name}
|
42
47
|
val = #{class_name}.lookup_id(arg.id)
|
data/lib/power_enum.rb
CHANGED
@@ -7,6 +7,7 @@ class PowerEnum < Rails::Engine
|
|
7
7
|
ActiveSupport.on_load(:active_record) do
|
8
8
|
include ActiveRecord::Acts::Enumerated
|
9
9
|
include ActiveRecord::Aggregations::HasEnumerated
|
10
|
+
include PowerEnum::Reflection
|
10
11
|
|
11
12
|
ActiveRecord::ConnectionAdapters.module_eval do
|
12
13
|
include PowerEnum::Schema::SchemaStatements
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module PowerEnum::Reflection
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def reflect_on_all_enumerated
|
6
|
+
# Need to give it a full namespace to avoid getting Rails confused in development
|
7
|
+
# mode where all objects are reloaded on every request.
|
8
|
+
reflections.values.grep(PowerEnum::Reflection::EnumerationReflection)
|
9
|
+
end
|
10
|
+
|
11
|
+
def reflect_on_enumerated enumerated
|
12
|
+
reflections[enumerated.to_sym].is_a?(PowerEnum::Reflection::EnumerationReflection) ? reflections[enumerated.to_sym] : nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class EnumerationReflection < ActiveRecord::Reflection::MacroReflection
|
17
|
+
def initialize name, options, active_record
|
18
|
+
super :has_enumerated, name, options, active_record
|
19
|
+
end
|
20
|
+
|
21
|
+
def class_name
|
22
|
+
@class_name ||= (@options[:class_name] || @name).to_s.camelize
|
23
|
+
end
|
24
|
+
|
25
|
+
def foreign_key
|
26
|
+
@foreign_key ||= (@options[:foreign_key] || "#{@name}_id").to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
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.
|
4
|
+
version: 0.4.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,11 +12,11 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2011-
|
15
|
+
date: 2011-10-10 00:00:00.000000000Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
19
|
-
requirement: &
|
19
|
+
requirement: &27154260 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: 3.0.0
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *27154260
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: jeweler
|
30
|
-
requirement: &
|
30
|
+
requirement: &27152980 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ! '>='
|
@@ -35,10 +35,10 @@ dependencies:
|
|
35
35
|
version: '0'
|
36
36
|
type: :development
|
37
37
|
prerelease: false
|
38
|
-
version_requirements: *
|
38
|
+
version_requirements: *27152980
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: rspec
|
41
|
-
requirement: &
|
41
|
+
requirement: &27152060 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ! '>='
|
@@ -46,10 +46,10 @@ dependencies:
|
|
46
46
|
version: '0'
|
47
47
|
type: :development
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
49
|
+
version_requirements: *27152060
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: sqlite3
|
52
|
-
requirement: &
|
52
|
+
requirement: &27151240 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
55
|
- - ! '>='
|
@@ -57,10 +57,10 @@ dependencies:
|
|
57
57
|
version: '0'
|
58
58
|
type: :development
|
59
59
|
prerelease: false
|
60
|
-
version_requirements: *
|
60
|
+
version_requirements: *27151240
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: genspec
|
63
|
-
requirement: &
|
63
|
+
requirement: &27150660 !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
66
|
- - ! '>='
|
@@ -68,7 +68,7 @@ dependencies:
|
|
68
68
|
version: '0'
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
|
-
version_requirements: *
|
71
|
+
version_requirements: *27150660
|
72
72
|
description: ! 'Power Enum allows you to treat instances of your ActiveRecord models
|
73
73
|
as though they were an enumeration of values.
|
74
74
|
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/generators/enum/templates/rails31_migration.rb.erb
|
100
100
|
- lib/power_enum.rb
|
101
101
|
- lib/power_enum/migration/command_recorder.rb
|
102
|
+
- lib/power_enum/reflection.rb
|
102
103
|
- lib/power_enum/schema/schema_statements.rb
|
103
104
|
- LICENSE
|
104
105
|
- README.md
|