class DraftApprove::Serialization::Json::Serializer::JsonDeserializer

Private Inner Class to contain the JSON Serialization logic

Public Class Methods

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

Public Instance Methods

new_values_for_draft() click to toggle source
# File lib/draft_approve/serialization/json/serializer.rb, line 139
def new_values_for_draft
  draftable_class = Object.const_get(@draft.draftable_type)
  association_attribute_names = draftable_class.reflect_on_all_associations(:belongs_to).map(&:name).map(&:to_s)

  return @draft.draft_changes.each_with_object({}) do |(attribute_name, change), result_hash|
    new_value = change[1]
    if association_attribute_names.include?(attribute_name)
      result_hash[attribute_name] = associated_model_for_new_value(new_value)
    else
      result_hash[attribute_name] = new_value
    end
  end
end

Private Instance Methods

associated_model_for_new_value(new_value) click to toggle source
# File lib/draft_approve/serialization/json/serializer.rb, line 155
def associated_model_for_new_value(new_value)
  return nil if new_value.nil?

  associated_model_type = new_value[Constants::TYPE]
  associated_model_id = new_value[Constants::ID]

  associated_class = Object.const_get(associated_model_type)

  if associated_class.ancestors.include? Draft
    # The associated class is a draft (or subclass).
    # It must be in the same draft transaction as the draft we're getting values for.
    associated_draft = @draft.draft_transaction.drafts.find(associated_model_id)

    raise(DraftApprove::Errors::PriorDraftNotAppliedError) if associated_draft.draftable.nil?

    return associated_draft.draftable
  else
    return associated_class.find(associated_model_id)
  end
end