module Crm::Helpers::Finders

Public Class Methods

included(base) click to toggle source
# File lib/crm/helpers/finders.rb, line 6
def self.included(base)
  base.extend self
end

Public Instance Methods

find(*args) click to toggle source
# File lib/crm/helpers/finders.rb, line 10
def find(*args)
  wants_array = args.first.is_a?(Array)
  ids = args.flatten

  case ids.size
  when 0 then raise ArgumentError, 'Requires one or more IDs as argument.'
  when 1
    crm_object = find_one(ids.first)
    wants_array ? [crm_object].compact : crm_object
  else find_many(ids)
  end
end
find_by_query(query, options = {}) click to toggle source
# File lib/crm/helpers/finders.rb, line 23
def find_by_query(query, options = {})
  limit = options[:limit] || 50
  sort_order = options[:sort_order] || 'desc'

  result = crm_class.query(query).limit(limit).sort_order(sort_order).to_a
  result.map do |crm_object|
    new(crm_object.attributes)
  end
end

Protected Instance Methods

find_many(ids) click to toggle source
# File lib/crm/helpers/finders.rb, line 42
def find_many(ids)
  crm_objects = Crm.find(ids).select do |crm_object|
    crm_object.is_a?(crm_class)
  end
  unknown_ids = ids - crm_objects.map(&:id)
  if unknown_ids.present?
    raise Errors::ResourceNotFound.new(
      'Items could not be found.', unknown_ids
    )
  end

  crm_objects.map { |crm_object| new(crm_object.attributes) }
end
find_one(id) click to toggle source
# File lib/crm/helpers/finders.rb, line 35
def find_one(id)
  crm_object = crm_class.find(id)
  return nil if crm_object.blank?

  new(crm_object.attributes)
end