Skip to content

Commit

Permalink
Added ability to specify :allow_blank, to allow blank values for an e…
Browse files Browse the repository at this point in the history
…num.
  • Loading branch information
Ryan Brunner committed Mar 25, 2011
1 parent 614fdc5 commit 0614d7a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/classy_enum/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,24 @@ module Attributes
# class Alarm < ActiveRecord::Base
# classy_enum_attr :priority, :alarm_priority
# end
def classy_enum_attr(enum, attribute=nil)

attribute ||= enum

def classy_enum_attr(*options)
enum = options[0]
attribute = enum
allow_blank = false

options[1..-1].each do |o|
attribute = o if o.is_a? Symbol
allow_blank = o[:allow_blank] if o.is_a? Hash
end

klass = enum.to_s.camelize.constantize

self.instance_eval do

# Add ActiveRecord validation to ensure it won't be saved unless it's an option
validates_inclusion_of attribute, :in => klass.all, :message => "must be one of #{klass.valid_options}"
validates_inclusion_of attribute, :in => klass.all, :message => "must be one of #{klass.valid_options}",
:allow_blank => allow_blank


# Define getter method that returns a ClassyEnum instance
define_method attribute do
Expand Down
33 changes: 33 additions & 0 deletions spec/classy_enum_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,39 @@

end

describe "A ClassyEnum that allows blanks" do

context "with valid breed options" do
before { @dog = BreedlessDog.new(:breed => :golden_retriever) }

it "should be valid with a valid option" do
@dog.should be_valid
end
end

it "should be valid with a nil breed" do
BreedlessDog.new(:breed => nil).should be_valid
end

it "should be valid with a blank breed" do
BreedlessDog.new(:breed => "").should be_valid
end

context "with invalid breed options" do
before { @dog = BreedlessDog.new(:breed => :fake_breed) }

it "should not be valid with an invalid option" do
@dog.should_not be_valid
end

it "should have an error message containing the right options" do
@dog.valid?
@dog.errors[:breed].should include("must be one of #{Breed.all.map(&:to_sym).join(', ')}")
end
end

end

describe "A ClassyEnum that has a different field name than the enum" do
before { @dog = OtherDog.new(:other_breed => :snoop) }

Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
t.string :other_breed
end

create_table :breedless_dogs, :force => true do |t|
t.string :breed
end
end

class Breed < ClassyEnum::Base
Expand All @@ -31,6 +34,10 @@ class Dog < ActiveRecord::Base
classy_enum_attr :breed
end

class BreedlessDog < ActiveRecord::Base
classy_enum_attr :breed, :allow_blank => true
end

class OtherDog < ActiveRecord::Base
classy_enum_attr :breed, :other_breed
end
Expand Down

0 comments on commit 0614d7a

Please sign in to comment.