Class: RiotRails::ActiveRecord::HasManyMacro

Inherits:
AssertionMacro
  • Object
show all
Defined in:
lib/riot/active_record/assertion_macros.rb

Overview

An ActiveRecord assertion macro that expects to pass when a given attribute is defined as has_many association. Will fail if an association is not defined for the attribute and if the association is not +has_many.jekyll

context "a Room" do
  setup { Room.new }

  topic.has_many(:doors)
  topic.has_many(:floors) # should probably fail given our current universe :)
end

Instance Method Summary collapse

Methods inherited from AssertionMacro

#error_from_writing_value

Instance Method Details

#evaluate(actual, attribute) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/riot/active_record/assertion_macros.rb', line 144

def evaluate(actual, attribute)
  reflection = actual.class.reflect_on_association(attribute)
  static_msg = "expected #{attribute.inspect} to be a has_many association, but was "
  if reflection.nil?
    fail(static_msg + "not")
  elsif "has_many" != reflection.macro.to_s
    fail(static_msg + "a #{reflection.macro} instead")
  else
    pass("has many #{attribute.inspect}")
  end
end