Skip to content

Instantly share code, notes, and snippets.

@robcole
Last active August 29, 2015 14:20
Show Gist options
  • Save robcole/c69550a5c5faed3a2c1c to your computer and use it in GitHub Desktop.
Save robcole/c69550a5c5faed3a2c1c to your computer and use it in GitHub Desktop.

Revisions

  1. robcole revised this gist Apr 30, 2015. 2 changed files with 7 additions and 6 deletions.
    10 changes: 5 additions & 5 deletions donation.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    def apply_grant
    current_grant = project.grants.current
    def apply_current_grant!
    current_grant = project.current_grant
    current_grant.apply_to_donation(self)
    end

    @@ -9,18 +9,18 @@ def grant_can_be_applied?

    def apply_multiple_grants
    if grants.current.remaining_amount >= total_amount
    apply_grant
    apply_current_grant!
    else
    while grant_can_be_applied?
    apply_grant
    apply_current_grant!
    end
    end
    end

    def apply_grant!
    grant_count = project.grants.active.count
    if grant_count == 1
    apply_grant
    apply_current_grant!
    elsif grant_count > 1
    apply_multiple_grants
    else
    3 changes: 2 additions & 1 deletion grant.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ def apply_to_donation(donation)
    if active?
    amount_to_apply = donation.project_amount
    donation.grant_amount ||= 0
    if amount_remaining >= amount_to_apply
    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
    @@ -11,6 +11,7 @@ def apply_to_donation(donation)
    donation.grant_amount += amount_remaining
    self.remaining_amount -= amount_remaining
    end
    donation.grants.push(self)
    donation.save!
    self.save!
    end
  2. robcole revised this gist Apr 30, 2015. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions grant.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    def apply_to_donation(donation)
    if active?
    amount_to_apply = donation.project_amount
    donation.grant_amount ||= 0
    if amount_remaining >= 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.save!
    self.save!
    end
    end
  3. robcole renamed this gist Apr 30, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. robcole created this gist Apr 30, 2015.
    28 changes: 28 additions & 0 deletions grants.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    def apply_grant
    current_grant = project.grants.current
    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_grant
    else
    while grant_can_be_applied?
    apply_grant
    end
    end
    end

    def apply_grant!
    grant_count = project.grants.active.count
    if grant_count == 1
    apply_grant
    elsif grant_count > 1
    apply_multiple_grants
    else
    end
    end