Module: Canned::Context::Matchers::Relation

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

Instance Method Summary collapse

Instance Method Details

#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

#to_which_it_belongs(_options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/canned/context/matchers/relation.rb', line 6

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

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

  resource = @stack.top

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

  if actor.respond_to? :reflect_on_association
    assoc = actor.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
    actor.send(assoc.foreign_key) == resource.id
  else
    Helpers.resolve(resource, :id) == Helpers.resolve(actor, "#{as}_id".to_sym)
  end
end