Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/forms/contribution_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/models/contribution.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion app/models/fund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion app/views/funds/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
</div>
<div class="friend-information">
<div class="contribute-button">
<%= 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 %>
</div>
</div>
<div class="friend-information">
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20150317023139_add_approved_to_contribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddApprovedToContribution < ActiveRecord::Migration
def change
add_column :contributions, :approved, :boolean, default: false
end
end
7 changes: 4 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down