classy_enum 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,116 @@
1
+ # ClassyEnum Changelog
2
+
3
+ ## 1.3.2
4
+
5
+ * Added support for Ruby 1.9.3
6
+ * Stop hijacking enum initialize method
7
+
8
+ ## 1.3.1
9
+
10
+ * Fixed JSON recursion issue
11
+
12
+ ## 1.3.0
13
+
14
+ * Added support for Rails 3.1.x
15
+
16
+ ## 1.2.0
17
+
18
+ * Added support for owner enum reference
19
+
20
+ ## 1.1.0
21
+
22
+ * Code refactor
23
+
24
+ ## 1.0.0
25
+
26
+ * Removed support for Rails 2.x.x
27
+ * Fixed support for validates_uniqueness_of with an enum scope
28
+
29
+ ## 0.9.1
30
+
31
+ * Fixed Rails dependency requirement
32
+
33
+ ## 0.9.0
34
+
35
+ * Further decoupled Formtastic support
36
+
37
+ ## 0.8.1
38
+
39
+ * Added `:allow_blank` option
40
+ * Added blank/nil option support to Formtastic
41
+ * Fixed documentation typos
42
+
43
+ ## 0.8.0
44
+
45
+ * Updated development dependencies
46
+ * Changed API for column and enum name differences
47
+
48
+ ## 0.7.1
49
+
50
+ * Added Comparable support
51
+ * Fixed an issue with invalid objects set to empty string
52
+ * Added allow blank support to Formtastic
53
+ * Improved error messages
54
+
55
+ ## 0.7.0
56
+
57
+ * Added "boolean" methods (i.e. `instance.enum?`)
58
+ * Improved documentation
59
+ * Updated generators to make inheritance more explicit
60
+ * Removed ActiveRecord dependency
61
+
62
+ ## 0.6.1
63
+
64
+ * Fixed validates_presence_of support
65
+ * Improved Formtastic support
66
+
67
+ ## 0.6.0
68
+
69
+ * Changed enums to use inheritance instead of mixins
70
+ * Fixed issue with generators
71
+ * Fixed documentation typos
72
+
73
+ ## 0.5.0
74
+
75
+ * Fixed documentation
76
+
77
+ ## 0.4.2
78
+
79
+ * Cleaned up hacky support for Rails 3.0.x
80
+ * Fixed Formtastic load support
81
+
82
+ ## 0.4.1
83
+
84
+ * Fixed validation issue in Rails 3.0.x
85
+
86
+ ## 0.4.0
87
+
88
+ * Added enum generator for Rails 3.0.x
89
+ * Added support for Ruby 1.9.2
90
+ * Fixed support for apps that are not using Formtastic
91
+
92
+ ## 0.3.1
93
+
94
+ * Fixed Formtastic helper support w/ Rails 3.0.x
95
+
96
+ ## 0.3.0
97
+
98
+ * Added custom Formtastic input type
99
+
100
+ ## 0.2.0
101
+
102
+ * Internal changes to how methods are defined
103
+
104
+ ## 0.1.0
105
+
106
+ * Added enum generator for Rails 2.3.x
107
+ * Internal changes to how ActiveRecord is extended
108
+ * More test coverage
109
+
110
+ ## 0.0.2
111
+
112
+ * Fixed initialized constant warning
113
+
114
+ ## 0.0.1
115
+
116
+ * Initial Release
data/Gemfile CHANGED
@@ -4,7 +4,8 @@ gem 'rails', '>= 3.0.0'
4
4
 
5
5
  group :development do
6
6
  gem "jeweler", "~> 1.6.2"
7
- gem "rspec-rails", "~> 2.6.1"
7
+ gem "rspec-rails", "~> 2.8.1"
8
8
  gem 'formtastic', '~> 1.2.4'
9
9
  gem 'sqlite3-ruby', :require => 'sqlite3'
10
+ # gem 'ruby-debug19', :require => 'false'
10
11
  end
data/Gemfile.lock CHANGED
@@ -30,7 +30,7 @@ GEM
30
30
  activesupport (3.0.5)
31
31
  arel (2.0.10)
32
32
  builder (2.1.2)
33
- diff-lcs (1.1.2)
33
+ diff-lcs (1.1.3)
34
34
  erubis (2.6.6)
35
35
  abstract (>= 1.0.0)
