Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Last active August 29, 2015 14:06
Show Gist options
  • Save davidrichards/b6ab5f91b5965ec80005 to your computer and use it in GitHub Desktop.
Save davidrichards/b6ab5f91b5965ec80005 to your computer and use it in GitHub Desktop.
Not tested...
class CleanSubContractorContracts
def records
@records ||= SubContractorContract.includes(:contact).where("users.id IS NULL")
end
def no_user
@no_user ||= User.find_by_email('[email protected]')
end
def sub_contractor(contract)
contract.sub_contractor
end
def first_authorized_signer(sub_contractor)
return nil unless sub_contractor
sub_contractor.authorized_signers.first
end
def best_user(contract)
first_authorized_signer(sub_contractor(contract)) || no_user
end
def get_contacts
records.each do |contract|
contract.contact = best_user(contact)
end
end
def save_records!
get_contacts
records.each do |record|
record.save!
end
end
def execute!
SubContractorContracts.transaction do
save_records!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment