Sha256: eb798843cad6a94bfe37573718eb95b0f28d0e44ab402d13432cfb6f2a4f9efd

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module JsonApiClient
  module Helpers
    module Requestable
      extend ActiveSupport::Concern

      included do
        attr_accessor :last_result_set, :errors
        class_attribute :requestor_class
        self.requestor_class = Query::Requestor
      end

      module ClassMethods
        def find(conditions)
          requestor.find(conditions)
        end

        def create(conditions = {})
          new(conditions).tap do |resource|
            resource.save
          end
        end

        def requestor
          @requestor ||= requestor_class.new(self)
        end
      end

      def save
        self.last_result_set = if persisted?
          self.class.requestor.update(self)
        else
          self.class.requestor.create(self)
        end

        if last_result_set.has_errors?
          self.errors = last_result_set.errors
          false
        else
          self.errors.clear if self.errors
          mark_as_persisted!
          if updated = last_result_set.first
            self.attributes = updated.attributes
          end
          true
        end
      end

      def destroy
        self.last_result_set = self.class.requestor.destroy(self)
        if !last_result_set.has_errors?
          self.attributes.clear
          true
        else
          false
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_api_client-1.0.0.beta lib/json_api_client/helpers/requestable.rb