Files
nairobi/app/controllers/concerns/dashboard/group_supports.rb
Javi Martín d0d681a44b Add and apply EmptyLineAfterGuardClause rule
We were inconsistent on this one. I consider it particularly useful when
a method starts with a `return` statement.

In other cases, we probably shouldn't have a guard rule in the middle of
a method in any case, but that's a different refactoring.
2019-10-24 17:56:03 +02:00

81 lines
2.0 KiB
Ruby

module Dashboard::GroupSupports
extend ActiveSupport::Concern
included do
def grouped_supports(attribute)
supports.group_by { |v| grouping_key_for(v[attribute].in_time_zone.to_date) }
end
def grouping_key_for(date)
return calculate_week(date) if params[:group_by] == "week"
return "#{date.year}-#{date.month}" if params[:group_by] == "month"
date
end
def accumulate_supports(grouped_votes)
grouped_votes.each do |group, votes|
grouped_votes[group] = votes.inject(0) { |sum, vote| sum + vote.vote_weight }
end
accumulated = 0
grouped_votes.each do |k, v|
accumulated += v
grouped_votes[k] = accumulated
end
end
def fill_holes(grouped_votes)
complete_grouped_votes = {}
(start_date(proposal.published_at.to_date)..end_date).each do |date|
key = grouping_key_for(date)
complete_grouped_votes[key] = if grouped_votes.key? key
grouped_votes[key]
else
[]
end
end
complete_grouped_votes
end
def previous_key_for(date)
grouping_key_for(date - interval)
end
def interval
return 1.week if params[:group_by] == "week"
return 1.month if params[:group_by] == "month"
1.day
end
end
private
def calculate_week(date)
week = date.cweek
year = calculate_year_of_week(date)
"#{week}/#{year}"
end
def calculate_year_of_week(date)
year = date.year
if first_week_of_year?(date) && date.end_of_week.year != date.year
year = year + 1
elsif last_week_of_year?(date) && date.beginning_of_week.year != date.year
year = year - 1
end
year
end
def first_week_of_year?(date)
date.cweek == 1
end
def last_week_of_year?(date)
date.cweek == 52 || date.cweek == 53
end
end