class Exact::Base

Attributes

client[RW]

Public Class Methods

all(client:) click to toggle source
# File lib/exact/models/base.rb, line 50
def self.all(client:)
  client.send(to_s.demodulize.pluralize)
  result = client.execute
  result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
end
create(attributes: {}, client:) click to toggle source
# File lib/exact/models/base.rb, line 56
def self.create(attributes: {}, client:)
  exact_obj = Exactonline.const_get(exact_endpoint.singularize).new(attributes)
  client.send("AddTo#{exact_endpoint}", exact_obj)
  result = client.save_changes
  result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
  result.first
end
create_client(access_token:, division:) click to toggle source
# File lib/exact/models/base.rb, line 40
def self.create_client(access_token:, division:)
  Exact::Client.new(access_token: access_token, division: division, service: exact_service, endpoint: exact_endpoint)
end
destroy(id: nil, client:) click to toggle source
# File lib/exact/models/base.rb, line 117
def self.destroy(id: nil, client:)
  client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
  result = client.execute
  return false unless result.any?
  client.delete_object(result.first)
  client.save_changes
  true
rescue OData::ServiceError => e
  errors.add(:base, e.message)
  false
end
exact_endpoint() click to toggle source
# File lib/exact/models/base.rb, line 30
def self.exact_endpoint
  name = Object.const_get("#{self}::EXACT_ENDPOINT") rescue nil
  name = to_s.demodulize.pluralize.camelize unless name
  name
end
exact_guid_attribute() click to toggle source
# File lib/exact/models/base.rb, line 14
def self.exact_guid_attribute
  Object.const_get("#{name}::EXACT_GUID")
end
exact_service() click to toggle source
# File lib/exact/models/base.rb, line 22
def self.exact_service
  Object.const_get("#{self}::EXACT_SERVICE")
end
find(id:, client:) click to toggle source
# File lib/exact/models/base.rb, line 64
def self.find(id:, client:)
  client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
  result = client.execute
  result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
  result.first
end
find_by(field:, value:, guid: false, client:) click to toggle source
# File lib/exact/models/base.rb, line 71
def self.find_by(field:, value:, guid: false, client:)
  query = "#{field.capitalize} eq "
  query << 'guid' if guid
  query << "'#{value}'"
  client.send(exact_endpoint).filter(query)
  result = client.execute
  result.map! { |obj| Exact.const_get("#{to_s.demodulize}Mapping").convert obj }
  result.first
end
update(id:, attributes: {}, client:) click to toggle source
# File lib/exact/models/base.rb, line 81
def self.update(id:, attributes: {}, client:)
  client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{id}'")
  result = client.execute
  return false unless result.any?
  exact_obj = result.first
  attributes.each do |attribute, value|
    exact_obj.send("#{attribute}=", value) if exact_obj.respond_to? attribute
  end
  client.update_object(exact_obj)
  client.save_changes
  true
end

Public Instance Methods

destroy(client:) click to toggle source
# File lib/exact/models/base.rb, line 129
def destroy(client:)
  client.send(self.class.exact_endpoint).filter("#{self.class.exact_guid_attribute} eq guid'#{guid}'")
  result = client.execute
  return false unless result.any?
  client.delete_object(result.first)
  client.save_changes
  true
rescue OData::ServiceError => e
  errors.add(:base, e.message)
  false
end
exact_endpoint() click to toggle source
# File lib/exact/models/base.rb, line 36
def exact_endpoint
  self.class.exact_endpoint
end
exact_guid_attribute() click to toggle source
# File lib/exact/models/base.rb, line 18
def exact_guid_attribute
  self.class.exact_guid_attribute
end
exact_service() click to toggle source
# File lib/exact/models/base.rb, line 26
def exact_service
  self.class.exact_service
end
guid() click to toggle source
# File lib/exact/models/base.rb, line 10
def guid
  send(self.class.exact_guid_attribute)
end
save(client:) click to toggle source
# File lib/exact/models/base.rb, line 108
def save(client:)
  if !guid.blank?
    update(client: client)
  else
    self.attributes = self.class.create(attributes: attributes, client: client).attributes
    self
  end
end
setup_client(access_token: nil, division: nil, reload: false) click to toggle source
# File lib/exact/models/base.rb, line 44
def setup_client(access_token: nil, division: nil, reload: false)
  return @client if @client.present? && !reload
  return @client = self.class.create_client(access_token: access_token, division: division) if @client.nil? || reload
  self
end
update(client:) click to toggle source
# File lib/exact/models/base.rb, line 94
def update(client:)
  return false if guid.blank?
  client.send(exact_endpoint).filter("#{exact_guid_attribute} eq guid'#{guid}'")
  result = client.execute
  return false unless result.any?
  exact_obj = result.first
  attributes.each do |attribute, value|
    exact_obj.send("#{attribute}=", value) if exact_obj.respond_to? attribute
  end
  client.update_object(exact_obj)
  client.save_changes
  true
end