class MarkMapper::Plugins::Associations::OneProxy

Public Instance Methods

build(attrs={}, &block) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 6
def build(attrs={}, &block)
  instantiate_target(:new, attrs, &block)
end
create(attrs={}, &block) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 10
def create(attrs={}, &block)
  instantiate_target(:create, attrs, &block)
end
create!(attrs={}, &block) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 14
def create!(attrs={}, &block)
  instantiate_target(:create!, attrs, &block)
end
delete() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 49
def delete
  target.delete
  reset
end
destroy() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 44
def destroy
  target.destroy
  reset
end
nullify() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 54
def nullify
  nullify_scope(target)
  target.save
  reset
end
replace(doc) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 18
def replace(doc)
  load_target

  if !target.nil? && target != doc
    if target.persisted?
      case options[:dependent]
        when :delete  then target.delete
        when :destroy then target.destroy
        else
          nullify_scope(target)
          target.save
      end
    end
  end

  unless doc.nil?
    proxy_owner.save unless proxy_owner.persisted?
    doc = klass.new(doc) unless doc.is_a?(klass)
    apply_scope(doc)
    doc.save unless doc.persisted?
  end

  loaded
  @target = doc
end

Protected Instance Methods

apply_scope(doc) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 88
def apply_scope(doc)
  criteria.each { |key, value| doc[key] = value }
  doc
end
criteria() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 79
def criteria
  {self.foreign_key => proxy_owner.id}
end
find_target() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 61
def find_target
  target_class.first(association.query_options.merge(criteria))
end
foreign_key() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 75
def foreign_key
  options[:foreign_key] || proxy_owner.class.name.foreign_key
end
instantiate_target(instantiator, attrs={}, &block) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 65
def instantiate_target(instantiator, attrs={}, &block)
  @target = target_class.send(instantiator, attrs.update(criteria), &block)
  loaded
  @target
end
nullify_scope(doc) click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 83
def nullify_scope(doc)
  criteria.each { |key, value| doc[key] = nil }
  doc
end
target_class() click to toggle source
# File lib/mark_mapper/plugins/associations/one_proxy.rb, line 71
def target_class
  @target_class ||= options[:class] || (options[:class_name] || association.name.to_s.camelize).constantize
end