Skip to content

Commit

Permalink
validates percentage
Browse files Browse the repository at this point in the history
removed check_tips_to_pay_against_avaiable_amount callback
refactored scopes
  • Loading branch information
sashazykov committed Apr 6, 2014
1 parent 7704d95 commit 5a12d6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions app/models/project.rb
Expand Up @@ -10,7 +10,7 @@ class Project < ActiveRecord::Base
validates :full_name, :github_id, uniqueness: true, presence: true
validates :host, inclusion: [ "github", "bitbucket" ], presence: true

before_save :check_tips_to_pay_against_avaiable_amount
# before_save :check_tips_to_pay_against_avaiable_amount

def update_repository_info repo
self.github_id = repo.id
Expand Down Expand Up @@ -125,20 +125,24 @@ def tip_for commit
end
end

def donated
self.deposits.where("confirmations > 0").map(&:available_amount).sum
end

def available_amount
self.deposits.where("confirmations > 0").map(&:available_amount).sum - tips_paid_amount
donated - tips_paid_amount
end

def unconfirmed_amount
self.deposits.where(:confirmations => 0).where('created_at > ?', 7.days.ago).map(&:available_amount).sum
end

def tips_paid_amount
self.tips.select(&:decided?).reject(&:refunded?).sum(&:amount)
self.tips.decided.non_refunded.sum(:amount)
end

def tips_paid_unclaimed_amount
self.tips.non_refunded.unclaimed.sum(:amount)
self.tips.decided.non_refunded.unclaimed.sum(:amount)
end

def next_tip_amount
Expand Down
6 changes: 3 additions & 3 deletions app/models/tip.rb
Expand Up @@ -26,7 +26,7 @@ def free?
amount == 0
end

scope :paid, -> { where('sendmany_id is not ?', nil) }
scope :paid, -> { where.not(sendmany_id: nil) }
def paid?
!!sendmany_id
end
Expand All @@ -45,7 +45,7 @@ def non_refunded?
unpaid.
where('users.bitcoin_address' => ['', nil]) }

scope :with_address, -> { joins(:user).where('users.bitcoin_address IS NOT NULL AND users.bitcoin_address != ?', "") }
scope :with_address, -> { joins(:user).where.not('users.bitcoin_address' => ['', nil]) }
def with_address?
user.bitcoin_address.present?
end
Expand Down Expand Up @@ -79,7 +79,7 @@ def amount_percentage
end

def amount_percentage=(percentage)
if undecided? and percentage.present?
if undecided? and percentage.present? and %w(0 0.1 0.5 1 2 5).include?(percentage)
self.amount = project.available_amount * (percentage.to_f / 100)
end
end
Expand Down

0 comments on commit 5a12d6b

Please sign in to comment.