Sha256: 58dfb7bd36b6562c861b2d1c97dcb0f1c4ef43ef92092134379c913a813bb439

Contents?: true

Size: 1.94 KB

Versions: 19

Compression:

Stored size: 1.94 KB

Contents

# TODO: PR to HER
# Fix to_params when embeded_params is nil
# Fix to_params when changes is nil 
# --> allow all params - this is required to be able to make class level 
#     requests like MyModel.post(path,{some: 'data'})
#
module Her
  module Model
    # This module handles resource data parsing at the model level (after the parsing middleware)
    module Parse
      extend ActiveSupport::Concern

      module ClassMethods

        # @private
        def to_params(attributes, changes = nil)
          filtered_attributes = attributes.dup.symbolize_keys
          filtered_attributes.merge!(embeded_params(attributes) || {})
          if her_api && her_api.options[:send_only_modified_attributes] && !changes.nil?
            filtered_attributes = changes.symbolize_keys.keys.inject({}) do |hash, attribute|
              hash[attribute] = filtered_attributes[attribute]
              hash
            end
          end

          if include_root_in_json?
            if json_api_format?
              { included_root_element => [filtered_attributes] }
            else
              { included_root_element => filtered_attributes }
            end
          else
            filtered_attributes
          end
        end

        # @private
        # TODO: Handle has_one
        def embeded_params(attributes)
          associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association|
            params = attributes[association[:data_key]].map(&:to_params)
            # <PATCH> - Return hash
            next hash if params.empty?
            # </PATCH>
            if association[:class_name].constantize.include_root_in_json?
              root = association[:class_name].constantize.root_element
              hash[association[:data_key]] = params.map { |n| n[root] }
            else
              hash[association[:data_key]] = params
            end
            hash
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mno-enterprise-core-3.4.0 lib/her_extension/model/parse.rb
mno-enterprise-core-3.3.3 lib/her_extension/model/parse.rb
mno-enterprise-core-3.3.2 lib/her_extension/model/parse.rb
mno-enterprise-core-3.2.1 lib/her_extension/model/parse.rb
mno-enterprise-core-3.3.1 lib/her_extension/model/parse.rb
mno-enterprise-core-3.3.0 lib/her_extension/model/parse.rb
mno-enterprise-core-3.2.0 lib/her_extension/model/parse.rb
mno-enterprise-core-3.1.4 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.7 lib/her_extension/model/parse.rb
mno-enterprise-core-3.1.3 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.6 lib/her_extension/model/parse.rb
mno-enterprise-core-3.1.2 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.5 lib/her_extension/model/parse.rb
mno-enterprise-core-3.1.1 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.4 lib/her_extension/model/parse.rb
mno-enterprise-core-3.1.0 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.3 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.2 lib/her_extension/model/parse.rb
mno-enterprise-core-3.0.1 lib/her_extension/model/parse.rb