Skip to content

Commit

Permalink
better handling of booleans for prefix/suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Baker committed Mar 22, 2011
1 parent 17099dd commit aeb9822
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/attr_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def attr_enumerator(field, enumerators, opts={})
generate_scopes = options.delete(:generate_scopes)

prefix = options.delete(:prefix)
prefix = { true => field.to_s + '_', false => '' }[prefix] || prefix.to_s
suffix = options.delete(:suffix).to_s
prefix = {true => field.to_s + '_', false => ''}[prefix] || prefix.to_s

suffix = options.delete(:suffix)
suffix = {true => '', false => ''}[suffix] || suffix.to_s

if generate_constant
const_name = generate_constant == true ? field.to_s.pluralize.upcase : generate_constant.to_s
Expand Down
20 changes: 20 additions & 0 deletions spec/attr_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ class TestModel
instance.should_not be_colored_blue
end

it "should allow for explicitly using the default prefix" do
subject.attr_enumerator :choice, ['red', 'blue'], :prefix => true
instance.should respond_to :choice_red?
end

it "should allow for no prefix" do
subject.attr_enumerator :choice, ['red', 'blue'], :prefix => false
instance.should respond_to :red?
end

it "should allow for a custom suffix" do
subject.attr_enumerator :choice, ['first', 'second'], :prefix => '', :suffix => '_choice'
instance.choice = 'first'
Expand All @@ -94,6 +104,16 @@ class TestModel
instance.should be_first_choice
end

it "should allow for explicitly using the default suffix" do
subject.attr_enumerator :choice, ['red', 'blue'], :suffix => true
instance.should respond_to :choice_red?
end

it "should allow for no suffix" do
subject.attr_enumerator :choice, ['red', 'blue'], :suffix => false
instance.should respond_to :choice_red?
end

it "should generate methods with friendly names" do
enumerations = {
:has_space? => 'has space',
Expand Down

0 comments on commit aeb9822

Please sign in to comment.