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 @@
- <%= link_to "#{@fund.friend.name} 에게 선물하기", new_fund_contribution_path(@fund), class: 'btn btn-lg btn-success btn-block pull-right'%> + <% if @fund.complete? %> + <%= link_to "모금 완료!", new_fund_contribution_path(@fund), class: 'btn btn-lg btn-success btn-block pull-right', disabled: true %> + <% else %> + <%= link_to "#{@fund.friend.name} 에게 선물하기", new_fund_contribution_path(@fund), class: 'btn btn-lg btn-success btn-block pull-right' %> + <% end %>
diff --git a/db/migrate/20150317023139_add_approved_to_contribution.rb b/db/migrate/20150317023139_add_approved_to_contribution.rb new file mode 100644 index 0000000..a63b1c6 --- /dev/null +++ b/db/migrate/20150317023139_add_approved_to_contribution.rb @@ -0,0 +1,5 @@ +class AddApprovedToContribution < ActiveRecord::Migration + def change + add_column :contributions, :approved, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index efce4c8..cedecb1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,16 +11,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150314144532) do +ActiveRecord::Schema.define(version: 20150317023139) do create_table "contributions", force: :cascade do |t| t.integer "user_id" t.integer "fund_id" t.integer "amount" t.text "message" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "payment" + t.boolean "approved", default: false end create_table "friends", force: :cascade do |t|