app/models/decidim/initiatives_vote.rb in decidim-initiatives-0.22.0 vs app/models/decidim/initiatives_vote.rb in decidim-initiatives-0.23.0

- old
+ new

@@ -9,59 +9,55 @@ belongs_to :author, foreign_key: "decidim_author_id", class_name: "Decidim::User" - belongs_to :user_group, - foreign_key: "decidim_user_group_id", - class_name: "Decidim::UserGroup", - optional: true - belongs_to :initiative, foreign_key: "decidim_initiative_id", class_name: "Decidim::Initiative", inverse_of: :votes - validates :initiative, uniqueness: { scope: [:author, :user_group] } + 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 :supports, -> { where.not(decidim_user_group_id: nil) } - scope :votes, -> { where(decidim_user_group_id: nil) } + scope :for_scope, ->(scope) { where(scope: scope) } - # PUBLIC + # Public: Generates a hashed representation of the initiative support. # - # Generates a hashed representation of the initiative support. + # Used when exporting the votes as CSV. def sha1 - return unless decidim_user_group_id.nil? - 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.initiative_votes_count = Decidim::InitiativesVote - .votes - .where(decidim_initiative_id: initiative.id) - .count - - initiative.initiative_supports_count = Decidim::InitiativesVote - .supports - .where(decidim_initiative_id: initiative.id) - .count - - initiative.save + initiative.update_online_votes_counters end end end