Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing includes to properly build and set the association. #12

Merged
merged 7 commits into from
Mar 14, 2023
6 changes: 4 additions & 2 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class RecordNotFound < StandardError; end
class ActiveQuery < Query
extend Forwardable

attr_reader :sobject
attr_reader :sobject, :association_mapping

def_delegators :sobject, :sfdc_client, :build, :table_name, :mappings
def_delegators :to_a, :each, :map, :inspect, :pluck, :each_with_object

def initialize sobject
@sobject = sobject
@association_mapping = {}
super table_name
fields sobject.fields
end
Expand All @@ -24,7 +25,7 @@ def to_a
end

private def records
@records ||= result.to_a.map { |mash| build mash }
@records ||= result.to_a.map { |mash| build mash, association_mapping }
end

alias_method :all, :to_a
Expand Down Expand Up @@ -65,6 +66,7 @@ def includes(*relations)
relations.each do |relation|
association = sobject.associations[relation]
fields Association::EagerLoadProjectionBuilder.build association
association_mapping[association.sfdc_association_field] = association.relation_name
end
self
end
Expand Down
5 changes: 1 addition & 4 deletions lib/active_force/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ def associations
@associations ||= {}
end

# i.e name = 'Quota__r'
def find_association name
associations.values.detect do |association|
association.represents_sfdc_table? name
end
associations[name.to_sym]
end

def has_many relation_name, options = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class HasManyAssociationProjectionBuilder < AbstractProjectionBuilder
# relationship name. Per SFDC convention, the name needs
# to be pluralized
def projections
match = association.sfdc_association_field.match /__r\z/
# pluralize the table name, and append '__r' if it was there to begin with
relationship_name = association.sfdc_association_field.sub(match.to_s, '').pluralize + match.to_s
relationship_name = association.sfdc_association_field
query = Query.new relationship_name
query.fields association.relation_model.fields
["(#{query.to_s})"]
Expand Down
7 changes: 7 additions & 0 deletions lib/active_force/association/has_many_association.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module ActiveForce
module Association
class HasManyAssociation < Association
def sfdc_association_field
name = relationship_name.gsub /__c\z/, '__r'
match = name.match /__r\z/
# pluralize the table name, and append '__r' if it was there to begin with
name.sub(match.to_s, '').pluralize + match.to_s
end

private

def default_foreign_key
Expand Down
7 changes: 5 additions & 2 deletions lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ def self.query
end

attr_accessor :build_attributes
def self.build mash
def self.build mash, association_mapping={}
return unless mash
sobject = new
sobject.build_attributes = mash[:build_attributes] || mash
sobject.run_callbacks(:build) do
mash.each do |column, value|
if association_mapping.has_key?(column)
column = association_mapping[column]
end
sobject.write_value column, value
end
end
Expand Down Expand Up @@ -164,7 +167,7 @@ def reload
end

def write_value key, value
if association = self.class.find_association(key.to_s)
if association = self.class.find_association(key.to_sym)
field = association.relation_name
value = Association::RelationModelBuilder.build(association, value)
elsif key.to_sym.in?(mappings.keys)
Expand Down
2 changes: 1 addition & 1 deletion spec/active_force/sobject/includes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ module ActiveForce
}
})]
allow(client).to receive(:query).once.and_return response
account = Account.includes(:opportunities).find '123'
account = Account.includes(:opportunities, :owner).find '123'
expect(account.opportunities).to be_an Array
expect(account.opportunities.all? { |o| o.is_a? Opportunity }).to eq true
expect(account.owner).to be_a Owner
Expand Down