Remove duplication to calculate percentage

This commit is contained in:
Javi Martín
2018-12-10 16:33:04 +01:00
parent 188278296c
commit ccaa2e1a77
4 changed files with 7 additions and 11 deletions

View File

@@ -30,9 +30,4 @@ module StatsHelper
opt[:data][:graph] = admin_api_stats_path(budget_investments: true)
content_tag :div, "", opt
end
def calculate_percentage(fraction, total)
percent = fraction / total.to_f
percent.nan? ? 0.0 : (percent * 100).round(3)
end
end

View File

@@ -188,11 +188,6 @@ class Budget::Stats
calculate_percentage(participants, population)
end
def calculate_percentage(fraction, total)
percent = fraction / total.to_f
percent.nan? ? 0.0 : (percent * 100).round(3)
end
def supports(supportable)
ActsAsVotable::Vote.where(votable_type: "Budget::Investment", votable_id: supportable.investments.pluck(:id))
end

View File

@@ -11,6 +11,13 @@ module Statisticable
def generate
self.class.stats_methods.map { |stat_name| [stat_name, send(stat_name)] }.to_h
end
private
def calculate_percentage(fraction, total)
percent = fraction / total.to_f
percent.nan? ? 0.0 : (percent * 100).round(3)
end
end
class_methods do

View File

@@ -1,6 +1,5 @@
class Poll::Stats
include Statisticable
include StatsHelper
alias_method :poll, :resource
def self.stats_methods