class Hobo::Model::FindFor::Finder

Public Instance Methods

[](anchor_or_id) click to toggle source
# File lib/hobo/model/find_for.rb, line 67
def [](anchor_or_id)
  anchor = if anchor_or_id.is_a?(String)
             id, klass = anchor_or_id.split(':')
             (klass.constantize || @anchor_reflection.klass).find(id)
           else
             anchor_or_id
           end
  result = if @association.loaded?
             @association.detect { |x| x.send("#{@anchor_reflection.name}_is?", anchor) }
          else
             @association.send("#{@anchor_reflection.name}_is", anchor).first
          end
  result ||= @association.new(@anchor_reflection.name => anchor)


  result.origin = self
  result.origin_attribute = if @anchor_reflection.options[:polymorphic]
                              "#{anchor.id}:#{anchor.class.name}"
                            else
                              "#{anchor.id}"
                            end
  result
end
origin() click to toggle source
# File lib/hobo/model/find_for.rb, line 59
def origin
  @association.proxy_owner
end
origin_attribute() click to toggle source
# File lib/hobo/model/find_for.rb, line 63
def origin_attribute
  @name
end

Public Class Methods

new(owner, name, collection, anchor_association) click to toggle source
# File lib/hobo/model/find_for.rb, line 53
def initialize(owner, name, collection, anchor_association)
  @association = owner.send(collection)
  @anchor_reflection = @association.member_class.reflections[anchor_association.to_s]
  @name = name
end