Sha256: f982642538d9d6e36216ab599c61ae3b5dee8a39e8603bcbec3217ca0268e947

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # Exhibit authorization roles
  class Role < ActiveRecord::Base
    belongs_to :resource, polymorphic: true, optional: true
    belongs_to :user, class_name: Spotlight::Engine.config.user_class, autosave: true, optional: false

    validates :role, inclusion: { in: Spotlight::Engine.config.exhibit_roles }
    validate :user_must_be_unique, if: :user

    def user_key
      if user
        @user_key = user.user_key
      else
        @user_key
      end
    end

    # setting user key causes the user to get set
    def user_key=(key)
      @user_key = key
      self.user ||= Spotlight::Engine.user_class.find_by_user_key(key)
      self.user ||= Spotlight::Engine.user_class.invite!(email: user_key, skip_invitation: true)
      user.user_key = key
    end

    protected

    # This is just like
    #    validates :user, uniqueness: { scope: :exhibit}
    # but it puts the error message on the user_key instead of user so that the form will render correctly
    def user_must_be_unique
      errors.add(:user_key, 'already a member of this exhibit') if Spotlight::Role.where(resource:, user:).where.not(id:).any?
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
blacklight-spotlight-4.7.0 app/models/spotlight/role.rb
blacklight-spotlight-4.6.1 app/models/spotlight/role.rb
blacklight-spotlight-4.6.0 app/models/spotlight/role.rb
blacklight-spotlight-4.5.0 app/models/spotlight/role.rb
blacklight-spotlight-4.4.0 app/models/spotlight/role.rb
blacklight-spotlight-4.3.6 app/models/spotlight/role.rb
blacklight-spotlight-4.3.5 app/models/spotlight/role.rb
blacklight-spotlight-4.3.4 app/models/spotlight/role.rb
blacklight-spotlight-4.3.3 app/models/spotlight/role.rb
blacklight-spotlight-4.3.2 app/models/spotlight/role.rb
blacklight-spotlight-4.3.1 app/models/spotlight/role.rb
blacklight-spotlight-4.3.0 app/models/spotlight/role.rb