power_enum 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -3
- data/lib/active_record/aggregations/has_enumerated.rb +56 -5
- metadata +14 -14
data/README.md
CHANGED
@@ -322,14 +322,17 @@ the `has_enumerated` macro behaves more like an aggregation than an association.
|
|
322
322
|
has_enumerated :status, :class_name => 'BookingStatus',
|
323
323
|
:foreign_key => 'status_id',
|
324
324
|
:on_lookup_failure => :optional_instance_method,
|
325
|
-
:permit_empty_name => true #Setting this to true disables automatic conversion of empty strings to nil. Default is false.
|
325
|
+
:permit_empty_name => true, #Setting this to true disables automatic conversion of empty strings to nil. Default is false.
|
326
|
+
:default => :unconfirmed #Default value of the attribute.
|
326
327
|
end
|
327
328
|
|
328
329
|
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
|
329
330
|
chose to make the column name 'status\_id' for the sake of brevity, we must explicitly designate it. Additionally, the default value for
|
330
331
|
`:class_name` is the camel-ized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below. `:permit_empty_name`
|
331
332
|
is an optional flag to disable automatic conversion of empty strings to nil. It is typically desirable to have `booking.update_attributes(:status => '')`
|
332
|
-
assign status_id to a nil rather than raise an Error, as you'll be often calling `update_attributes` with form data
|
333
|
+
assign status_id to a nil rather than raise an Error, as you'll be often calling `update_attributes` with form data, but
|
334
|
+
the choice is yours. Setting a `:default` option will generate an after_initialize callback to set a default value on the
|
335
|
+
attribute unless a non-nil value has already been set.
|
333
336
|
|
334
337
|
With that, your Booking class will have the following methods defined:
|
335
338
|
|
@@ -399,7 +402,7 @@ Run migrations for test environment:
|
|
399
402
|
|
400
403
|
RAILS_ENV=test rake db:migrate
|
401
404
|
|
402
|
-
|
405
|
+
You may need to use `bundle exec` if you have gem conflicts:
|
403
406
|
|
404
407
|
RAILS_ENV=test bundle exec rake db:migrate
|
405
408
|
|
@@ -4,12 +4,14 @@
|
|
4
4
|
module ActiveRecord
|
5
5
|
module Aggregations # :nodoc:
|
6
6
|
module HasEnumerated # :nodoc:
|
7
|
+
|
7
8
|
def self.append_features(base)
|
8
9
|
super
|
9
10
|
base.extend(MacroMethods)
|
10
11
|
end
|
11
12
|
|
12
13
|
module MacroMethods
|
14
|
+
|
13
15
|
def enumerated_attributes
|
14
16
|
@enumerated_attributes ||= []
|
15
17
|
end
|
@@ -19,8 +21,43 @@ module ActiveRecord
|
|
19
21
|
enumerated_attributes.include? attribute.to_s
|
20
22
|
end
|
21
23
|
|
24
|
+
# Defines an enumerated attribute with the given name on the model. Also accepts a hash of options as an
|
25
|
+
# optional second argument.
|
26
|
+
#
|
27
|
+
# === Supported options
|
28
|
+
# [:class_name]
|
29
|
+
# Name of the enum class. By default it is the camel-ized version of the has_enumerated attribute.
|
30
|
+
# [:foreign_key]
|
31
|
+
# Explicitly set the foreign key column. By default it's assumed to be your_enumerated_attribute_name_id.
|
32
|
+
# [:on_lookup_failure]
|
33
|
+
# The :on_lookup_failure option in has_enumerated is there because you may want to create an error handler for
|
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 specify an instance
|
36
|
+
# method to be called in the case of a lookup failure. The method signature is as follows:
|
37
|
+
# <tt>your_lookup_handler(operation, name, name_foreign_key, acts_enumerated_class_name, lookup_value)</tt>
|
38
|
+
# The 'operation' arg will be either :read or :write. In the case of :read you are expected to return
|
39
|
+
# something or raise an exception, while in the case of a :write you don't have to return anything. Note that
|
40
|
+
# there's enough information in the method signature that you can specify one method to handle all lookup
|
41
|
+
# failures for all has_enumerated fields if you happen to have more than one defined in your model.
|
42
|
+
# 'NOTE': A nil is always considered to be a valid value for status=(arg) since it's assumed you're trying to
|
43
|
+
# null out the foreign key. The :on_lookup_failure method will be bypassed.
|
44
|
+
# [:permit_empty_name]
|
45
|
+
# Setting this to 'true' disables automatic conversion of empty strings to nil. Default is 'false'.
|
46
|
+
# [:default]
|
47
|
+
# Setting this option will generate an after_initialize callback to set a default value on the attribute
|
48
|
+
# unless a non-nil one already exists.
|
49
|
+
#
|
50
|
+
# === Example
|
51
|
+
# class Booking < ActiveRecord::Base
|
52
|
+
# has_enumerated :status,
|
53
|
+
# :class_name => 'BookingStatus',
|
54
|
+
# :foreign_key => 'status_id',
|
55
|
+
# :on_lookup_failure => :optional_instance_method,
|
56
|
+
# :permit_empty_name => true
|
57
|
+
# :default => :unconfirmed
|
58
|
+
# end
|
22
59
|
def has_enumerated(part_id, options = {})
|
23
|
-
options.assert_valid_keys(:class_name, :foreign_key, :on_lookup_failure, :permit_empty_name)
|
60
|
+
options.assert_valid_keys(:class_name, :foreign_key, :on_lookup_failure, :permit_empty_name, :default)
|
24
61
|
|
25
62
|
reflection = PowerEnum::Reflection::EnumerationReflection.new(part_id, options, self)
|
26
63
|
self.reflections.merge! part_id => reflection
|
@@ -71,8 +108,22 @@ module ActiveRecord
|
|
71
108
|
|
72
109
|
enumerated_attributes << name
|
73
110
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
111
|
+
if options.has_key?(:default)
|
112
|
+
default = options[:default]
|
113
|
+
set_default_method = "set_default_value_for_#{name}".to_sym
|
114
|
+
|
115
|
+
after_initialize set_default_method
|
116
|
+
|
117
|
+
define_method set_default_method do
|
118
|
+
self.send("#{name}=", default) if self.send(name).nil?
|
119
|
+
end
|
120
|
+
private set_default_method
|
121
|
+
end
|
122
|
+
|
123
|
+
end #has_enumerated
|
124
|
+
|
125
|
+
end #module MacroMethods
|
126
|
+
|
127
|
+
end #module HasEnumerated
|
128
|
+
end #module Aggregations
|
78
129
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 2
|
10
|
+
version: 0.5.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Trevor Squires
|
@@ -18,10 +18,10 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date:
|
21
|
+
date: 2012-01-07 00:00:00 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
@@ -32,12 +32,12 @@ dependencies:
|
|
32
32
|
- 0
|
33
33
|
- 0
|
34
34
|
version: 3.0.0
|
35
|
+
version_requirements: *id001
|
35
36
|
name: rails
|
36
37
|
prerelease: false
|
37
38
|
type: :runtime
|
38
|
-
requirement: *id001
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
|
-
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ">="
|
@@ -46,12 +46,12 @@ dependencies:
|
|
46
46
|
segments:
|
47
47
|
- 0
|
48
48
|
version: "0"
|
49
|
+
version_requirements: *id002
|
49
50
|
name: jeweler
|
50
51
|
prerelease: false
|
51
52
|
type: :development
|
52
|
-
requirement: *id002
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
|
-
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
55
|
none: false
|
56
56
|
requirements:
|
57
57
|
- - ">="
|
@@ -60,12 +60,12 @@ dependencies:
|
|
60
60
|
segments:
|
61
61
|
- 0
|
62
62
|
version: "0"
|
63
|
+
version_requirements: *id003
|
63
64
|
name: rspec
|
64
65
|
prerelease: false
|
65
66
|
type: :development
|
66
|
-
requirement: *id003
|
67
67
|
- !ruby/object:Gem::Dependency
|
68
|
-
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
69
|
none: false
|
70
70
|
requirements:
|
71
71
|
- - ">="
|
@@ -74,12 +74,12 @@ dependencies:
|
|
74
74
|
segments:
|
75
75
|
- 0
|
76
76
|
version: "0"
|
77
|
+
version_requirements: *id004
|
77
78
|
name: sqlite3
|
78
79
|
prerelease: false
|
79
80
|
type: :development
|
80
|
-
requirement: *id004
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
|
-
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - "="
|
@@ -90,10 +90,10 @@ dependencies:
|
|
90
90
|
- 2
|
91
91
|
- 1
|
92
92
|
version: 0.2.1
|
93
|
+
version_requirements: *id005
|
93
94
|
name: genspec
|
94
95
|
prerelease: false
|
95
96
|
type: :development
|
96
|
-
requirement: *id005
|
97
97
|
description: |
|
98
98
|
Power Enum allows you to treat instances of your ActiveRecord models as though they were an enumeration of values.
|
99
99
|
It allows you to cleanly solve many of the problems that the traditional Rails alternatives handle poorly if at all.
|