class ActiveForce::ActiveQuery
Attributes
association_mapping[R]
belongs_to_association_mapping[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 33 def initialize(sobject, custom_table_name = nil) @sobject = sobject @association_mapping = {} @belongs_to_association_mapping = {} super custom_table_name || table_name fields sobject.fields end
Public Instance Methods
count()
click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 51 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 86 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 93 def find_by conditions where(conditions).limit 1 end
find_by!(conditions)
click to toggle source
# File lib/active_force/active_query.rb, line 97 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 66 def first super.to_a.first end
includes(*relations)
click to toggle source
# File lib/active_force/active_query.rb, line 104 def includes(*relations) includes_query = Association::EagerLoadBuilderForNestedIncludes.build(relations, sobject) 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 62 def limit limit limit == 1 ? super.to_a.first : super end
loaded?()
click to toggle source
# File lib/active_force/active_query.rb, line 118 def loaded? !@records.nil? end
none()
click to toggle source
# File lib/active_force/active_query.rb, line 111 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 70 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 122 def order *args return self if args.nil? super build_order_by args end
select(*selected_fields)
click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 81 def select *selected_fields selected_fields.map! { |field| mappings[field] } super *selected_fields end
sum(field)
click to toggle source
Calls superclass method
# File lib/active_force/active_query.rb, line 55 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 41 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 76 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 184 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 129 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 140 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 175 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 234 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 213 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 199 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 194 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 247 def order_type(type) type == :desc ? 'DESC' : 'ASC' end
quote_string(s)
click to toggle source
# File lib/active_force/active_query.rb, line 226 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 169 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 203 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 45 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 161 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 150 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 230 def result sfdc_client.query(self.to_s) end