Skip to content

Commit

Permalink
Fix enum attribute name verification
Browse files Browse the repository at this point in the history
Fix `define_enum_for` so that it actually tests that the attribute is
present in the list of defined enums, as you could fool it by merely
defining a class method that was the pluralized version of the attribute
name. In the same vein, passing a pluralized version of the attribute
name to `define_enum_for` would erroneously pass, and now it fails.
  • Loading branch information
guilhermeFranco authored and mcmire committed Feb 13, 2015
1 parent 445a3ea commit 714129b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
attributes is a UUID column that ends in an "f", the matcher is able to
generate a proper "next" value without erroring.

* Fix `define_enum_for` so that it actually tests that the attribute is present
in the list of defined enums, as you could fool it by merely defining a class
method that was the pluralized version of the attribute name. In the same
vein, passing a pluralized version of the attribute name to `define_enum_for`
would erroneously pass, and now it fails. ([#641])

[#641]: https://github.com/thoughtbot/shoulda-matchers/pull/641

# 2.8.0

### Deprecations
Expand Down
8 changes: 2 additions & 6 deletions lib/shoulda/matchers/active_record/define_enum_for_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,12 @@ def expected_enum_values
hashify(options[:expected_enum_values]).with_indifferent_access
end

def enum_method
attribute_name.to_s.pluralize
end

def actual_enum_values
model.class.send(enum_method)
model.class.send(attribute_name.to_s.pluralize)
end

def enum_defined?
model.class.respond_to?(enum_method)
model.defined_enums.include?(attribute_name.to_s)
end

def enum_values_match?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do
if active_record_supports_enum?
context 'if the attribute is given in plural form accidentally' do
it 'rejects' do
record = record_with_array_values
plural_enum_attribute = enum_attribute.to_s.pluralize
message = "Expected #{record.class} to define :#{plural_enum_attribute} as an enum"
assertion = -> {
expect(record).to define_enum_for(plural_enum_attribute)
}
expect(&assertion).to fail_with_message(message)
end
end

context 'if a method to hold enum values exists on the model but was not created via the enum macro' do
it 'rejects' do
model = define_model :example do
def self.statuses; end
end
message = "Expected #{model} to define :statuses as an enum"
assertion = -> {
expect(model.new).to define_enum_for(:statuses)
}
expect(&assertion).to fail_with_message(message)
end
end

describe "with only the attribute name specified" do
it "accepts a record where the attribute is defined as an enum" do
expect(record_with_array_values).to define_enum_for(enum_attribute)
Expand Down

0 comments on commit 714129b

Please sign in to comment.