Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removing :suffix option from classy_enum_attr

This was a temporary hack to get around a bug in ActiveRecord uniqueness
validations.
  • Loading branch information...
commit da9dcab51293b1f336aa1a6ebd414e18676ff65e 1 parent 22c4ff4
@beerlington beerlington authored
View
1  CHANGELOG.md
@@ -7,6 +7,7 @@
* Removing ClassyEnum::Base.valid_options
* Removing ClassyEnum::Base.find
* Removing ClassyEnum::Base#name
+* Removing :suffix option from classy_enum_attr
* Enforce use of namespacing for subclasses (Parent::Child < Parent)
* Use require instead of autoload
* Lots of code refactoring
View
15 README.md
@@ -197,7 +197,7 @@ end
## Special Cases
-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*.
+What if your enum class name is not the same as your model's attribute name? No problem! Just use a second argument in `classy_enum_attr` to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
```ruby
class Alarm < ActiveRecord::Base
@@ -208,19 +208,6 @@ end
@alarm.alarm_priority # => Priority::Medium
```
-If you would like the default getter method to return a string, you can
-use the optional *:suffix* option for the enum getter:
-
-```ruby
-class Alarm < ActiveRecord::Base
- classy_enum_attr :priority, :suffix => 'type'
-end
-
-alarm = Alarm.create(:priority => :high)
-alarm.priority # => 'high'
-alarm.priority_type # instance of Priority::High enum
-```
-
## Model Validation
An ActiveRecord validator `validates_inclusion_of :field, :in => ENUM.all` is automatically added to your model when you use `classy_enum_attr`.
View
11 lib/classy_enum/active_record.rb
@@ -21,9 +21,6 @@ module ActiveRecord
#
# # Allow enum value to be blank
# classy_enum_attr :priority, :allow_blank => true
- #
- # # Use a different name for the enum method, preserving ActiveRecord getter method
- # classy_enum_attr :priority, :suffix => '_type'
def classy_enum_attr(*args)
options = args.extract_options!
@@ -34,21 +31,17 @@ def classy_enum_attr(*args)
allow_nil = options[:allow_nil] || false
serialize_as_json = options[:serialize_as_json] || false
- reader_method = attribute.to_s
- reader_method += "_#{options[:suffix]}" if options.has_key?(:suffix)
-
- valid_attributes = reader_method == attribute.to_s ? enum.all : enum.all.map(&:to_s)
error_message = "must be #{enum.all.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ')}"
# Add ActiveRecord validation to ensure it won't be saved unless it's an option
validates_inclusion_of attribute,
- :in => valid_attributes,
+ :in => enum.all,
:message => error_message,
:allow_blank => allow_blank,
:allow_nil => allow_nil
# Define getter method that returns a ClassyEnum instance
- define_method reader_method do
+ define_method attribute do
enum.build(read_attribute(attribute),
:owner => self,
:serialize_as_json => serialize_as_json,
View
22 spec/classy_enum/active_record_spec.rb
@@ -96,28 +96,12 @@ class OtherDog < ActiveRecord::Base
end
class ActiveDog < ActiveRecord::Base
- classy_enum_attr :breed, :suffix => 'type'
classy_enum_attr :color
-
- validates :name,
- :presence => true,
- :uniqueness => { :scope => [:breed] }
-
validates_uniqueness_of :name, :scope => :color
-
scope :goldens, where(:breed => 'golden_retriever')
end
describe ActiveDog do
-
- context 'valid instance' do
- subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retriever, :color => :black) }
-
- it { should have(:no).errors_on(:breed) }
- its(:breed_type) { should be_a_golden_retriever }
- its(:breed) { should == 'golden_retriever' }
- end
-
context 'uniqueness on name' do
subject { ActiveDog.new(:name => 'Kitteh', :breed => :golden_retriever, :color => :black) }
it { should be_valid }
@@ -131,12 +115,6 @@ class ActiveDog < ActiveRecord::Base
end
end
- context 'invalid instance' do
- subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retrievers, :color => :white) }
-
- it { should have(1).error_on(:breed) }
- end
-
context 'scopes' do
let!(:golden) { ActiveDog.create!(:name => 'Sebastian', :breed => :golden_retriever, :color => :white) }
let!(:husky) { ActiveDog.create!(:name => 'Sirius', :breed => :husky, :color => :black) }
Please sign in to comment.
Something went wrong with that request. Please try again.