Method: Canned::Context::Matchers::Relation#that_belongs_to_it

Defined in:
lib/canned/context/matchers/relation.rb

#that_belongs_to_it(_options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/canned/context/matchers/relation.rb', line 28

def that_belongs_to_it(_options={})
  return false unless indeed?

  actor = @stack.top(:actor)
  raise Canned::SetupError.new '"that_belongs_to_it" require an enclosing actor context' if actor.nil?
  resource = @stack.top

  as = _options[:as]
  as = resource.class.name.parameterize if as.nil?

  if resource.respond_to? :reflect_on_association
    assoc = resource.reflect_on_association(as)
    raise Canned::SetupError.new 'Invalid association name' if assoc.nil?
    raise Canned::SetupError.new 'Thorugh assoc is not supported' if assoc.options.has_key? :through # TODO: support through!
    raise Canned::SetupError.new 'Invalid association type' if assoc.macro != :belongs_to
    resource.send(assoc.foreign_key) == actor.id
  else
    Helpers.resolve(actor, :id) == Helpers.resolve(resource, "#{as}_id".to_sym)
  end
end