diff --git a/app/forms/contribution_form.rb b/app/forms/contribution_form.rb index c3365a4..cf00120 100644 --- a/app/forms/contribution_form.rb +++ b/app/forms/contribution_form.rb @@ -17,6 +17,7 @@ class ContributionForm greater_than_or_equal_to: 0 } validate :not_over_remain_amount + validate :no_less_than_equal_zero_amount def fund @fund = Fund.find(fund_id) @@ -55,4 +56,10 @@ def not_over_remain_amount errors.add(:amount, "최대 참여 가능 금액을 초과하였습니다.") end end + + def no_less_than_equal_zero_amount + if amount.present? and amount <= 0 + errors.add(:amount, "0원 이하로 낼 수 없습니다.") + end + end end \ No newline at end of file diff --git a/app/models/contribution.rb b/app/models/contribution.rb index 7a96796..5798594 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -1,4 +1,10 @@ class Contribution < ActiveRecord::Base belongs_to :user belongs_to :fund + + scope :approved, -> { where(approved: true) } + + def approve! + update_attributes!(approved: true) + end end diff --git a/app/models/fund.rb b/app/models/fund.rb index 02b0032..ff35587 100644 --- a/app/models/fund.rb +++ b/app/models/fund.rb @@ -4,8 +4,12 @@ class Fund < ActiveRecord::Base belongs_to :friend has_many :contributions + def complete? + remain_amount <= 0 + end + def total_amount - @total_amount ||= self.contributions.pluck(:amount).reduce(0, :+) + @total_amount ||= self.contributions.approved.pluck(:amount).reduce(0, :+) end def remain_amount diff --git a/app/views/funds/show.html.erb b/app/views/funds/show.html.erb index 11c8a11..f5cf59b 100644 --- a/app/views/funds/show.html.erb +++ b/app/views/funds/show.html.erb @@ -44,7 +44,11 @@