Last active
August 29, 2015 14:20
-
-
Save robcole/c69550a5c5faed3a2c1c to your computer and use it in GitHub Desktop.
This file contains 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
def apply_current_grant! | |
current_grant = project.current_grant | |
current_grant.apply_to_donation(self) | |
end | |
def grant_can_be_applied? | |
grants.current.present? && (total_amount <= grant_amount) | |
end | |
def apply_multiple_grants | |
if grants.current.remaining_amount >= total_amount | |
apply_current_grant! | |
else | |
while grant_can_be_applied? | |
apply_current_grant! | |
end | |
end | |
end | |
def apply_grant! | |
grant_count = project.grants.active.count | |
if grant_count == 1 | |
apply_current_grant! | |
elsif grant_count > 1 | |
apply_multiple_grants | |
else | |
end | |
end |
This file contains 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
def apply_to_donation(donation) | |
if active? | |
amount_to_apply = donation.project_amount | |
donation.grant_amount ||= 0 | |
if remaining_amount >= amount_to_apply | |
# Case: There is enough remaining to match the gift | |
donation.grant_amount += amount_to_apply | |
self.remaining_amount -= amount_to_apply | |
else | |
# Case: There is NOT enough remaining to match the gift | |
donation.grant_amount += amount_remaining | |
self.remaining_amount -= amount_remaining | |
end | |
donation.grants.push(self) | |
donation.save! | |
self.save! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment