Sha256: 15e110721297da9f47fff30b9a0fd4c5b9dae93ab83d098598af06caedb00f84

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module CanCan
  # This module adds the accessible_by class method to a model. It is included in the model adapters.
  module ModelAdditions
    module ClassMethods
      # Returns a scope which fetches only the records that the passed ability
      # can perform a given action on. The action defaults to :index. This
      # is usually called from a controller and passed the +current_ability+.
      #
      #   @articles = Article.accessible_by(current_ability)
      #
      # Here only the articles which the user is able to read will be returned.
      # If the user does not have permission to read any articles then an empty
      # result is returned. Since this is a scope it can be combined with any
      # other scopes or pagination.
      #
      # An alternative action can optionally be passed as a second argument.
      #
      #   @articles = Article.accessible_by(current_ability, :update)
      #
      # Here only the articles which the user can update are returned.
      def accessible_by(ability, action = :index, strategy: CanCan.accessible_by_strategy)
        CanCan.with_accessible_by_strategy(strategy) do
          ability.model_adapter(self, action).database_records
        end
      end
    end

    def self.included(base)
      base.extend ClassMethods
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/cancancan-3.6.1/lib/cancan/model_additions.rb
cancancan-3.5.0 lib/cancan/model_additions.rb
cancancan-3.4.0 lib/cancan/model_additions.rb
cancancan-3.3.0 lib/cancan/model_additions.rb