class DraftApprove::Serialization::Json::Serializer::JsonSerializer

Private Inner Class to contain the JSON Serialization logic

Public Class Methods

new(model) click to toggle source
# File lib/draft_approve/serialization/json/serializer.rb, line 45
def initialize(model)
  @model = model
end

Public Instance Methods

changes_for_model() click to toggle source
# File lib/draft_approve/serialization/json/serializer.rb, line 49
def changes_for_model
  changes = {}
  @model.class.reflect_on_all_associations(:belongs_to).each do |belongs_to_assoc|
    changes.merge!(association_change(belongs_to_assoc))
  end
  return changes.merge!(non_association_changes)
end

Private Instance Methods

association_change(association) click to toggle source
# File lib/draft_approve/serialization/json/serializer.rb, line 59
def association_change(association)
  old_value = association_old_value(association)
  new_value = association_new_value(association)

  if old_value == new_value
    return {}
  else
    return { association.name.to_s => [old_value, new_value] }
  end
end
association_new_value(association) click to toggle source

The new value of an association may be nil, or point to a persisted model, or point to a non-persisted model with a persisted draft.

Note that if the associated object is not persisted, and has no persisted draft, then this is an error scenario.

# File lib/draft_approve/serialization/json/serializer.rb, line 104
def association_new_value(association)
  associated_obj = @model.public_send(association.name)

  if associated_obj.blank?
    return nil
  elsif associated_obj.persisted?
    if association.polymorphic?
      return {
        Constants::TYPE => @model.public_send(association.foreign_type),
        Constants::ID => @model.public_send(association.foreign_key)
      }
    else
      return {
        Constants::TYPE => association.class_name,
        Constants::ID => @model.public_send(association.foreign_key)
      }
    end
  else  # associated_obj not persisted - so we need a persisted draft
    draft = associated_obj.draft_pending_approval

    if draft.blank? || draft.new_record?
      raise(DraftApprove::Errors::AssociationUnsavedError, "#{association.name} points to an unsaved object")
    end

    return { Constants::TYPE => draft.class.name, Constants::ID => draft.id }
  end
end
association_old_value(association) click to toggle source

The old value of an association must be nil or point to a persisted non-draft object.

# File lib/draft_approve/serialization/json/serializer.rb, line 86
def association_old_value(association)
  if association.polymorphic?
    old_type = @model.public_send("#{association.foreign_type}_was")
    old_id = @model.public_send("#{association.foreign_key}_was")
  else
    old_type = association.class_name
    old_id = @model.public_send("#{association.foreign_key}_was")
  end

  return nil if old_id.blank? || old_type.blank?
  return { Constants::TYPE => old_type, Constants::ID => old_id }
end
non_association_changes() click to toggle source
# File lib/draft_approve/serialization/json/serializer.rb, line 70
def non_association_changes
  association_attribute_names = @model.class.reflect_on_all_associations(:belongs_to).map do |ref|
    [ref.foreign_type, ref.foreign_key, ref.association_foreign_key]
  end.flatten.uniq.compact

  non_association_attribute_names = @model.attribute_names - association_attribute_names

  return non_association_attribute_names.each_with_object({}) do |attribute_name, result_hash|
    if @model.public_send("#{attribute_name}_changed?")
      result_hash[attribute_name] = @model.public_send("#{attribute_name}_change")
    end
  end
end