Sha256: beca0d89634ceece44cfc22fc7db57dcfe3163aebb2b81417e0ecde8415047b4

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    # The data store for a Question in the Decidim::Elections component. It stores a
    # title, description and a maximum number of selection that voters can choose.
    class Question < ApplicationRecord
      include Decidim::Resourceable
      include Traceable
      include Loggable

      belongs_to :election, foreign_key: "decidim_elections_election_id", class_name: "Decidim::Elections::Election", inverse_of: :questions
      has_many :answers, foreign_key: "decidim_elections_question_id", class_name: "Decidim::Elections::Answer", inverse_of: :question, dependent: :destroy
      has_many :results, foreign_key: "decidim_elections_question_id", class_name: "Decidim::Elections::Result", dependent: :destroy
      has_one :component, through: :election, foreign_key: "decidim_component_id", class_name: "Decidim::Component"

      default_scope { order(weight: :asc, id: :asc) }

      # Public: Checks if enough answers are given for max_selections attribute
      #
      # Returns a boolean.
      def valid_max_selection?
        max_selections <= answers.count
      end

      # Public: Checks if the question accepts a blank/NOTA as an answer
      #
      # Returns a boolean.
      def nota_option?
        @nota_option ||= min_selections.zero?
      end

      def blank_votes
        @blank_votes ||= results.blank_answers.sum(:value)
      end

      def results_total
        @results_total ||= answers.sum(&:results_total) + blank_votes
      end

      # A result percentage relative to the question
      # Returns a Float.
      def blank_votes_percentage
        @blank_votes_percentage ||= begin
          return 0 unless results_total.positive?

          result = blank_votes.to_f / results_total * 100.0
          result.round
        end
      end

      def slug
        "question-#{id}"
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-elections-0.26.2 app/models/decidim/elections/question.rb
decidim-elections-0.26.1 app/models/decidim/elections/question.rb
decidim-elections-0.26.0 app/models/decidim/elections/question.rb
decidim-elections-0.26.0.rc2 app/models/decidim/elections/question.rb
decidim-elections-0.26.0.rc1 app/models/decidim/elections/question.rb
decidim-elections-0.25.2 app/models/decidim/elections/question.rb
decidim-elections-0.25.1 app/models/decidim/elections/question.rb
decidim-elections-0.25.0 app/models/decidim/elections/question.rb
decidim-elections-0.25.0.rc4 app/models/decidim/elections/question.rb
decidim-elections-0.25.0.rc3 app/models/decidim/elections/question.rb
decidim-elections-0.25.0.rc2 app/models/decidim/elections/question.rb
decidim-elections-0.25.0.rc1 app/models/decidim/elections/question.rb