Files
nairobi/app/models/budget/result.rb
Javi Martín db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00

52 lines
978 B
Ruby

class Budget
class Result
attr_accessor :budget, :heading, :current_investment
def initialize(budget, heading)
@budget = budget
@heading = heading
end
def calculate_winners
reset_winners
investments.compatible.each do |investment|
@current_investment = investment
set_winner if inside_budget?
end
end
def investments
heading.investments.selected.sort_by_ballots
end
def inside_budget?
available_budget >= @current_investment.price
end
def available_budget
total_budget - money_spent
end
def total_budget
heading.price
end
def money_spent
@money_spent ||= 0
end
def reset_winners
investments.update_all(winner: false)
end
def set_winner
@money_spent += @current_investment.price
@current_investment.update!(winner: true)
end
def winners
investments.where(winner: true)
end
end
end