36
36
  formtastic (1.2.4)
@@ -50,7 +50,7 @@ GEM
50
50
  treetop (~> 1.4.8)
51
51
  mime-types (1.16)
52
52
  polyglot (0.3.2)
53
- rack (1.2.3)
53
+ rack (1.2.5)
54
54
  rack-mount (0.6.14)
55
55
  rack (>= 1.0.0)
56
56
  rack-test (0.5.7)
@@ -68,26 +68,26 @@ GEM
68
68
  activesupport (= 3.0.5)
69
69
  rake (>= 0.8.7)
70
70
  thor (~> 0.14.4)
71
- rake (0.9.2)
72
- rspec (2.6.0)
73
- rspec-core (~> 2.6.0)
74
- rspec-expectations (~> 2.6.0)
75
- rspec-mocks (~> 2.6.0)
76
- rspec-core (2.6.4)
77
- rspec-expectations (2.6.0)
71
+ rake (0.9.2.2)
72
+ rspec (2.8.0)
73
+ rspec-core (~> 2.8.0)
74
+ rspec-expectations (~> 2.8.0)
75
+ rspec-mocks (~> 2.8.0)
76
+ rspec-core (2.8.0)
77
+ rspec-expectations (2.8.0)
78
78
  diff-lcs (~> 1.1.2)
79
- rspec-mocks (2.6.0)
80
- rspec-rails (2.6.1)
81
- actionpack (~> 3.0)
82
- activesupport (~> 3.0)
83
- railties (~> 3.0)
84
- rspec (~> 2.6.0)
79
+ rspec-mocks (2.8.0)
80
+ rspec-rails (2.8.1)
81
+ actionpack (>= 3.0)
82
+ activesupport (>= 3.0)
83
+ railties (>= 3.0)
84
+ rspec (~> 2.8.0)
85
85
  sqlite3-ruby (1.3.1)
86
86
  thor (0.14.6)
87
87
  treetop (1.4.10)
88
88
  polyglot
89
89
  polyglot (>= 0.3.1)
90
- tzinfo (0.3.29)
90
+ tzinfo (0.3.31)
91
91
 
92
92
  PLATFORMS
93
93
  ruby
@@ -96,5 +96,5 @@ DEPENDENCIES
96
96
  formtastic (~> 1.2.4)
97
97
  jeweler (~> 1.6.2)
98
98
  rails (>= 3.0.0)
99
- rspec-rails (~> 2.6.1)
99
+ rspec-rails (~> 2.8.1)
100
100
  sqlite3-ruby
@@ -1,40 +1,40 @@
1
- h1. classy_enum "!https://secure.travis-ci.org/beerlington/classy_enum.png?branch=master!":http://travis-ci.org/beerlington/classy_enum
1
+ # classy_enum
2
2
 
