Sha256: e94e4147d0784ca4aa132eb48cccc677c66ddea1251316d886d97929b301fb8d

Contents?: true

Size: 1.72 KB

Versions: 38

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    # Service that encapsulates the vote flow used for elections for registered users.
    class CurrentUserVoteFlow < VoteFlow
      def initialize(election, current_user, &can_vote_block)
        super(election)

        @current_user = current_user
        @can_vote_block = can_vote_block
      end

      def voter_login(params)
        # There is no previous login page for this vote flow
      end

      def has_voter?
        current_user.present?
      end

      delegate :name, to: :current_user, prefix: :voter, allow_nil: true
      delegate :email, to: :current_user, allow_nil: true

      def user
        current_user
      end

      def voter_data
        return nil unless current_user

        {
          id: current_user.id,
          created: current_user.created_at.to_i
        }
      end

      def vote_check(*)
        VoteCheckResult.new(
          allowed: current_user && (received_voter_token || can_vote_block.call),
          error_message: I18n.t("votes.messages.not_allowed", scope: "decidim.elections")
        )
      end

      def login_path(online_vote_path); end

      def questions_for(election)
        election.questions
      end

      def ballot_style_id; end

      private

      attr_accessor :current_user, :can_vote_block

      def valid_token_flow_data?
        return @valid_token_flow_data if defined?(@valid_token_flow_data)

        @valid_token_flow_data = received_voter_token && received_voter_token_user_id && current_user.id == received_voter_token_user_id
      end

      def received_voter_token_user_id
        @received_voter_token_user_id ||= received_voter_token_data.dig(:flow, :id)&.to_i
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
decidim-elections-0.28.4 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.9 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.28.3 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.8 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.28.2 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.7 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.28.1 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.6 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.26.10 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.26.9 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.28.0 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.5 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.28.0.rc5 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.28.0.rc4 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.26.8 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.4 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.3 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.26.7 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.26.5 app/services/decidim/elections/current_user_vote_flow.rb
decidim-elections-0.27.2 app/services/decidim/elections/current_user_vote_flow.rb