Sha256: 3b39bed3e496231686f3806be9767cf02d25e188ece1aef0960f0f819b49b6e1

Contents?: true

Size: 1.51 KB

Versions: 25

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true
module Hyrax
  ##
  # Provides an enumerator of the Work classes the user is authorized to create.
  #
  # @example
  #   QuickClassificationQuery.new(user: current_user).to_a
  class QuickClassificationQuery
    ##
    # @!attribute [r] user
    #   @return [::User]
    attr_reader :user

    # @param [::User] user the current user
    # @param [#call] concern_name_normalizer (String#constantize) a proc that translates names to classes
    # @param [Array<String>] models the options to display, defaults to everything.
    def initialize(user,
                   models: Hyrax.config.registered_curation_concern_types,
                   concern_name_normalizer: ->(str) { str.constantize })
      @user = user
      @concern_name_normalizer = concern_name_normalizer
      @models = models
    end

    def each(&block)
      authorized_models.each(&block)
    end

    # @return true if the requested concerns is same as all avaliable concerns
    def all?
      models == Hyrax.config.registered_curation_concern_types
    end

    # @return [Array] a list of all the requested concerns that the user can create
    def authorized_models
      normalized_model_names.select { |klass| user.can?(:create, klass) }
    end
    alias to_a authorized_models

    private

    attr_reader :concern_name_normalizer, :models

    # Transform the list of requested model names into a list of class names
    def normalized_model_names
      models.map { |name| concern_name_normalizer.call(name) }
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
hyrax-5.0.2 app/services/hyrax/quick_classification_query.rb
hyrax-5.0.1 app/services/hyrax/quick_classification_query.rb
hyrax-5.0.0 app/services/hyrax/quick_classification_query.rb
hyrax-5.0.0.rc3 app/services/hyrax/quick_classification_query.rb
hyrax-5.0.0.rc2 app/services/hyrax/quick_classification_query.rb
hyrax-5.0.0.rc1 app/services/hyrax/quick_classification_query.rb
hyrax-3.6.0 app/services/hyrax/quick_classification_query.rb
hyrax-4.0.0 app/services/hyrax/quick_classification_query.rb
hyrax-4.0.0.rc3 app/services/hyrax/quick_classification_query.rb
hyrax-4.0.0.rc2 app/services/hyrax/quick_classification_query.rb
hyrax-4.0.0.rc1 app/services/hyrax/quick_classification_query.rb
hyrax-3.5.0 app/services/hyrax/quick_classification_query.rb
hyrax-4.0.0.beta2 app/services/hyrax/quick_classification_query.rb
hyrax-3.4.2 app/services/hyrax/quick_classification_query.rb
hyrax-4.0.0.beta1 app/services/hyrax/quick_classification_query.rb
hyrax-3.4.1 app/services/hyrax/quick_classification_query.rb
hyrax-3.4.0 app/services/hyrax/quick_classification_query.rb
hyrax-3.3.0 app/services/hyrax/quick_classification_query.rb
hyrax-3.2.0 app/services/hyrax/quick_classification_query.rb
hyrax-3.1.0 app/services/hyrax/quick_classification_query.rb