Sha256: ca5926350c97c672a724e6e8b5f24dacbe4fe85c5edb26d04daf28b348f36ebe

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Signaling::Base::Persistence
  extend ActiveSupport::Concern

  module ClassMethods
    def create(params)
      from_response(request(:create, scope_params(params)))
    rescue Signaling::Error::UnprocessableEntity => e
      from_response(e.response[:body])
    end

    def destroy(id)
      from_response(request(:destroy, id: id))
    end

    def scope_params(params)
      key = params.is_a?(Array) ? param_key.pluralize : param_key

      { key => to_params(params, true) }
    end

    def param_key
      ActiveModel::Naming.param_key(self)
    end
  end

  def new?
    self.id.blank?
  end

  def persisted?
    !new?
  end

  def save
    new? ? create : update
    true
  rescue Signaling::Error::UnprocessableEntity => e
    self.errors = e.response[:body][:errors]
    false
  end

  def save!
    save || raise(ResourceInvalid.new(self))
  end

  def update_attributes(params)
    update(params)
  rescue Signaling::Error::UnprocessableEntity => e
    self.attributes = e.response[:body]
    false
  end


  private

  def update(params = attributes)
    params = self.class.scope_params(params).merge(id: self.id)
    self.class.request(:update, params) do |response|
      self.attributes = response
    end
  end

  def create
    params = self.class.scope_params(attributes.except(:id))
    self.class.request(:create, params) do |response|
      self.attributes = response
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signaling-1.1.2 lib/signaling/base/persistence.rb