Skip to content

Instantly share code, notes, and snippets.

@olvap
Last active January 3, 2016 12:39
Show Gist options
  • Select an option

  • Save olvap/8464061 to your computer and use it in GitHub Desktop.

Select an option

Save olvap/8464061 to your computer and use it in GitHub Desktop.
ActiveForce refactor.
class Account < ActiveForce::SObject
...
has_many :medications,
where: "(Date_Discontinued__c > #{ Date.today.strftime("%Y-%m-%d") } or Date_Discontinued__c = NULL)"
...
end
class PortalUser < ActiveRecord::Base
...
def medications
@medications ||= account.medications
end
...
end
class Medication < ActiveForce::SObject
...
field :account_id, from: 'Account'
table_name = 'Patient_Medication__c'
...
end
class PortalUser < ActiveRecord::Base
def medications
@medications ||= find_medications.map do |sobject|
Medication.build sobject
end if account_id?
end
def find_medications
Client.query(<<-SOQL.strip_heredoc).first.Patient_Medications__r.to_a
SELECT Id,
(SELECT #{Medication.mappings.values.join(', ')} FROM Patient_Medications__r
WHERE Date_Discontinued__c > #{ Date.today.strftime("%Y-%m-%d") } or Date_Discontinued__c = NULL)
FROM Account
WHERE Id = '#{account_id}'
SOQL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment