class ActiveShepherd::Methods::QueryChanges

Public Instance Methods

handle_attribute(attribute_name, before, after) click to toggle source
# File lib/active_shepherd/methods/query_changes.rb, line 8
def handle_attribute(attribute_name, before, after)
  query[attribute_name] = [before, after]
end
handle_has_many_association(reflection) click to toggle source
# File lib/active_shepherd/methods/query_changes.rb, line 12
def handle_has_many_association(reflection)
  association = aggregate.model.send reflection.name

  collection_changes = association.each.with_object({}).with_index do |(associated_model, h), index|
    changes = self.class.query_changes recurse(associated_model, reflection.foreign_key)
    h[index] = changes unless changes.blank?
  end

  unless collection_changes.blank?
    query[reflection.name] = collection_changes
  end
end
handle_has_one_association(reflection) click to toggle source
# File lib/active_shepherd/methods/query_changes.rb, line 25
def handle_has_one_association(reflection)
  associated_model = aggregate.model.send reflection.name
  return unless associated_model.present?

  changes = self.class.query_changes recurse(associated_model, reflection.foreign_key)
  query[reflection.name] = changes unless changes.blank?
end
query_changes() click to toggle source
# File lib/active_shepherd/methods/query_changes.rb, line 2
def query_changes
  traverse!
  set_meta_action
  query
end
setup() click to toggle source
Calls superclass method ActiveShepherd::QueryMethod#setup
# File lib/active_shepherd/methods/query_changes.rb, line 33
def setup
  super
  @attributes = aggregate.model.changes.each_with_object({}) do |(name,changes),h|
    next if aggregate.excluded_attributes.include? name.to_s
    h[name.to_sym] = changes.map do |raw_value|
      aggregate.serialize_value name, raw_value
    end
  end
end

Private Instance Methods

set_meta_action() click to toggle source
# File lib/active_shepherd/methods/query_changes.rb, line 45
def set_meta_action
  if not aggregate.model.persisted?
    query[:_create] = '1'
  elsif aggregate.model.marked_for_destruction?
    query[:_destroy] = '1'
  end
end