Sha256: 8425abd5d7e6d71728f928fb3782d7b2bf7146beb454f027aae0531d3ae66c6b

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

module Restly::Base::Instance::WriteCallbacks
  extend ActiveSupport::Concern

  included do
    before_save :format_request
  end

  private

  def format_request
    @request_body = case format.to_sym
                      when :json
                        savable_resource.to_json
                      when :xml
                        savable_resource.to_xml
                      else
                        savable_resource.to_param
                    end
  end

  def savable_resource
    {resource_name => attributes_with_present_values(writeable_attributes)}
  end

  def attributes_with_present_values(attributes=self.attributes)
    attributes.as_json.reduce({}) do |hash, (key, val)|
      if val.is_a?(Hash)
        hash[key] = attributes_with_present_values(val)
      elsif val.present? && key.to_sym != :id
        hash[key] = val
      end
      hash
    end
  end

  def writeable_attributes(attributes=self.attributes)
    if (maa = mass_assignment_authorizer :default).is_a? ActiveModel::MassAssignmentSecurity::BlackList
      attributes.reject{ |key, val| maa.map(&:to_sym).include?(key.to_sym) }
    elsif mass_assignment_authorizer.is_a? ActiveModel::MassAssignmentSecurity::BlackList
      attributes.select{ |key, val| maa.map(&:to_sym).include?(key.to_sym) }
    end
  end


end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
restly-0.0.1.alpha.11 lib/restly/base/instance/write_callbacks.rb
restly-0.0.1.alpha.10 lib/restly/base/instance/write_callbacks.rb
restly-0.0.1.alpha.9 lib/restly/base/instance/write_callbacks.rb
restly-0.0.1.alpha.8 lib/restly/base/instance/write_callbacks.rb
restly-0.0.1.alpha.7 lib/restly/base/instance/write_callbacks.rb
restly-0.0.1.alpha.6 lib/restly/base/instance/write_callbacks.rb