Sha256: 35e1236d2e5bca0598c6fac5d7fb0b4ec1c7367f534580a12ed37b090739ef14

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

module JsonApiClient
  module Helpers
    module Attributable
      extend ActiveSupport::Concern

      included do
        include DynamicAttributes
        attr_accessor :errors
        initializer do |obj, params|
          obj.attributes = params.merge(obj.class.default_attributes)
        end
      end

      module ClassMethods
        def load(params)
          new(params).tap do |resource|
            resource.mark_as_persisted!
          end
        end

        def default_attributes
          {type: table_name}
        end
      end

      def update_attributes(attrs = {})
        self.attributes = attrs
        save
      end

      def mark_as_persisted!
        @persisted = true
      end

      def persisted?
        !!@persisted && has_attribute?(primary_key)
      end

      def query_params
        attributes.except(primary_key)
      end

      def to_param
        attributes.fetch(primary_key, "").to_s
      end

      protected

      def ==(other)
        self.class == other.class && attributes == other.attributes
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_api_client-1.0.0.beta5 lib/json_api_client/helpers/attributable.rb
json_api_client-1.0.0.beta4 lib/json_api_client/helpers/attributable.rb
json_api_client-1.0.0.beta3 lib/json_api_client/helpers/attributable.rb
json_api_client-1.0.0.beta2 lib/json_api_client/helpers/attributable.rb
json_api_client-1.0.0.beta lib/json_api_client/helpers/attributable.rb