Sha256: 55d18d863c2b88299817230128ddeae45690195a40b4bf0f43083803e456e56f
Contents?: true
Size: 1.82 KB
Versions: 45
Compression:
Stored size: 1.82 KB
Contents
# frozen_string_literal: true require "digest/sha1" module Decidim # Initiatives can be voted by users and supported by organizations. class InitiativesVote < ApplicationRecord include Decidim::TranslatableAttributes belongs_to :author, foreign_key: "decidim_author_id", class_name: "Decidim::User" belongs_to :initiative, foreign_key: "decidim_initiative_id", class_name: "Decidim::Initiative", inverse_of: :votes belongs_to :scope, foreign_key: "decidim_scope_id", class_name: "Decidim::Scope", optional: true validates :initiative, uniqueness: { scope: [:author, :scope] } validates :initiative, uniqueness: { scope: [:hash_id, :scope] } after_commit :update_counter_cache, on: [:create, :destroy] scope :for_scope, ->(scope) { where(scope: scope) } # Public: Generates a hashed representation of the initiative support. # # Used when exporting the votes as CSV. def sha1 title = translated_attribute(initiative.title) description = translated_attribute(initiative.description) Digest::SHA1.hexdigest "#{authorization_unique_id}#{title}#{description}" end def decrypted_metadata @decrypted_metadata ||= encrypted_metadata ? encryptor.decrypt(encrypted_metadata) : {} end private def encryptor @encryptor ||= Decidim::Initiatives::DataEncryptor.new(secret: "personal user metadata") end def authorization_unique_id first_authorization = Decidim::Initiatives::UserAuthorizations .for(author) .first first_authorization&.unique_id || author.email end def update_counter_cache initiative.update_online_votes_counters end end end
Version data entries
45 entries across 45 versions & 1 rubygems