class ActiveForce::ActiveQuery

Attributes

association_mapping[R]
belongs_to_association_mapping[R]
nested_query_fields[R]
sobject[R]

Public Class Methods

new(sobject, custom_table_name = nil) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 34
def initialize(sobject, custom_table_name = nil)
  @sobject = sobject
  @association_mapping = {}
  @belongs_to_association_mapping = {}
  super custom_table_name || table_name
  fields sobject.fields
  @nested_query_fields = []
end

Public Instance Methods

all()
Alias for: to_a
count() click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 53
def count
  sfdc_client.query(super.to_s).first.expr0
end
find!(id) click to toggle source
# File lib/active_force/active_query.rb, line 103
def find!(id)
  result = find(id)
  raise RecordNotFound.new("Couldn't find #{table_name} with id #{id}", table_name, id: id) if result.nil?

  result
end
find_by(conditions) click to toggle source
# File lib/active_force/active_query.rb, line 110
def find_by conditions
  where(conditions).limit 1
end
find_by!(conditions) click to toggle source
# File lib/active_force/active_query.rb, line 114
def find_by!(conditions)
  result = find_by(conditions)
  raise RecordNotFound.new("Couldn't find #{table_name} with #{conditions}", table_name, conditions) if result.nil?

  result
end
first() click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 68
def first
  super.to_a.first
end
ids() click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 99
def ids
  super.pluck(:id)
end
includes(*relations) click to toggle source
# File lib/active_force/active_query.rb, line 121
def includes(*relations)
  includes_query = Association::EagerLoadBuilderForNestedIncludes.build(relations, sobject, nil, nested_query_fields) 
  fields includes_query[:fields]
  association_mapping.merge!(includes_query[:association_mapping])
  self
end
limit(limit) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 64
def limit limit
  limit == 1 ? super.to_a.first : super
end
loaded?() click to toggle source
# File lib/active_force/active_query.rb, line 135
def loaded?
  !@records.nil?
end
none() click to toggle source
# File lib/active_force/active_query.rb, line 128
def none
  clone_and_set_instance_variables(
    records: [],
    conditions: [build_condition(id: '1' * 18), build_condition(id: '0' * 18)]
  )
end
not(args=nil, *rest) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 72
def not args=nil, *rest
  return self if args.nil?

  super build_condition args, rest
end
order(*args) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 139
def order *args
  return self if args.nil?
  super build_order_by args
end
select(*selected_fields, &block) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 83
def select *selected_fields, &block
  if block
    result = []
    self.each do |record|
      result << record if block.call(record)
    end
    result
  else
    fields_collection = ActiveForce::SelectBuilder.new(selected_fields, self).parse
    nested_query_fields.concat(fields_collection[:nested_query_fields]) if fields_collection[:nested_query_fields]
    return self if fields_collection[:non_nested_query_fields].blank?
  
    super *fields_collection[:non_nested_query_fields]
  end
end
sum(field) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 57
def sum(field)
  raise ArgumentError, 'field is required' if field.blank?
  raise UnknownFieldError.new(sobject, field) unless mappings.key?(field.to_sym)

  sfdc_client.query(super(mappings.fetch(field.to_sym)).to_s).first.expr0
end
to_a() click to toggle source
# File lib/active_force/active_query.rb, line 43
def to_a
  @decorated_records ||= sobject.try(:decorate, records) || records
end
Also aliased as: all
where(args=nil, *rest) click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 78
def where args=nil, *rest
  return self if args.nil?
  super build_condition args, rest
end

Private Instance Methods

applicable_predicates(attribute, value) click to toggle source
# File lib/active_force/active_query.rb, line 201
def applicable_predicates(attribute, value)
  if value.is_a?(Array)
    [in_predicate(attribute, value)]
  elsif value.is_a?(Range)
    range_predicates(attribute, value)
  else
    [eq_predicate(attribute, value)]
  end
end
build_condition(args, other=[]) click to toggle source
# File lib/active_force/active_query.rb, line 146
def build_condition(args, other=[])
  case args
  when String, Array
    build_condition_from_array other.empty? ? args : ([args] + other)
  when Hash
    build_conditions_from_hash args
  else
    args
  end
