Sha256: 04b79719abcc059f40b59e6ac4c718bd7015e5c48e7a50295b9e20cd82bf5688

Contents?: true

Size: 1.32 KB

Versions: 46

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # Spotlight user mixins for roles
  module User
    extend ActiveSupport::Concern
    included do
      has_many :roles, class_name: 'Spotlight::Role', dependent: :destroy
      has_many :exhibits, class_name: 'Spotlight::Exhibit', through: :roles, source: 'resource', source_type: 'Spotlight::Exhibit'

      scope :with_roles, -> { where(id: Spotlight::Role.distinct.pluck(:user_id)) }

      before_create :add_default_roles
    end

    def superadmin?
      roles.where(role: 'admin', resource: Spotlight::Site.instance).any?
    end

    def exhibit_roles
      roles.where(resource_type: 'Spotlight::Exhibit').where.not(role: 'viewer')
    end

    def viewer_roles
      roles.where(resource_type: 'Spotlight::Exhibit', role: 'viewer')
    end

    def admin_roles
      exhibit_roles.where(role: 'admin')
    end

    def add_default_roles
      return unless Spotlight::Engine.config.assign_default_roles_to_first_user

      roles.build role: 'admin', resource: Spotlight::Site.instance unless self.class.any?
    end

    def invite_pending?
      invited_to_sign_up? && !invitation_accepted?
    end

    alias_attribute :user_key, :email

    ##
    # Class-level user mixins
    module ClassMethods
      def find_by_user_key(key)
        find_by email: key
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
blacklight-spotlight-4.3.6 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.3.5 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.3.4 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.3.3 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.3.2 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.3.1 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.3.0 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.2.0 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.1.2 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.1.1 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.1.0 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.0.3 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.0.2 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.0.1 app/models/concerns/spotlight/user.rb
blacklight-spotlight-4.0.0 app/models/concerns/spotlight/user.rb
blacklight-spotlight-3.6.0.beta10 app/models/concerns/spotlight/user.rb
blacklight-spotlight-3.6.0.beta9 app/models/concerns/spotlight/user.rb
blacklight-spotlight-3.6.0.beta8 app/models/concerns/spotlight/user.rb
blacklight-spotlight-3.5.0.4 app/models/concerns/spotlight/user.rb
blacklight-spotlight-3.6.0.beta7 app/models/concerns/spotlight/user.rb