Sha256: e52bdae87800eb6def7c2ced64c0be5f8f7e3929d21d3f39d8c9e72ead801f46

Contents?: true

Size: 1.16 KB

Versions: 24

Compression:

Stored size: 1.16 KB

Contents

module Spotlight
  ##
  # Exhibit authorization roles
  class Role < ActiveRecord::Base
    ROLES = %w(admin curator).freeze
    belongs_to :resource, polymorphic: true, optional: true
    belongs_to :user, class_name: Spotlight::Engine.config.user_class, autosave: true, required: true
    validates :role, inclusion: { in: 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: resource, user: user).where.not(id: id).any?
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
blacklight-spotlight-2.3.2 app/models/spotlight/role.rb
blacklight-spotlight-2.3.1 app/models/spotlight/role.rb
blacklight-spotlight-2.3.0 app/models/spotlight/role.rb
blacklight-spotlight-2.2.1 app/models/spotlight/role.rb
blacklight-spotlight-2.2.0 app/models/spotlight/role.rb
blacklight-spotlight-2.1.0 app/models/spotlight/role.rb
blacklight-spotlight-2.0.2 app/models/spotlight/role.rb
blacklight-spotlight-2.0.1 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0.rc6 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0.rc5 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0.rc4 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0.rc3 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0.rc2 app/models/spotlight/role.rb
blacklight-spotlight-2.0.0.rc1 app/models/spotlight/role.rb
blacklight-spotlight-1.5.1 app/models/spotlight/role.rb
blacklight-spotlight-1.4.1 app/models/spotlight/role.rb
blacklight-spotlight-1.4.0 app/models/spotlight/role.rb
blacklight-spotlight-1.3.0 app/models/spotlight/role.rb
blacklight-spotlight-1.2.0 app/models/spotlight/role.rb