Sha256: 59bf4859b07dfd7a7d5d22107c6b31454519a84b423b16514bb0b8ea0d65290b

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Decidim
  # This concern contains the logic related to authorship
  module Authorable
    extend ActiveSupport::Concern

    included do
      belongs_to :author, foreign_key: "decidim_author_id", class_name: "Decidim::User", optional: true
      belongs_to :user_group, foreign_key: "decidim_user_group_id", class_name: "Decidim::UserGroup", optional: true

      validate :verified_user_group, :user_group_membership
      validate :author_belongs_to_organization

      # Checks whether the user is author of the given proposal, either directly
      # authoring it or via a user group.
      #
      # user - the user to check for authorship
      def authored_by?(user)
        author == user || user.user_groups.include?(user_group)
      end

      # Returns the normalized author, whether it's a user group or a user. Ideally this should be
      # the *author* method, but it's pending a refactor.
      #
      # Returns an Author, a UserGroup or nil.
      def normalized_author
        user_group || author
      end

      private

      def verified_user_group
        return unless user_group
        errors.add :user_group, :invalid unless user_group.verified?
      end

      def user_group_membership
        return unless user_group
        errors.add :user_group, :invalid unless user_group.users.include? author
      end

      def author_belongs_to_organization
        return if !author || !organization
        errors.add(:author, :invalid) unless author.organization == organization
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.12.2 lib/decidim/authorable.rb
decidim-core-0.12.1 lib/decidim/authorable.rb
decidim-core-0.12.0 lib/decidim/authorable.rb
decidim-core-0.11.2 lib/decidim/authorable.rb
decidim-core-0.12.0.pre lib/decidim/authorable.rb
decidim-core-0.11.1 lib/decidim/authorable.rb
decidim-core-0.11.0.pre1 lib/decidim/authorable.rb