classy_enum 1.4.0 → 2.0.1
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/.travis.yml +4 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +68 -59
- data/README.md +33 -12
- data/VERSION +1 -1
- data/classy_enum.gemspec +15 -14
- data/gemfiles/Gemfile.rails-3.0.x +8 -0
- data/gemfiles/Gemfile.rails-3.1.x +8 -0
- data/gemfiles/Gemfile.rails-3.2.x +8 -0
- data/lib/classy_enum/class_methods.rb +22 -17
- data/lib/classy_enum/instance_methods.rb +0 -11
- data/spec/classy_enum_owner_reference_spec.rb +14 -2
- data/spec/classy_enum_spec.rb +8 -8
- data/spec/spec_helper.rb +9 -31
- metadata +49 -23
- data/lib/classy_enum/semantic_form_builder.rb +0 -24
- data/spec/classy_enum_semantic_form_builder_spec.rb +0 -98
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# ClassyEnum Changelog
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
* Enum class definitions are no longer defined implicity and must be explicitly subclassed from children of ClassyEnum::Base
|
|
6
|
+
* Formtastic support is not longer built-in. See https://github.com/beerlington/classy_enum/wiki/Formtastic-Support
|
|
7
|
+
* validates_uniqueness_of with an enum scope no longer works in Rails
|
|
8
|
+
3.0.x (no changes for Rails 3.1 or 3.2)
|
|
9
|
+
|
|
3
10
|
## 1.3.2
|
|
4
11
|
|
|
5
12
|
* Added support for Ruby 1.9.3
|
data/Gemfile
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
source :rubygems
|
|
2
2
|
|
|
3
|
-
gem 'rails', '
|
|
3
|
+
gem 'rails', '~> 3.2.0'
|
|
4
4
|
|
|
5
5
|
group :development do
|
|
6
6
|
gem "jeweler", "~> 1.6.2"
|
|
7
7
|
gem "rspec-rails", "~> 2.8.1"
|
|
8
|
-
gem '
|
|
9
|
-
gem '
|
|
8
|
+
gem 'sqlite3'
|
|
9
|
+
gem 'json', '~> 1.6.5'
|
|
10
10
|
# gem 'ruby-debug19', :require => 'false'
|
|
11
11
|
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,74 +1,78 @@
|
|
|
1
1
|
GEM
|
|
2
2
|
remote: http://rubygems.org/
|
|
3
3
|
specs:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
rack (~> 1.2
|
|
15
|
-
rack-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
arel (
|
|
32
|
-
builder (
|
|
4
|
+
actionmailer (3.2.3)
|
|
5
|
+
actionpack (= 3.2.3)
|
|
6
|
+
mail (~> 2.4.4)
|
|
7
|
+
actionpack (3.2.3)
|
|
8
|
+
activemodel (= 3.2.3)
|
|
9
|
+
activesupport (= 3.2.3)
|
|
10
|
+
builder (~> 3.0.0)
|
|
11
|
+
erubis (~> 2.7.0)
|
|
12
|
+
journey (~> 1.0.1)
|
|
13
|
+
rack (~> 1.4.0)
|
|
14
|
+
rack-cache (~> 1.2)
|
|
15
|
+
rack-test (~> 0.6.1)
|
|
16
|
+
sprockets (~> 2.1.2)
|
|
17
|
+
activemodel (3.2.3)
|
|
18
|
+
activesupport (= 3.2.3)
|
|
19
|
+
builder (~> 3.0.0)
|
|
20
|
+
activerecord (3.2.3)
|
|
21
|
+
activemodel (= 3.2.3)
|
|
22
|
+
activesupport (= 3.2.3)
|
|
23
|
+
arel (~> 3.0.2)
|
|
24
|
+
tzinfo (~> 0.3.29)
|
|
25
|
+
activeresource (3.2.3)
|
|
26
|
+
activemodel (= 3.2.3)
|
|
27
|
+
activesupport (= 3.2.3)
|
|
28
|
+
activesupport (3.2.3)
|
|
29
|
+
i18n (~> 0.6)
|
|
30
|
+
multi_json (~> 1.0)
|
|
31
|
+
arel (3.0.2)
|
|
32
|
+
builder (3.0.0)
|
|
33
33
|
diff-lcs (1.1.3)
|
|
34
|
-
erubis (2.
|
|
35
|
-
abstract (>= 1.0.0)
|
|
36
|
-
formtastic (1.2.4)
|
|
37
|
-
actionpack (>= 2.3.7)
|
|
38
|
-
activesupport (>= 2.3.7)
|
|
39
|
-
i18n (~> 0.4)
|
|
34
|
+
erubis (2.7.0)
|
|
40
35
|
git (1.2.5)
|
|
36
|
+
hike (1.2.1)
|
|
41
37
|
i18n (0.6.0)
|
|
42
38
|
jeweler (1.6.4)
|
|
43
39
|
bundler (~> 1.0)
|
|
44
40
|
git (>= 1.2.5)
|
|
45
41
|
rake
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
journey (1.0.3)
|
|
43
|
+
json (1.6.6)
|
|
44
|
+
mail (2.4.4)
|
|
48
45
|
i18n (>= 0.4.0)
|
|
49
46
|
mime-types (~> 1.16)
|
|
50
47
|
treetop (~> 1.4.8)
|
|
51
|
-
mime-types (1.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
rack
|
|
55
|
-
|
|
56
|
-
|
|
48
|
+
mime-types (1.18)
|
|
49
|
+
multi_json (1.3.2)
|
|
50
|
+
polyglot (0.3.3)
|
|
51
|
+
rack (1.4.1)
|
|
52
|
+
rack-cache (1.2)
|
|
53
|
+
rack (>= 0.4)
|
|
54
|
+
rack-ssl (1.3.2)
|
|
55
|
+
rack
|
|
56
|
+
rack-test (0.6.1)
|
|
57
57
|
rack (>= 1.0)
|
|
58
|
-
rails (3.
|
|
59
|
-
actionmailer (= 3.
|
|
60
|
-
actionpack (= 3.
|
|
61
|
-
activerecord (= 3.
|
|
62
|
-
activeresource (= 3.
|
|
63
|
-
activesupport (= 3.
|
|
58
|
+
rails (3.2.3)
|
|
59
|
+
actionmailer (= 3.2.3)
|
|
60
|
+
actionpack (= 3.2.3)
|
|
61
|
+
activerecord (= 3.2.3)
|
|
62
|
+
activeresource (= 3.2.3)
|
|
63
|
+
activesupport (= 3.2.3)
|
|
64
64
|
bundler (~> 1.0)
|
|
65
|
-
railties (= 3.
|
|
66
|
-
railties (3.
|
|
67
|
-
actionpack (= 3.
|
|
68
|
-
activesupport (= 3.
|
|
65
|
+
railties (= 3.2.3)
|
|
66
|
+
railties (3.2.3)
|
|
67
|
+
actionpack (= 3.2.3)
|
|
68
|
+
activesupport (= 3.2.3)
|
|
69
|
+
rack-ssl (~> 1.3.2)
|
|
69
70
|
rake (>= 0.8.7)
|
|
70
|
-
|
|
71
|
+
rdoc (~> 3.4)
|
|
72
|
+
thor (~> 0.14.6)
|
|
71
73
|
rake (0.9.2.2)
|
|
74
|
+
rdoc (3.12)
|
|
75
|
+
json (~> 1.4)
|
|
72
76
|
rspec (2.8.0)
|
|
73
77
|
rspec-core (~> 2.8.0)
|
|
74
78
|
rspec-expectations (~> 2.8.0)
|
|
@@ -82,19 +86,24 @@ GEM
|
|
|
82
86
|
activesupport (>= 3.0)
|
|
83
87
|
railties (>= 3.0)
|
|
84
88
|
rspec (~> 2.8.0)
|
|
85
|
-
|
|
89
|
+
sprockets (2.1.2)
|
|
90
|
+
hike (~> 1.2)
|
|
91
|
+
rack (~> 1.0)
|
|
92
|
+
tilt (~> 1.1, != 1.3.0)
|
|
93
|
+
sqlite3 (1.3.5)
|
|
86
94
|
thor (0.14.6)
|
|
95
|
+
tilt (1.3.3)
|
|
87
96
|
treetop (1.4.10)
|
|
88
97
|
polyglot
|
|
89
98
|
polyglot (>= 0.3.1)
|
|
90
|
-
tzinfo (0.3.
|
|
99
|
+
tzinfo (0.3.33)
|
|
91
100
|
|
|
92
101
|
PLATFORMS
|
|
93
102
|
ruby
|
|
94
103
|
|
|
95
104
|
DEPENDENCIES
|
|
96
|
-
formtastic (~> 1.2.4)
|
|
97
105
|
jeweler (~> 1.6.2)
|
|
98
|
-
|
|
106
|
+
json (~> 1.6.5)
|
|
107
|
+
rails (~> 3.2.0)
|
|
99
108
|
rspec-rails (~> 2.8.1)
|
|
100
|
-
sqlite3
|
|
109
|
+
sqlite3
|
data/README.md
CHANGED
|
@@ -20,9 +20,22 @@ The gem is hosted at [rubygems.org](https://rubygems.org/gems/classy_enum)
|
|
|
20
20
|
|
|
21
21
|
You will also need to add `app/enums` as an autoloadable path. This configuration will depend on which version of rails you are using.
|
|
22
22
|
|
|
23
|
+
## Upgrading to 2.0
|
|
24
|
+
|
|
25
|
+
Prior to 2.0, enum classes were implicity defined and were only required
|
|
26
|
+
when overriding methods or properties. As of 2.0, all enum classes must
|
|
27
|
+
explicity subclass a child of ClassyEnum::Base. If you used the
|
|
28
|
+
generator, there are no changes to the existing structure.
|
|
29
|
+
|
|
30
|
+
Built-in Formtastic support has been removed. See the note at the
|
|
31
|
+
bottom of this readme for more information how how to enable it.
|
|
32
|
+
|
|
33
|
+
validates_uniqueness_of will no longer work with Rails 3.0.x when using
|
|
34
|
+
a scope that is the enum field.
|
|
35
|
+
|
|
23
36
|
## Example Usage
|
|
24
37
|
|
|
25
|
-
The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an ActiveRecord model called `Alarm` with an attribute called `priority`. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested.
|
|
38
|
+
The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an ActiveRecord model called `Alarm` with an attribute called `priority`. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested.
|
|
26
39
|
|
|
27
40
|
### 1. Generate the Enum
|
|
28
41
|
|
|
@@ -49,13 +62,13 @@ class PriorityHigh < Priority
|
|
|
49
62
|
end
|
|
50
63
|
```
|
|
51
64
|
|
|
52
|
-
The `enum_classes` macro will
|
|
65
|
+
The `enum_classes` class macro will define the enum member order as well as additional ClassyEnum behavior, which is described further down in this document.
|
|
53
66
|
|
|
54
67
|
### 2. Customize the Enum
|
|
55
68
|
|
|
56
69
|
The generator creates a default setup, but each enum member can be changed to fit your needs.
|
|
57
70
|
|
|
58
|
-
|
|
71
|
+
I have defined three priority levels: low, medium, and high. Each priority level can have different properties and methods associated with it.
|
|
59
72
|
|
|
60
73
|
I would like to add a method called `send_email?` that all member subclasses respond to. By default this method will return false, but will be overridden for high priority alarms to return true.
|
|
61
74
|
|
|
@@ -68,6 +81,12 @@ class Priority < ClassyEnum::Base
|
|
|
68
81
|
end
|
|
69
82
|
end
|
|
70
83
|
|
|
84
|
+
class prioritylow < priority
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class prioritymedium < priority
|
|
88
|
+
end
|
|
89
|
+
|
|
71
90
|
class PriorityHigh < Priority
|
|
72
91
|
def send_email?
|
|
73
92
|
true
|
|
@@ -75,8 +94,6 @@ class PriorityHigh < Priority
|
|
|
75
94
|
end
|
|
76
95
|
```
|
|
77
96
|
|
|
78
|
-
Note: Defining the subclasses within your enum file is only required when you will be overriding behavior and/or properties. The member subclasses still exist without being defined here because `ClassyEnum.enum_classes` automatically creates a class for each member. The generator only creates these subclass definitions for convenience, but they can be deleted as shown in this example.
|
|
79
|
-
|
|
80
97
|
### 3. Setup the ActiveRecord model
|
|
81
98
|
|
|
82
99
|
My ActiveRecord Alarm model needs a text field that will store a string representing the enum member. An example model schema might look something like:
|
|
@@ -131,6 +148,12 @@ class Priority < ClassyEnum::Base
|
|
|
131
148
|
owner :alarm
|
|
132
149
|
end
|
|
133
150
|
|
|
151
|
+
class prioritylow < priority
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
class prioritymedium < priority
|
|
155
|
+
end
|
|
156
|
+
|
|
134
157
|
class PriorityHigh < Priority
|
|
135
158
|
def send_email?
|
|
136
159
|
alarm.enabled?
|
|
@@ -183,7 +206,7 @@ end
|
|
|
183
206
|
```
|
|
184
207
|
|
|
185
208
|
There is an [issue](https://github.com/beerlington/classy_enum/issues/8)
|
|
186
|
-
with Rails 3.
|
|
209
|
+
with Rails 3.0 and higher when using validates_uniqueness_of
|
|
187
210
|
and a scope that is the enum field. This issue also occurs when using
|
|
188
211
|
`composed_of` and is not a bug with ClassyEnum. As a workaround to this
|
|
189
212
|
problem, you can use the reader suffix option when declaring your field:
|
|
@@ -244,11 +267,9 @@ Priority.valid_options # => low, medium, high
|
|
|
244
267
|
|
|
245
268
|
## Formtastic Support
|
|
246
269
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
require 'classy_enum/semantic_form_builder'
|
|
251
|
-
```
|
|
270
|
+
Built-in Formtastic support has been removed as of 2.0. It is still
|
|
271
|
+
available but needs to be enabled manually. To enable support visit
|
|
272
|
+
[the wiki](https://github.com/beerlington/classy_enum/wiki/Formtastic-Support)
|
|
252
273
|
|
|
253
274
|
Then in your Formtastic view forms, use this syntax: `<%= f.input :priority, :as => :enum_select %>`
|
|
254
275
|
|
|
@@ -256,4 +277,4 @@ Note: ClassyEnum respects the `:allow_blank` and `:allow_nil` options and will i
|
|
|
256
277
|
|
|
257
278
|
## Copyright
|
|
258
279
|
|
|
259
|
-
Copyright (c)
|
|
280
|
+
Copyright (c) 2012 [Peter Brown](https://github.com/beerlington). See LICENSE for details.
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
2.0.1
|
data/classy_enum.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = "classy_enum"
|
|
8
|
-
s.version = "
|
|
8
|
+
s.version = "2.0.1"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Peter Brown"]
|
|
12
|
-
s.date = "2012-
|
|
12
|
+
s.date = "2012-04-22"
|
|
13
13
|
s.description = "A utility that adds class based enum functionality to ActiveRecord attributes"
|
|
14
14
|
s.email = "github@lette.us"
|
|
15
15
|
s.extra_rdoc_files = [
|
|
@@ -28,50 +28,51 @@ Gem::Specification.new do |s|
|
|
|
28
28
|
"Rakefile",
|
|
29
29
|
"VERSION",
|
|
30
30
|
"classy_enum.gemspec",
|
|
31
|
+
"gemfiles/Gemfile.rails-3.0.x",
|
|
32
|
+
"gemfiles/Gemfile.rails-3.1.x",
|
|
33
|
+
"gemfiles/Gemfile.rails-3.2.x",
|
|
31
34
|
"init.rb",
|
|
32
35
|
"lib/classy_enum.rb",
|
|
33
36
|
"lib/classy_enum/attributes.rb",
|
|
34
37
|
"lib/classy_enum/base.rb",
|
|
35
38
|
"lib/classy_enum/class_methods.rb",
|
|
36
39
|
"lib/classy_enum/instance_methods.rb",
|
|
37
|
-
"lib/classy_enum/semantic_form_builder.rb",
|
|
38
40
|
"lib/generators/classy_enum/classy_enum_generator.rb",
|
|
39
41
|
"lib/generators/classy_enum/templates/enum.rb",
|
|
40
42
|
"spec/active_record_spec.rb",
|
|
41
43
|
"spec/classy_enum_attributes_spec.rb",
|
|
42
44
|
"spec/classy_enum_owner_reference_spec.rb",
|
|
43
|
-
"spec/classy_enum_semantic_form_builder_spec.rb",
|
|
44
45
|
"spec/classy_enum_spec.rb",
|
|
45
46
|
"spec/spec_helper.rb"
|
|
46
47
|
]
|
|
47
48
|
s.homepage = "http://github.com/beerlington/classy_enum"
|
|
48
49
|
s.licenses = ["MIT"]
|
|
49
50
|
s.require_paths = ["lib"]
|
|
50
|
-
s.rubygems_version = "1.8.
|
|
51
|
+
s.rubygems_version = "1.8.19"
|
|
51
52
|
s.summary = "A class based enumerator utility for Ruby on Rails"
|
|
52
53
|
|
|
53
54
|
if s.respond_to? :specification_version then
|
|
54
55
|
s.specification_version = 3
|
|
55
56
|
|
|
56
57
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
57
|
-
s.add_runtime_dependency(%q<rails>, ["
|
|
58
|
+
s.add_runtime_dependency(%q<rails>, ["~> 3.2.0"])
|
|
58
59
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
|
59
60
|
s.add_development_dependency(%q<rspec-rails>, ["~> 2.8.1"])
|
|
60
|
-
s.add_development_dependency(%q<
|
|
61
|
-
s.add_development_dependency(%q<
|
|
61
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
|
62
|
+
s.add_development_dependency(%q<json>, ["~> 1.6.5"])
|
|
62
63
|
else
|
|
63
|
-
s.add_dependency(%q<rails>, ["
|
|
64
|
+
s.add_dependency(%q<rails>, ["~> 3.2.0"])
|
|
64
65
|
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
|
65
66
|
s.add_dependency(%q<rspec-rails>, ["~> 2.8.1"])
|
|
66
|
-
s.add_dependency(%q<
|
|
67
|
-
s.add_dependency(%q<
|
|
67
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
|
68
|
+
s.add_dependency(%q<json>, ["~> 1.6.5"])
|
|
68
69
|
end
|
|
69
70
|
else
|
|
70
|
-
s.add_dependency(%q<rails>, ["
|
|
71
|
+
s.add_dependency(%q<rails>, ["~> 3.2.0"])
|
|
71
72
|
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
|
72
73
|
s.add_dependency(%q<rspec-rails>, ["~> 2.8.1"])
|
|
73
|
-
s.add_dependency(%q<
|
|
74
|
-
s.add_dependency(%q<
|
|
74
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
|
75
|
+
s.add_dependency(%q<json>, ["~> 1.6.5"])
|
|
75
76
|
end
|
|
76
77
|
end
|
|
77
78
|
|
|
@@ -18,24 +18,29 @@ module ClassyEnum
|
|
|
18
18
|
# Priority.build(:low) or PriorityLow.new
|
|
19
19
|
#
|
|
20
20
|
def enum_classes(*enums)
|
|
21
|
-
self.
|
|
21
|
+
self.class_eval do
|
|
22
|
+
class_attribute :enum_options, :base_class
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
self.enum_options = enums.map(&:to_sym)
|
|
25
|
+
self.base_class = self
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
# # Use ActiveModel::AttributeMethods to define attribute? methods
|
|
28
|
+
attribute_method_suffix '?'
|
|
29
|
+
define_attribute_methods enums
|
|
30
|
+
end
|
|
31
|
+
end
|
|
28
32
|
|
|
29
|
-
|
|
33
|
+
def inherited(klass)
|
|
34
|
+
return if self == ClassyEnum::Base
|
|
30
35
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
define_attribute_methods enums
|
|
36
|
+
enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym
|
|
37
|
+
index = self.enum_options.index(enum) + 1
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
klass.class_eval do
|
|
40
|
+
@index = index
|
|
41
|
+
@option = enum
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
Object.const_set(klass_name, klass) unless Object.const_defined? klass_name
|
|
43
|
+
attr_accessor :owner, :serialize_as_json
|
|
39
44
|
end
|
|
40
45
|
end
|
|
41
46
|
|
|
@@ -50,9 +55,9 @@ module ClassyEnum
|
|
|
50
55
|
# Priority.build(:low) # => PriorityLow.new
|
|
51
56
|
def build(value, options={})
|
|
52
57
|
return value if value.blank?
|
|
53
|
-
return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self
|
|
58
|
+
return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self.enum_options.include? value.to_sym
|
|
54
59
|
|
|
55
|
-
object =
|
|
60
|
+
object = ("#{self}#{value.to_s.camelize}").constantize.new
|
|
56
61
|
object.owner = options[:owner]
|
|
57
62
|
object.serialize_as_json = options[:serialize_as_json]
|
|
58
63
|
object
|
|
@@ -70,7 +75,7 @@ module ClassyEnum
|
|
|
70
75
|
#
|
|
71
76
|
# Priority.all # => [PriorityLow.new, PriorityMedium.new, PriorityHigh.new]
|
|
72
77
|
def all
|
|
73
|
-
self
|
|
78
|
+
self.enum_options.map {|e| build(e) }
|
|
74
79
|
end
|
|
75
80
|
|
|
76
81
|
# Returns a 2D array for Rails select helper options.
|
|
@@ -98,10 +103,10 @@ module ClassyEnum
|
|
|
98
103
|
#
|
|
99
104
|
# Priority.valid_options # => "low, medium, high"
|
|
100
105
|
def valid_options
|
|
101
|
-
self
|
|
106
|
+
self.enum_options.map(&:to_s).join(', ')
|
|
102
107
|
end
|
|
103
108
|
|
|
104
|
-
|
|
109
|
+
private
|
|
105
110
|
|
|
106
111
|
# DSL setter method for reference to enum owner
|
|
107
112
|
def owner(owner)
|
|
@@ -79,17 +79,6 @@ module ClassyEnum
|
|
|
79
79
|
index <=> other.index
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
-
# Used by ActiveRecord::PredicateBuilder when building from a hash
|
|
83
|
-
def is_a?(klass)
|
|
84
|
-
return true if klass == ActiveRecord::Base
|
|
85
|
-
super(klass)
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
# Used by ActiveRecord::PredicateBuilder when building from a hash
|
|
89
|
-
def id
|
|
90
|
-
to_s
|
|
91
|
-
end
|
|
92
|
-
|
|
93
82
|
# Overrides as_json to remove owner reference recursion issues
|
|
94
83
|
def as_json(options=nil)
|
|
95
84
|
return to_s unless serialize_as_json
|
|
@@ -9,6 +9,18 @@ class CatBreed < ClassyEnum::Base
|
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
class CatBreedAbyssian < CatBreed
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class CatBreedBengal < CatBreed
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class CatBreedBirman < CatBreed
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class CatBreedPersian < CatBreed
|
|
22
|
+
end
|
|
23
|
+
|
|
12
24
|
class Cat < ActiveRecord::Base
|
|
13
25
|
classy_enum_attr :breed, :enum => :cat_breed
|
|
14
26
|
attr_accessor :color
|
|
@@ -31,11 +43,11 @@ describe Cat do
|
|
|
31
43
|
end
|
|
32
44
|
|
|
33
45
|
it 'should correctly serialize without the owner reference' do
|
|
34
|
-
abyssian.to_json.should ==
|
|
46
|
+
JSON.parse(abyssian.to_json)['cat']['breed'].should == 'abyssian'
|
|
35
47
|
end
|
|
36
48
|
|
|
37
49
|
it 'should convert the enum to a string when serializing' do
|
|
38
|
-
persian.to_json.should
|
|
50
|
+
JSON.parse(persian.to_json)['other_cat']['breed'].should be_a(Hash)
|
|
39
51
|
end
|
|
40
52
|
|
|
41
53
|
end
|
data/spec/classy_enum_spec.rb
CHANGED
|
@@ -12,7 +12,10 @@ class TestEnum < ClassyEnum::Base
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
class
|
|
15
|
+
class TestEnumOne < TestEnum
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class TestEnumTwo < TestEnum
|
|
16
19
|
def self.test_class_method?
|
|
17
20
|
true
|
|
18
21
|
end
|
|
@@ -22,20 +25,17 @@ class TestEnumTwo
|
|
|
22
25
|
end
|
|
23
26
|
end
|
|
24
27
|
|
|
25
|
-
|
|
28
|
+
class TestEnumThree < TestEnum
|
|
29
|
+
end
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
it "should define a TestEnum#{option.to_s.capitalize} class" do
|
|
29
|
-
Object.const_defined?("TestEnum#{option.to_s.capitalize}").should be_true
|
|
30
|
-
end
|
|
31
|
-
end
|
|
31
|
+
describe "A ClassyEnum Descendent" do
|
|
32
32
|
|
|
33
33
|
it "should return an array of enums" do
|
|
34
34
|
TestEnum.all.map(&:class).should == [TestEnumOne, TestEnumTwo, TestEnumThree]
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
it "should return an array of enums for a select tag" do
|
|
38
|
-
TestEnum.select_options.should == TestEnum
|
|
38
|
+
TestEnum.select_options.should == TestEnum.enum_options.map {|o| [TestEnum.build(o).name, TestEnum.build(o).to_s] }
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "should return a type error when adding an invalid option" do
|
data/spec/spec_helper.rb
CHANGED
|
@@ -3,11 +3,8 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
|
3
3
|
|
|
4
4
|
require 'rubygems'
|
|
5
5
|
require 'active_record'
|
|
6
|
-
require 'active_support'
|
|
7
|
-
require 'action_pack'
|
|
8
6
|
require 'action_view'
|
|
9
7
|
require 'action_controller'
|
|
10
|
-
require 'formtastic'
|
|
11
8
|
require 'rspec/rails'
|
|
12
9
|
require 'classy_enum'
|
|
13
10
|
|
|
@@ -53,6 +50,15 @@ class Breed < ClassyEnum::Base
|
|
|
53
50
|
enum_classes :golden_retriever, :snoop, :husky
|
|
54
51
|
end
|
|
55
52
|
|
|
53
|
+
class BreedGoldenRetriever < Breed
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class BreedSnoop < Breed
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class BreedHusky < Breed
|
|
60
|
+
end
|
|
61
|
+
|
|
56
62
|
class Dog < ActiveRecord::Base
|
|
57
63
|
classy_enum_attr :breed
|
|
58
64
|
end
|
|
@@ -68,31 +74,3 @@ end
|
|
|
68
74
|
class OtherDog < ActiveRecord::Base
|
|
69
75
|
classy_enum_attr :other_breed, :enum => :breed
|
|
70
76
|
end
|
|
71
|
-
|
|
72
|
-
module FormtasticSpecHelper
|
|
73
|
-
include ActionView::Context if defined?(ActionView::Context)
|
|
74
|
-
include ActionController::RecordIdentifier
|
|
75
|
-
include ActionView::Helpers::FormHelper
|
|
76
|
-
include ActionView::Helpers::FormTagHelper
|
|
77
|
-
include ActionView::Helpers::FormOptionsHelper
|
|
78
|
-
include Formtastic::SemanticFormHelper
|
|
79
|
-
|
|
80
|
-
def self.included(base)
|
|
81
|
-
base.class_eval do
|
|
82
|
-
|
|
83
|
-
attr_accessor :output_buffer
|
|
84
|
-
|
|
85
|
-
def protect_against_forgery?
|
|
86
|
-
false
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
module ActionView
|
|
95
|
-
class OutputBuffer < ActiveSupport::SafeBuffer
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: classy_enum
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,22 +9,27 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-
|
|
12
|
+
date: 2012-04-22 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rails
|
|
16
|
-
requirement:
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
|
-
- -
|
|
19
|
+
- - ~>
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version: 3.
|
|
21
|
+
version: 3.2.0
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements:
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 3.2.0
|
|
25
30
|
- !ruby/object:Gem::Dependency
|
|
26
31
|
name: jeweler
|
|
27
|
-
requirement:
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
28
33
|
none: false
|
|
29
34
|
requirements:
|
|
30
35
|
- - ~>
|
|
@@ -32,10 +37,15 @@ dependencies:
|
|
|
32
37
|
version: 1.6.2
|
|
33
38
|
type: :development
|
|
34
39
|
prerelease: false
|
|
35
|
-
version_requirements:
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ~>
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 1.6.2
|
|
36
46
|
- !ruby/object:Gem::Dependency
|
|
37
47
|
name: rspec-rails
|
|
38
|
-
requirement:
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
39
49
|
none: false
|
|
40
50
|
requirements:
|
|
41
51
|
- - ~>
|
|
@@ -43,29 +53,44 @@ dependencies:
|
|
|
43
53
|
version: 2.8.1
|
|
44
54
|
type: :development
|
|
45
55
|
prerelease: false
|
|
46
|
-
version_requirements:
|
|
47
|
-
- !ruby/object:Gem::Dependency
|
|
48
|
-
name: formtastic
|
|
49
|
-
requirement: &70293149207360 !ruby/object:Gem::Requirement
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
57
|
none: false
|
|
51
58
|
requirements:
|
|
52
59
|
- - ~>
|
|
53
60
|
- !ruby/object:Gem::Version
|
|
54
|
-
version:
|
|
61
|
+
version: 2.8.1
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: sqlite3
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ! '>='
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
55
70
|
type: :development
|
|
56
71
|
prerelease: false
|
|
57
|
-
version_requirements:
|
|
58
|
-
- !ruby/object:Gem::Dependency
|
|
59
|
-
name: sqlite3-ruby
|
|
60
|
-
requirement: &70293149206540 !ruby/object:Gem::Requirement
|
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
61
73
|
none: false
|
|
62
74
|
requirements:
|
|
63
75
|
- - ! '>='
|
|
64
76
|
- !ruby/object:Gem::Version
|
|
65
77
|
version: '0'
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: json
|
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ~>
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: 1.6.5
|
|
66
86
|
type: :development
|
|
67
87
|
prerelease: false
|
|
68
|
-
version_requirements:
|
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
89
|
+
none: false
|
|
90
|
+
requirements:
|
|
91
|
+
- - ~>
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: 1.6.5
|
|
69
94
|
description: A utility that adds class based enum functionality to ActiveRecord attributes
|
|
70
95
|
email: github@lette.us
|
|
71
96
|
executables: []
|
|
@@ -85,19 +110,20 @@ files:
|
|
|
85
110
|
- Rakefile
|
|
86
111
|
- VERSION
|
|
87
112
|
- classy_enum.gemspec
|
|
113
|
+
- gemfiles/Gemfile.rails-3.0.x
|
|
114
|
+
- gemfiles/Gemfile.rails-3.1.x
|
|
115
|
+
- gemfiles/Gemfile.rails-3.2.x
|
|
88
116
|
- init.rb
|
|
89
117
|
- lib/classy_enum.rb
|
|
90
118
|
- lib/classy_enum/attributes.rb
|
|
91
119
|
- lib/classy_enum/base.rb
|
|
92
120
|
- lib/classy_enum/class_methods.rb
|
|
93
121
|
- lib/classy_enum/instance_methods.rb
|
|
94
|
-
- lib/classy_enum/semantic_form_builder.rb
|
|
95
122
|
- lib/generators/classy_enum/classy_enum_generator.rb
|
|
96
123
|
- lib/generators/classy_enum/templates/enum.rb
|
|
97
124
|
- spec/active_record_spec.rb
|
|
98
125
|
- spec/classy_enum_attributes_spec.rb
|
|
99
126
|
- spec/classy_enum_owner_reference_spec.rb
|
|
100
|
-
- spec/classy_enum_semantic_form_builder_spec.rb
|
|
101
127
|
- spec/classy_enum_spec.rb
|
|
102
128
|
- spec/spec_helper.rb
|
|
103
129
|
homepage: http://github.com/beerlington/classy_enum
|
|
@@ -115,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
115
141
|
version: '0'
|
|
116
142
|
segments:
|
|
117
143
|
- 0
|
|
118
|
-
hash:
|
|
144
|
+
hash: 3580145781228639088
|
|
119
145
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
146
|
none: false
|
|
121
147
|
requirements:
|
|
@@ -124,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
124
150
|
version: '0'
|
|
125
151
|
requirements: []
|
|
126
152
|
rubyforge_project:
|
|
127
|
-
rubygems_version: 1.8.
|
|
153
|
+
rubygems_version: 1.8.19
|
|
128
154
|
signing_key:
|
|
129
155
|
specification_version: 3
|
|
130
156
|
summary: A class based enumerator utility for Ruby on Rails
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
module ClassyEnum
|
|
2
|
-
class SemanticFormBuilder < Formtastic::SemanticFormBuilder # :nodoc: all
|
|
3
|
-
def enum_select_input(method, options)
|
|
4
|
-
raise Error.invalid_classy_enum_object(method) unless object.respond_to? "#{method}_options"
|
|
5
|
-
|
|
6
|
-
enum_options = object.send("#{method}_options")
|
|
7
|
-
enum_class = enum_options[:enum].to_s.classify.constantize
|
|
8
|
-
|
|
9
|
-
options[:collection] = enum_class.select_options
|
|
10
|
-
options[:selected] = object.send(method).to_s
|
|
11
|
-
options[:include_blank] = enum_options[:allow_blank]
|
|
12
|
-
|
|
13
|
-
select_input(method, options)
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
module Error # :nodoc: all
|
|
18
|
-
def self.invalid_classy_enum_object(method)
|
|
19
|
-
raise "#{method} is not a ClassyEnum object. Make sure you've added 'classy_enum_attr :#{method}' to your model"
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
Formtastic::SemanticFormHelper.builder = ClassyEnum::SemanticFormBuilder
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
|
-
require 'classy_enum/semantic_form_builder'
|
|
3
|
-
|
|
4
|
-
describe 'using enum_select input' do
|
|
5
|
-
include FormtasticSpecHelper
|
|
6
|
-
|
|
7
|
-
# Reset output buffer
|
|
8
|
-
before { @output_buffer = "" }
|
|
9
|
-
|
|
10
|
-
context 'when building a form with a classy_enum select' do
|
|
11
|
-
context 'with an object that has the enum set' do
|
|
12
|
-
let(:output) do
|
|
13
|
-
semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
|
14
|
-
concat(builder.input(:breed, :as => :enum_select))
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should produce an unselected option tag for Golden Retriever' do
|
|
19
|
-
output.should =~ Regexp.new(%q{option value="golden_retriever">Golden Retriever})
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'should produce a selected option tag for Snoop' do
|
|
23
|
-
output.should =~ Regexp.new(%q{<option value="snoop" selected="selected">Snoop})
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'should not produce a blank option tag' do
|
|
27
|
-
output.should_not =~ Regexp.new(%q{<option value=""><})
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
context 'with an object that has a nil enum' do
|
|
32
|
-
let(:output) do
|
|
33
|
-
semantic_form_for(Dog.new, :url => "/") do |builder|
|
|
34
|
-
concat(builder.input(:breed, :as => :enum_select))
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it 'should produce an unselected option tag for Golden Retriever' do
|
|
39
|
-
output.should =~ Regexp.new(%q{<option value="golden_retriever">Golden Retriever})
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it 'should not produce an selected option tag' do
|
|
43
|
-
output.should_not =~ Regexp.new("selected")
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context 'with an object that allows blank enums' do
|
|
48
|
-
let(:output) do
|
|
49
|
-
semantic_form_for(AllowBlankBreedDog.new, :url => "/") do |builder|
|
|
50
|
-
concat(builder.input(:breed, :as => :enum_select))
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it 'should produce an unselected option tag for Golden Retriever' do
|
|
55
|
-
output.should =~ Regexp.new(%q{<option value="golden_retriever">Golden Retriever})
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it 'should produce a blank option tag' do
|
|
59
|
-
output.should =~ Regexp.new(%q{<option value=""><})
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
context 'with an object that allows nil enums' do
|
|
64
|
-
let(:output) do
|
|
65
|
-
semantic_form_for(AllowNilBreedDog.new, :url => "/") do |builder|
|
|
66
|
-
concat(builder.input(:breed, :as => :enum_select))
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
it 'should produce an unselected option tag for Golden Retriever' do
|
|
71
|
-
output.should =~ Regexp.new(%q{<option value="golden_retriever">Golden Retriever})
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it 'should produce a blank option tag' do
|
|
75
|
-
output.should_not =~ Regexp.new(%q{<option value=""><})
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it 'should raise an error if the attribute is not a ClassyEnum object' do
|
|
82
|
-
lambda do
|
|
83
|
-
semantic_form_for(Dog.new(:breed => :snoop), :url => "/") do |builder|
|
|
84
|
-
concat(builder.input(:id, :as => :enum_select))
|
|
85
|
-
end
|
|
86
|
-
end.should raise_error("id is not a ClassyEnum object. Make sure you've added 'classy_enum_attr :id' to your model")
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
it 'should raise an error if the attribute is not a ClassyEnum object and its value is nil' do
|
|
90
|
-
lambda do
|
|
91
|
-
semantic_form_for(Dog.new, :url => "/") do |builder|
|
|
92
|
-
concat(builder.input(:id, :as => :enum_select))
|
|
93
|
-
end
|
|
94
|
-
end.should raise_error("id is not a ClassyEnum object. Make sure you've added 'classy_enum_attr :id' to your model")
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
end
|
|
98
|
-
|