end
build_condition_from_array(ary) click to toggle source
# File lib/active_force/active_query.rb, line 157
def build_condition_from_array(ary)
  statement, *bind_parameters = ary
  return statement if bind_parameters.empty?
  if bind_parameters.first.is_a? Hash
    replace_named_bind_parameters statement, bind_parameters.first
  else
    replace_bind_parameters statement, bind_parameters
  end
end
build_conditions_from_hash(hash) click to toggle source
# File lib/active_force/active_query.rb, line 192
def build_conditions_from_hash(hash)
  hash.flat_map do |key, value|
    field = mappings[key]
    raise UnknownFieldError.new(sobject, key) if field.blank?

    applicable_predicates(field, value)
  end
end
build_order_by(args) click to toggle source
# File lib/active_force/active_query.rb, line 251
def build_order_by(args)
  args.map do |arg|
    case arg
    when Symbol
      mappings[arg].to_s
    when Hash
      arg.map { |key, value| "#{mappings[key]} #{order_type(value)}" }
    else
      arg
    end
  end.join(', ')
end
enclose_value(value) click to toggle source
# File lib/active_force/active_query.rb, line 230
def enclose_value value
  case value
  when String
    quote_string(value)
  when NilClass
    'NULL'
  when Time
    value.iso8601
  else
    value.to_s
  end
end
eq_predicate(attribute, value) click to toggle source
# File lib/active_force/active_query.rb, line 216
def eq_predicate(attribute, value)
  "#{attribute} = #{enclose_value value}"
end
in_predicate(attribute, values) click to toggle source
# File lib/active_force/active_query.rb, line 211
def in_predicate(attribute, values)
  escaped_values = values.map &method(:enclose_value)
  "#{attribute} IN (#{escaped_values.join(',')})"
end
order_type(type) click to toggle source
# File lib/active_force/active_query.rb, line 264
def order_type(type)
  type == :desc ? 'DESC' : 'ASC'
end
quote_string(s) click to toggle source
# File lib/active_force/active_query.rb, line 243
def quote_string(s)
  "'#{s.gsub(/(['\\])/, '\\\\\\1')}'"
end
raise_if_bind_arity_mismatch(expected_var_count, actual_var_count) click to toggle source
# File lib/active_force/active_query.rb, line 186
def raise_if_bind_arity_mismatch(expected_var_count, actual_var_count)
  if expected_var_count != actual_var_count
    raise PreparedStatementInvalid, "wrong number of bind variables (#{actual_var_count} for #{expected_var_count})"
  end
end
range_predicates(attribute, range) click to toggle source
# File lib/active_force/active_query.rb, line 220
def range_predicates(attribute, range)
  conditions = []
  conditions << "#{attribute} >= #{enclose_value(range.begin)}" unless range.begin.nil?
  unless range.end.nil?
    operator = range.exclude_end? ? '<' : '<='
    conditions << "#{attribute} #{operator} #{enclose_value(range.end)}"
  end
  conditions
end
records() click to toggle source
# File lib/active_force/active_query.rb, line 47
        def records
  @records ||= result.to_a.map { |mash| build mash, association_mapping }
end
replace_bind_parameters(statement, values) click to toggle source
# File lib/active_force/active_query.rb, line 178
def replace_bind_parameters(statement, values)
  raise_if_bind_arity_mismatch statement.count('?'), values.size
  bound = values.dup
  statement.gsub('?') do
    enclose_value bound.shift
  end
end
replace_named_bind_parameters(statement, bind_parameters) click to toggle source
# File lib/active_force/active_query.rb, line 167
def replace_named_bind_parameters(statement, bind_parameters)
  statement.gsub(/(:?):([a-zA-Z]\w*)/) do
    key = $2.to_sym
    if bind_parameters.has_key? key
      enclose_value bind_parameters[key]
    else
      raise PreparedStatementInvalid, "missing value for :#{key} in #{statement}"
    end
  end
end
result() click to toggle source
# File lib/active_force/active_query.rb, line 247
def result
  sfdc_client.query(self.to_s)
end