Sha256: a7a0299406a8355a638f06bf06f59c20ac79e9d005b31668d5db46b91a8ede3f

Contents?: true

Size: 1.69 KB

Versions: 24

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Decidim
  # Defines a relation between a user and a participatory process, and what
  # kind of relation does the user has.
  class ParticipatoryProcessUserRole < ApplicationRecord
    include Traceable
    include Loggable

    belongs_to :user, foreign_key: "decidim_user_id", class_name: "Decidim::User", optional: true
    belongs_to :participatory_process, foreign_key: "decidim_participatory_process_id", class_name: "Decidim::ParticipatoryProcess", optional: true

    alias participatory_space participatory_process

    ROLES = %w(admin collaborator moderator valuator).freeze
    validates :role, inclusion: { in: ROLES }, uniqueness: { scope: [:user, :participatory_process] }
    validate :user_and_participatory_process_same_organization

    def self.log_presenter_class_for(_log)
      Decidim::ParticipatoryProcesses::AdminLog::ParticipatoryProcessUserRolePresenter
    end

    ransacker :name do
      Arel.sql(%{("decidim_users"."name")::text})
    end

    ransacker :nickname do
      Arel.sql(%{("decidim_users"."nickname")::text})
    end

    ransacker :email do
      Arel.sql(%{("decidim_users"."email")::text})
    end

    ransacker :invitation_accepted_at do
      Arel.sql(%{("decidim_users"."invitation_accepted_at")::text})
    end

    ransacker :last_sign_in_at do
      Arel.sql(%{("decidim_users"."last_sign_in_at")::text})
    end

    private

    # Private: check if the process and the user have the same organization
    def user_and_participatory_process_same_organization
      return if !participatory_process || !user

      errors.add(:participatory_process, :invalid) unless user.organization == participatory_process.organization
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
decidim-participatory_processes-0.27.9 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.8 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.7 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.6 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.10 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.9 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.5 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.8 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.4 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.3 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.7 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.5 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.2 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.1 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.4 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.0 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.3 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.0.rc2 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.27.0.rc1 app/models/decidim/participatory_process_user_role.rb
decidim-participatory_processes-0.26.2 app/models/decidim/participatory_process_user_role.rb