module Fixably::Encoding

Public Instance Methods

encode(_options = nil, attrs: nil) click to toggle source

Since our monkey patch converts the keys to underscore, it is necessary to convert them back to camelcase when performing a create or update

# File lib/fixably/encoding.rb, line 7
def encode(_options = nil, attrs: nil)
  attrs ||= attributes
  remove_has_many_associations(attrs)
  remove_unallowed_parameters(attrs)
  nest_for_association(attrs).
    deep_transform_keys { _1.camelize(:lower) }.
    public_send("to_#{self.class.format.extension}")
end

Protected Instance Methods

nest_for_association(attrs) click to toggle source
# File lib/fixably/encoding.rb, line 32
def nest_for_association(attrs)
  return attrs unless parent_association

  { parent_association.to_s => [attrs] }
end
remove_has_many_associations(attrs) click to toggle source
# File lib/fixably/encoding.rb, line 22
def remove_has_many_associations(attrs)
  reflections.select { _2.macro.equal?(:has_many) }.keys.each do
    attrs.delete(_1)
  end
end
remove_on_encode(= %w[created_at href id]) click to toggle source
# File lib/fixably/encoding.rb, line 18
  def remove_on_encode = %w[created_at href id]

  private

  def remove_has_many_associations(attrs)
    reflections.select { _2.macro.equal?(:has_many) }.keys.each do
      attrs.delete(_1)
    end
  end

  def remove_unallowed_parameters(attrs)
    remove_on_encode.each { attrs.delete(_1) }
  end

  def nest_for_association(attrs)
    return attrs unless parent_association

    { parent_association.to_s => [attrs] }
  end
end
remove_unallowed_parameters(attrs) click to toggle source
# File lib/fixably/encoding.rb, line 28
def remove_unallowed_parameters(attrs)
  remove_on_encode.each { attrs.delete(_1) }
end