Last active
August 29, 2015 14:06
-
-
Save davidrichards/b6ab5f91b5965ec80005 to your computer and use it in GitHub Desktop.
Not tested...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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