Sha256: 4ec5f085907ef1e3dbc57932846f8655ac5ea177be936df7cb9df9bb6ddef5c9

Contents?: true

Size: 1.41 KB

Versions: 47

Compression:

Stored size: 1.41 KB

Contents

module Her
  module JsonApi
    module Model
      
      def self.included(klass)
        klass.class_eval do
          include Her::Model

          [:parse_root_in_json, :include_root_in_json, :root_element, :primary_key].each do |method|
            define_method method do |*args|
              raise NoMethodError, "Her::JsonApi::Model does not support the #{method} configuration option"
            end
          end

          method_for :update, :patch

          @type = name.demodulize.tableize
          
          def self.parse(data)
            data.fetch(:attributes).merge(data.slice(:id))
          end

          def self.to_params(attributes, changes={})
            request_data = { type: @type }.tap { |request_body| 
              attrs = attributes.dup.symbolize_keys.tap { |filtered_attributes|
                if her_api.options[:send_only_modified_attributes]
                  filtered_attributes = changes.symbolize_keys.keys.inject({}) do |hash, attribute|
                    hash[attribute] = filtered_attributes[attribute]
                    hash
                  end
                end
              }
              request_body[:id] = attrs.delete(:id) if attrs[:id]
              request_body[:attributes] = attrs
            }
            { data: request_data }
          end

          def self.type(type_name)
            @type = type_name.to_s
          end
        end
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 3 rubygems

Version Path
test_track_rails_client-4.0.0.alpha12 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha11 vendor/gems/her/lib/her/json_api/model.rb
her-0.10.4 lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha10 vendor/gems/her/lib/her/json_api/model.rb
her-0.10.3 lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha9 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha8 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha7 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha6 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha5 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha4 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha3 vendor/gems/her/lib/her/json_api/model.rb
her-0.10.2 lib/her/json_api/model.rb
her-1.0.1 lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha2 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-4.0.0.alpha1 vendor/gems/her/lib/her/json_api/model.rb
her-1.0.0 lib/her/json_api/model.rb
her-0.10.1 lib/her/json_api/model.rb
test_track_rails_client-3.0.1 vendor/gems/her/lib/her/json_api/model.rb
test_track_rails_client-3.0.0 vendor/gems/her/lib/her/json_api/model.rb