app/models/decidim/elections/answer.rb in decidim-elections-0.24.3 vs app/models/decidim/elections/answer.rb in decidim-elections-0.25.0.rc1
- old
+ new
@@ -14,17 +14,38 @@
delegate :organization, :participatory_space, to: :component
belongs_to :question, foreign_key: "decidim_elections_question_id", class_name: "Decidim::Elections::Question", inverse_of: :answers
has_one :election, through: :question, foreign_key: "decidim_elections_election_id", class_name: "Decidim::Elections::Election"
has_one :component, through: :election, foreign_key: "decidim_component_id", class_name: "Decidim::Component"
+ has_many :results, foreign_key: "decidim_elections_answer_id", class_name: "Decidim::Elections::Result", dependent: :destroy
default_scope { order(weight: :asc, id: :asc) }
# Public: Get all the proposals related to the answer
#
# Returns an ActiveRecord::Relation.
def proposals
linked_resources(:proposals, "related_proposals")
+ end
+
+ def slug
+ "answer-#{id}"
+ end
+
+ # Sum all valid results from different origins (PollingStations or BulletinBoard)
+ def results_total
+ @results_total ||= results.valid_answers.sum(:value)
+ end
+
+ # A result percentage relative to the question
+ # Returns a Float.
+ def results_percentage
+ @results_percentage ||= begin
+ return 0 unless results_total.positive?
+
+ result = results_total.to_f / question.results_total * 100.0
+ result.round
+ end
end
end
end
end