Class: ActiveForce::ActiveQuery

Inherits:
Query
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/active_force/active_query.rb

Instance Attribute Summary collapse

Attributes inherited from Query

#query_fields, #table

Instance Method Summary collapse

Methods inherited from Query

#fields, #find, #join, #last, #limit_value, #offset, #offset_value, #or, #to_s

Constructor Details

#initialize(sobject, custom_table_name = nil) ⇒ ActiveQuery

Returns a new instance of ActiveQuery.



34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#association_mappingObject (readonly)

Returns the value of attribute association_mapping.



29
30
31
# File 'lib/active_force/active_query.rb', line 29

def association_mapping
  @association_mapping
end

#belongs_to_association_mappingObject (readonly)

Returns the value of attribute belongs_to_association_mapping.



29
30
31
# File 'lib/active_force/active_query.rb', line 29

def belongs_to_association_mapping
  @belongs_to_association_mapping
end

#nested_query_fieldsObject (readonly)

Returns the value of attribute nested_query_fields.



29
30
31
# File 'lib/active_force/active_query.rb', line 29

def nested_query_fields
  @nested_query_fields
end

#sobjectObject (readonly)

Returns the value of attribute sobject.



29
30
31
# File 'lib/active_force/active_query.rb', line 29

def sobject
  @sobject
end

Instance Method Details

#countObject



53
54
55
# File 'lib/active_force/active_query.rb', line 53

def count
  sfdc_client.query(super.to_s).first.expr0
end

#find!(id) ⇒ Object

Raises:



103
104
105
106
107
108
# 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) ⇒ Object



110
111
112
# File 'lib/active_force/active_query.rb', line 110

def find_by conditions
  where(conditions).limit 1
end

#find_by!(conditions) ⇒ Object

Raises:



114
115
116
117
118
119
# 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

#firstObject



68
69
70
# File 'lib/active_force/active_query.rb', line 68

def first
  super.to_a.first
end

#idsObject



99
100
101
# File 'lib/active_force/active_query.rb', line 99

def ids
  super.pluck(:id)
end

#includes(*relations) ⇒ Object



121
122
123
124
125
126
# 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) ⇒ Object



64
65
66
# File 'lib/active_force/active_query.rb', line 64

def limit limit
  limit == 1 ? super.to_a.first : super
end

#loaded?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/active_force/active_query.rb', line 135

def loaded?
  !@records.nil?
end

#noneObject



128
129
130
131
132
133
# 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) ⇒ Object



72
73
74
75
76
# 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) ⇒ Object



139
140
141
142
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
# 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_aObject Also known as: all



43
44
45
# File 'lib/active_force/active_query.rb', line 43

def to_a
  @decorated_records ||= sobject.try(:decorate, records) || records
end

#where(args = nil, *rest) ⇒ Object



78
79
80
81
# 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