3
- ClassyEnum is a Ruby on Rails gem that adds class-based enumerator functionality to ActiveRecord attributes.
3
+ [![Build Status](https://secure.travis-ci.org/beerlington/classy_enum.png?branch=master)](http://travis-ci.org/beerlington/classy_enum)
4
4
 
5
- !http://dl.dropbox.com/u/1934677/classy.jpg!
5
+ ClassyEnum is a Ruby on Rails gem that adds class-based enumerator functionality to ActiveRecord attributes.
6
6
 
7
- h2. Rails & Ruby Versions Supported
7
+ ## Rails & Ruby Versions Supported
8
8
 
9
9
  *Rails:*
10
10
 
11
- * 3.1.0.rc: Mostly functional, known issue with using uniqueness validation with ClassyEnum attribute as a scope
12
- * 3.0.0 - 3.0.9: Fully tested in a production application, no known issues
13
- * 2.3.x: If you need support for Rails 2.3.x, please install "version 0.9.1":https://rubygems.org/gems/classy_enum/versions/0.9.1
11
+ * 3.0.x - 3.2.x: Fully tested in a production application. See below
12
+ for known issues.
13
+ * 2.3.x: If you need support for Rails 2.3.x, please install [version 0.9.1](https://rubygems.org/gems/classy_enum/versions/0.9.1)
14
14
 
15
- *Ruby:* Ruby 1.8.7 and 1.9.2 both tested and supported
15
+ *Ruby:* Ruby 1.8.7, 1.9.2, and 1.9.3 are tested and supported
16
16
 
17
- h2. Installation
17
+ ## Installation
18
18
 
19
- The gem is hosted at "rubygems.org":https://rubygems.org/gems/classy_enum
19
+ The gem is hosted at [rubygems.org](https://rubygems.org/gems/classy_enum)
20
20
 
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.
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
- h2. Example Usage
23
+ ## Example Usage
24
24
 
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.
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.
26
26
 
27
- h3. 1. Generate the Enum
27
+ ### 1. Generate the Enum
28
28
 
29
29
  The fastest way to get up and running with ClassyEnum is to use the built-in Rails generator like so:
30
30
 
31
- <pre>
31
+ ```
32
32
  rails g classy_enum Priority low medium high
33
- </pre>
33
+ ```
34
34
 
35
35
  A new enum template file will be created at app/enums/priority.rb that will look like:
36
36
 
37
- <pre>
37
+ ```ruby
38
38
  class Priority < ClassyEnum::Base
39
39
  enum_classes :low, :medium, :high
40
40
  end
@@ -47,19 +47,19 @@ end
47
47
 
48
48
  class PriorityHigh < Priority
49
49
  end
50
- </pre>
50
+ ```
51
51
 
52
- The @enum_classes@ macro will add all the ClassyEnum behavior, which is described further down in this document.
52
+ The `enum_classes` macro will add all the ClassyEnum behavior, which is described further down in this document.
53
53
 
54
- h3. 2. Customize the Enum
54
+ ### 2. Customize the Enum
55
55
 
56
56
  The generator creates a default setup, but each enum member can be changed to fit your needs.
57
57
 
58
- Using the @enum_classes@ method, I have defined three priority levels: low, medium, and high. Each priority level can have different properties and methods associated with it.
58
+ Using the `enum_classes` method, I have defined three priority levels: low, medium, and high. Each priority level can have different properties and methods associated with it.
59
59
 
60
- 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.
60
+ 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
61
 
62
- <pre>
62
+ ```ruby
63
63
  class Priority < ClassyEnum::Base
64
64
  enum_classes :low, :medium, :high
65
65
 
@@ -73,34 +73,34 @@ class PriorityHigh < Priority
73
73
  true
74
74
  end
75
75
  end
76
- </pre>
76
+ ```
77
77
 
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.
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
79
 
80
- h3. 3. Setup the ActiveRecord model
80
+ ### 3. Setup the ActiveRecord model
81
81
 
82
82
  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:
83
83
 
84
- <pre>
84
+ ```ruby
85
85
  create_table "alarms", :force => true do |t|
86
86
  t.string "priority"
87
87
  t.boolean "enabled"
88
88
  end
89
- </pre>
89
+ ```
90
90
 
91
- Then in my model I've added a line that calls @classy_enum_attr@ with a single argument representing the enum I want to associate with my model. I am also delegating the send_email? method to my Priority enum class.
91
+ Then in my model I've added a line that calls `classy_enum_attr` with a single argument representing the enum I want to associate with my model. I am also delegating the send_email? method to my Priority enum class.
92
92
 
93
- <pre>
93
+ ```ruby
94
94
  class Alarm < ActiveRecord::Base
95
95
  classy_enum_attr :priority
96
96
 
97
97
  delegate :send_email?, :to => :priority
98
98
  end
99
- </pre>
99
+ ```
100
100
 
101
101
  With this setup, I can now do the following:
102
102
 
103
- <pre>
103
+ ```ruby
104
104
  @alarm = Alarm.create(:priority => :medium)
105
105
 
106
106
  @alarm.priority # => PriorityMedium
@@ -113,19 +113,19 @@ With this setup, I can now do the following:
113
113
  @alarm.send_email? # => false
114
114
  @alarm.priority = :high
115
115
  @alarm.send_email? # => true
116
- </pre>
116
+ ```
117
117
 
118
- The enum field works like any other model attribute. It can be mass-assigned using @update_attribute(s)@.
118
+ The enum field works like any other model attribute. It can be mass-assigned using `update_attribute(s)`.
119
119
 
120
- h2. Back reference to owning object
120
+ ## Back reference to owning object
121
121
 
122
- In some cases you may want an enum class to be able to reference the owning object (an instance of the active record model). Think of it as a @belongs_to@ relationship, where the enum can reference its owning object.
122
+ In some cases you may want an enum class to be able to reference the owning object (an instance of the active record model). Think of it as a `belongs_to` relationship, where the enum can reference its owning object.
123
123
 
124
- In order to create the back reference, you must declare how you wish to refer to the owner using the @owner@ class method.
124
+ In order to create the back reference, you must declare how you wish to refer to the owner using the `owner` class method.
125
125
 
126
126
  For example:
127
127
 
128
- <pre>
128
+ ```ruby
129
129
  class Priority < ClassyEnum::Base
130
130
  enum_classes :low, :medium, :high
131
131
  owner :alarm
@@ -136,108 +136,124 @@ class PriorityHigh < Priority
136
136
  alarm.enabled?
137
137
  end
138
138
  end
139
- </pre>
139
+ ```
140
140
 
141
141
  In the above example, high priority alarms are only emailed if the owning alarm is enabled.
142
142
 
143
- <pre>
143
+ ```ruby
144
144
  @alarm = Alarm.create(:priority => :high, :enabled => true)
145
145
 
146
146
  # Should this alarm send an email?
147
147
  @alarm.send_email? # => true
148
148
  @alarm.enabled = false
149
149
  @alarm.send_email? # => false
150
- </pre>
150
+ ```
151
151
 
152
- h2. Serializing as JSON
152
+ ## Serializing as JSON
153
153
 
154
154
  By default, the enum will be serialized as a string representing the value:
155
155
 
156
- <pre>
156
+ ```ruby
157
157
  @alarm = Alarm.create(:priority => :high, :enabled => true)
158
158
  @alarm.to_json.should == "{\"alarm\":{\"priority\":\"high\"}}"
159
- </pre>
159
+ ```
160
160
 
161
- This behavior can be overridden by using the @:serialize_as_json => true@ option in your ActiveRecord model:
161
+ This behavior can be overridden by using the `:serialize_as_json => true` option in your ActiveRecord model:
162
162
 
163
- <pre>
163
+ ```ruby
164
164
  class Alarm < ActiveRecord::Base
165
165
  classy_enum_attr :priority, :serialize_as_json => true
166
166
  end
167
167
 
168
168
  @alarm = Alarm.create(:priority => :high, :enabled => true)
169
169
  @alarm.to_json.should == "{\"alarm\":{\"priority\":{}}}"
170
- </pre>
170
+ ```
171
171
 
172
- h2. Special Cases
172
+ ## Special Cases and Known Issue
173
173
 
174
- What if your enum class name is not the same as your model's attribute name? No problem! Just use a second arugment in @classy_enum_attr@ to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
174
+ What if your enum class name is not the same as your model's attribute name? No problem! Just use a second arugment in `classy_enum_attr` to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
175
175
 
176
- <pre>
176
+ ```ruby
177
177
  class Alarm < ActiveRecord::Base
178
178
  classy_enum_attr :alarm_priority, :enum => :priority
179
179
  end
180
180
 
181
181
  @alarm = Alarm.create(:alarm_priority => :medium)
182
182
  @alarm.alarm_priority # => PriorityMedium
183
- </pre>
183
+ ```
184
+
185
+ There is an [issue](https://github.com/beerlington/classy_enum/issues/8)
186
+ with Rails 3.1 and higher when using validates_uniqueness_of
187
+ and a scope that is the enum field. This issue also occurs when using
188
+ `composed_of` and is not a bug with ClassyEnum. As a workaround to this
189
+ problem, you can use the reader suffix option when declaring your field:
190
+
191
+ ```ruby
192
+ class Alarm < ActiveRecord::Base
193
+ classy_enum_attr :priority, :suffix => 'type'
194
+ end
195
+
196
+ alarm = Alarm.create(:priority => :high)
197
+ alarm.priority # => 'high'
198
+ alarm.priority_type # instance of PriorityHigh enum
199
+ ```
184
200
 
185
- h2. Model Validation
201
+ ## Model Validation
186
202
 
187
- An ActiveRecord validator @validates_inclusion_of :field, :in => ENUM.all@ is automatically added to your model when you use @classy_enum_attr@.
203
+ An ActiveRecord validator `validates_inclusion_of :field, :in => ENUM.all` is automatically added to your model when you use `classy_enum_attr`.
188
204
 
189
205
  If your enum only has members low, medium, and high, then the following validation behavior would be expected:
190
206
 
191
- <pre>
207
+ ```ruby
192
208
  @alarm = Alarm.new(:priority => :really_high)
193
209
  @alarm.valid? # => false
194
210
  @alarm.priority = :high
195
211
  @alarm.valid? # => true
196
- </pre>
212
+ ```
197
213
 
198
- To allow nil or blank values, you can pass in :allow_nil and :allow_blank as options to classy_enum_attr, like so:
214
+ To allow nil or blank values, you can pass in `:allow_nil` and `:allow_blank` as options to `classy_enum_attr`:
199
215
 
200
- <pre>
216
+ ```ruby
201
217
  class Alarm < ActiveRecord::Base
202
218
  classy_enum_attr :priority, :allow_nil => true
203
219
  end
204
220
 
205
221
  @alarm = Alarm.new(:priority => nil)
206
222
  @alarm.valid? # => true
207
- </pre>
223
+ ```
208
224
 
209
- h2. Working with ClassyEnum outside of ActiveRecord
225
+ ## Working with ClassyEnum outside of ActiveRecord
210
226
 
211
227
  While ClassyEnum was designed to be used directly with ActiveRecord, it can also be used outside of it. Here are some examples based on the enum class defined earlier in this document.
212
228
 
213
229
  Instantiate an enum member subclass *PriorityLow*
214
230
 
215
- <pre>
231
+ ```ruby
216
232
  # These statements are all equivalent
217
233
  low = Priority.build(:low)
218
234
  low = Priority.build('low')
219
235
  low = Priority.find(:low)
220
236
  low = PriorityLow.new
221
- </pre>
237
+ ```
222
238
 
223
239
  Get a list of the valid enum options
224
240
 
225
- <pre>
241
+ ```ruby
226
242
  Priority.valid_options # => low, medium, high
227
- </pre>
243
+ ```
228
244
 
229
- h2. Formtastic Support
245
+ ## Formtastic Support
230
246
 
231
247
  To add ClassyEnum support to Formtastic, add the following to your formtastic.rb initializer (config/initializers/formtastic.rb):
232
248
 
233
- <pre>
249
+ ```ruby
234
250
  require 'classy_enum/semantic_form_builder'
235
- </pre>
251
+ ```
236
252
 
237
- Then in your Formtastic view forms, use this syntax: @<%= f.input :priority, :as => :enum_select %>@
253
+ Then in your Formtastic view forms, use this syntax: `<%= f.input :priority, :as => :enum_select %>`
238
254
 
239
- Note: ClassyEnum respects the @:allow_blank@ and @:allow_nil@ options and will include a blank select option in these cases
255
+ Note: ClassyEnum respects the `:allow_blank` and `:allow_nil` options and will include a blank select option in these cases
240
256
 
241
- h2. Copyright
257
+ ## Copyright
242
258
 
243
- Copyright (c) 2011 Peter Brown. See LICENSE for details.
259
+ Copyright (c) 2011 [Peter Brown](https://github.com/beerlington). See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.4.0
data/classy_enum.gemspec CHANGED
@@ -4,26 +4,27 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{classy_enum}
8
- s.version = "1.3.2"
7
+ s.name = "classy_enum"
8
+ s.version = "1.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Peter Brown}]
12
- s.date = %q{2011-11-12}
13
- s.description = %q{A utility that adds class based enum functionality to ActiveRecord attributes}
14
- s.email = %q{github@lette.us}
11
+ s.authors = ["Peter Brown"]
12
+ s.date = "2012-02-05"
13
+ s.description = "A utility that adds class based enum functionality to ActiveRecord attributes"
14
+ s.email = "github@lette.us"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.textile"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
21
  ".rvmrc",
22
22
  ".travis.yml",
23
+ "CHANGELOG.md",
23
24
  "Gemfile",
24
25
  "Gemfile.lock",
25
26
  "LICENSE",
26
- "README.textile",
27
+ "README.md",
27
28
  "Rakefile",
28
29
  "VERSION",
29
30
  "classy_enum.gemspec",
@@ -43,11 +44,11 @@ Gem::Specification.new do |s|
43
44
  "spec/classy_enum_spec.rb",
44
45
  "spec/spec_helper.rb"
45
46
  ]
46
- s.homepage = %q{http://github.com/beerlington/classy_enum}
47
- s.licenses = [%q{MIT}]
48
- s.require_paths = [%q{lib}]
49
- s.rubygems_version = %q{1.8.6}
50
- s.summary = %q{A class based enumerator utility for Ruby on Rails}
47
+ s.homepage = "http://github.com/beerlington/classy_enum"
48
+ s.licenses = ["MIT"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = "1.8.15"
51
+ s.summary = "A class based enumerator utility for Ruby on Rails"
51
52
 
52
53
  if s.respond_to? :specification_version then
53
54
  s.specification_version = 3
@@ -55,20 +56,20 @@ Gem::Specification.new do |s|
55
56
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
57
  s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
57
58
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
58
- s.add_development_dependency(%q<rspec-rails>, ["~> 2.6.1"])
59
+ s.add_development_dependency(%q<rspec-rails>, ["~> 2.8.1"])
59
60
  s.add_development_dependency(%q<formtastic>, ["~> 1.2.4"])
60
61
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
61
62
  else
62
63
  s.add_dependency(%q<rails>, [">= 3.0.0"])
63
64
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
64
- s.add_dependency(%q<rspec-rails>, ["~> 2.6.1"])
65
+ s.add_dependency(%q<rspec-rails>, ["~> 2.8.1"])
65
66
  s.add_dependency(%q<formtastic>, ["~> 1.2.4"])
66
67
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
67
68
  end
68
69
  else
69
70
  s.add_dependency(%q<rails>, [">= 3.0.0"])
70
71
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
71
- s.add_dependency(%q<rspec-rails>, ["~> 2.6.1"])
72
+ s.add_dependency(%q<rspec-rails>, ["~> 2.8.1"])
72
73
  s.add_dependency(%q<formtastic>, ["~> 1.2.4"])
73
74
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
74
75
  end
@@ -22,22 +22,31 @@ module ClassyEnum
22
22
  options = args.extract_options!
23
23
 
24
24
  attribute = args[0]
25
+
25
26
  enum = options[:enum] || attribute
26
27
  allow_blank = options[:allow_blank] || false
27
28
  allow_nil = options[:allow_nil] || false
28
29
  serialize_as_json = options[:serialize_as_json] || false
29
30
 
31
+ reader_method = attribute.to_s
32
+ reader_method += "_#{options[:suffix]}" if options.has_key?(:suffix)
33
+
30
34
  klass = enum.to_s.camelize.constantize
31
35
 
36
+ valid_attributes = reader_method == attribute.to_s ? klass.all : klass.all.map(&:to_s)
37
+
32
38
  self.instance_eval do
33
39
 
34
40
  # Add ActiveRecord validation to ensure it won't be saved unless it's an option
35
- validates_inclusion_of attribute, :in => klass.all, :message => "must be one of #{klass.valid_options}",
36
- :allow_blank => allow_blank, :allow_nil => allow_nil
41
+ validates_inclusion_of attribute,
42
+ :in => valid_attributes,
43
+ :message => "must be one of #{klass.valid_options}",
44
+ :allow_blank => allow_blank,
45
+ :allow_nil => allow_nil
37
46
 
38
47
  # Define getter method that returns a ClassyEnum instance
39
- define_method attribute do
40
- klass.build(super(), :owner => self, :serialize_as_json => serialize_as_json)
48
+ define_method reader_method do
49
+ klass.build(read_attribute(attribute), :owner => self, :serialize_as_json => serialize_as_json)
41
50
  end
42
51
 
43
52
  # Define setter method that accepts either string or symbol for member
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  class ActiveDog < ActiveRecord::Base
4
- classy_enum_attr :breed
4
+ classy_enum_attr :breed, :suffix => 'type'
5
5
 
6
6
  validates :name,
7
7
  :presence => true,
@@ -12,24 +12,35 @@ class ActiveDog < ActiveRecord::Base
12
12
  end
13
13
 
14
14
  describe ActiveDog do
15
- before do
16
- ActiveDog.destroy_all
17
- @golden = ActiveDog.create!(:name => 'Sebastian', :breed => :golden_retriever)
18
- @husky = ActiveDog.create!(:name => 'Sirius', :breed => :husky)
19
- end
20
15
 
21
- it 'should allow validation with classy enum scope' do
22
- dog = ActiveDog.new(:name => 'Sirius', :breed => :golden_retriever)
23
- dog.should be_valid
24
- dog.breed.golden_retriever?.should be_true
16
+ context 'valid instance' do
17
+ subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retriever) }
18
+
19
+ it { should have(:no).errors_on(:breed) }
20
+ its(:breed_type) { should be_a_golden_retriever }
21
+ its(:breed) { should == 'golden_retriever' }
25
22
  end
26
23
 
27
- it 'should know all dogs' do
28
- ActiveDog.all.should include(@golden, @husky)
24
+ context 'invalid instance' do
25
+ subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retrievers) }
26
+
27
+ it { should have(1).error_on(:breed) }
29
28
  end
30
29
 
31
- it 'should have a working scope' do
32
- ActiveDog.goldens.should include(@golden)
33
- ActiveDog.goldens.should_not include(@husky)
30
+ context 'scopes' do
31
+ let!(:golden) { ActiveDog.create!(:name => 'Sebastian', :breed => :golden_retriever) }
32
+ let!(:husky) { ActiveDog.create!(:name => 'Sirius', :breed => :husky) }
33
+
34
+ after { ActiveDog.destroy_all }
35
+
36
+ it 'should know all dogs' do
37
+ ActiveDog.all.should include(golden, husky)
38
+ end
39
+
40
+ it 'should have a working scope' do
41
+ ActiveDog.goldens.should include(golden)
42
+ ActiveDog.goldens.should_not include(husky)
43
+ end
34
44
  end
45
+
35
46
  end
data/spec/spec_helper.rb CHANGED
@@ -2,13 +2,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'rubygems'
5
- require 'rspec'
6
5
  require 'active_record'
7
6
  require 'active_support'
8
7
  require 'action_pack'
9
8
  require 'action_view'
10
9
  require 'action_controller'
11
10
  require 'formtastic'
11
+ require 'rspec/rails'
12
12
  require 'classy_enum'
13
13
 
14
14
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
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: 1.3.2
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-12 00:00:00.000000000Z
12
+ date: 2012-02-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70109521646960 !ruby/object:Gem::Requirement
16
+ requirement: &70293149209880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70109521646960
24
+ version_requirements: *70293149209880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jeweler
27
- requirement: &70109521646460 !ruby/object:Gem::Requirement
27
+ requirement: &70293149209000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,21 +32,21 @@ dependencies:
32
32
  version: 1.6.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70109521646460
35
+ version_requirements: *70293149209000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &70109521645440 !ruby/object:Gem::Requirement
38
+ requirement: &70293149208180 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- version: 2.6.1
43
+ version: 2.8.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70109521645440
46
+ version_requirements: *70293149208180
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: formtastic
49
- requirement: &70109521644780 !ruby/object:Gem::Requirement
49
+ requirement: &70293149207360 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.2.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70109521644780
57
+ version_requirements: *70293149207360
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3-ruby
60
- requirement: &70109521644180 !ruby/object:Gem::Requirement
60
+ requirement: &70293149206540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,22 +65,23 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70109521644180
68
+ version_requirements: *70293149206540
69
69
  description: A utility that adds class based enum functionality to ActiveRecord attributes
70
70
  email: github@lette.us
71
71
  executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files:
74
74
  - LICENSE
75
- - README.textile
75
+ - README.md
76
76
  files:
77
77
  - .document
78
78
  - .rvmrc
79
79
  - .travis.yml
80
+ - CHANGELOG.md
80
81
  - Gemfile
81
82
  - Gemfile.lock
82
83
  - LICENSE
83
- - README.textile
84
+ - README.md
84
85
  - Rakefile
85
86
  - VERSION
86
87
  - classy_enum.gemspec
@@ -114,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
115
  version: '0'
115
116
  segments:
116
117
  - 0
117
- hash: -4456031736000763211
118
+ hash: -3670451766157368452
118
119
  required_rubygems_version: !ruby/object:Gem::Requirement
119
120
  none: false
120
121
  requirements:
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  version: '0'
124
125
  requirements: []
125
126
  rubyforge_project:
126
- rubygems_version: 1.8.6
127
+ rubygems_version: 1.8.15
127
128
  signing_key:
128
129
  specification_version: 3
129
130
  summary: A class based enumerator utility for Ruby on Rails