Skip to content

Commit

Permalink
Added support for Mixed Case terms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tammer Saleh committed Feb 19, 2009
1 parent 499dad2 commit efb5eea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/enum_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def enum_field(field, possible_values, options={})
const_set field.to_s.pluralize.upcase, possible_values unless const_defined?(field.to_s.pluralize.upcase)

possible_values.each do |current_value|
method_name = current_value.gsub(/[-\s]/, '_')
method_name = current_value.downcase.gsub(/[-\s]/, '_')
define_method("#{method_name}?") do
self.send(field) == current_value
end
Expand Down
22 changes: 19 additions & 3 deletions test/enum_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EnumFieldTest < Test::Unit::TestCase
end

should "extend active record base with method" do
assert ActiveRecord::Base.respond_to?(:enum_field)
assert_respond_to ActiveRecord::Base, :enum_field
end
end

Expand All @@ -57,11 +57,27 @@ class EnumFieldTest < Test::Unit::TestCase
end

should "define an underscored query method for the multiple word choice" do
assert @model.respond_to?('choice_one?')
assert_respond_to @model, :choice_one?
end

should "define an underscored query method for the dasherized choice" do
assert @model.respond_to?('choice_two?')
assert_respond_to @model, :choice_two?
end
end

context "With an enum containing mixed case choices" do
setup do
MockedModel.stubs(:validates_inclusion_of)
MockedModel.send :enum_field, :field, ['Choice One', 'ChoiceTwo', 'Other']
@model = MockedModel.new
end

should "define a lowercase, underscored query method for the multiple word choice" do
assert_respond_to @model, :choice_one?
end

should "define a lowercase query method for the camelcase choice" do
assert_respond_to @model, :choicetwo?
end
end
end
Expand Down

0 comments on commit efb5eea

Please sign in to comment.