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.



35
36
37
38
39
40
41
42
# File 'lib/active_force/active_query.rb', line 35

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.



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

def association_mapping
  @association_mapping
end

#belongs_to_association_mappingObject (readonly)

Returns the value of attribute belongs_to_association_mapping.



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

def belongs_to_association_mapping
  @belongs_to_association_mapping
end

#nested_query_fieldsObject (readonly)

Returns the value of attribute nested_query_fields.



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

def nested_query_fields
  @nested_query_fields
end

#sobjectObject (readonly)

Returns the value of attribute sobject.



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

def sobject
  @sobject
end

Instance Method Details

#countObject



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

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

#find!(id) ⇒ Object

Raises:



104
105
106
107
108
109
# File 'lib/active_force/active_query.rb', line 104

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



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

def find_by conditions
  where(conditions).limit 1
end

#find_by!(conditions) ⇒ Object

Raises:



115
116
117
118
119
120
# File 'lib/active_force/active_query.rb', line 115

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



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

def first
  super.to_a.first
end

#idsObject



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

def ids
  super.pluck(:id)
end

#includes(*relations) ⇒ Object



122
123
124
125
126
127
# File 'lib/active_force/active_query.rb', line 122

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



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

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

#loaded?Boolean

Returns:

  • (Boolean)


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

def loaded?
  !@records.nil?
end

#noneObject



129
130
131
132
133
134
# File 'lib/active_force/active_query.rb', line 129

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



73
74
75
76
77
# File 'lib/active_force/active_query.rb', line 73

def not args=nil, *rest
  return self if args.nil?

  super build_condition args, rest
end

#order(*args) ⇒ Object



140
141
142
143
# File 'lib/active_force/active_query.rb', line 140

def order *args
  return self if args.nil?
  super build_order_by args
end

#select(*selected_fields, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_force/active_query.rb', line 84

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)


58
59
60
61
62
63
# File 'lib/active_force/active_query.rb', line 58

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



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

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

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



79
80
81
82
# File 'lib/active_force/active_query.rb', line 79

def where args=nil, *rest
  return self if args.nil?
  super build_condition args, rest
end