Sha256: 0b79beaaf139e9a916cf6b7ee101e87e48be6e2ac51ceab4170112047f819146

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module JsonApiClient
  module Helpers
    module Requestable
      extend ActiveSupport::Concern

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

      module ClassMethods
        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

        self.errors = last_result_set.errors
        if last_result_set.has_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

2 entries across 2 versions & 1 rubygems

Version Path
json_api_client-1.0.0.beta5 lib/json_api_client/helpers/requestable.rb
json_api_client-1.0.0.beta4 lib/json_api_client/helpers/requestable.rb