Module: CanCan

Defined in:
lib/cancan/model_adapters/sti_normalizer.rb,
lib/cancan/rule.rb,
lib/cancan/config.rb,
lib/cancan/ability.rb,
lib/cancan/version.rb,
lib/cancan/relevant.rb,
lib/cancan/exceptions.rb,
lib/cancan/ability/rules.rb,
lib/cancan/ability/actions.rb,
lib/cancan/model_additions.rb,
lib/cancan/rules_compressor.rb,
lib/cancan/conditions_matcher.rb,
lib/cancan/controller_resource.rb,
lib/cancan/controller_additions.rb,
lib/cancan/parameter_validators.rb,
lib/cancan/controller_resource_finder.rb,
lib/cancan/controller_resource_loader.rb,
lib/cancan/controller_resource_builder.rb,
lib/cancan/controller_resource_sanitizer.rb,
lib/cancan/unauthorized_message_resolver.rb,
lib/cancan/model_adapters/default_adapter.rb,
lib/cancan/controller_resource_name_finder.rb,
lib/cancan/model_adapters/abstract_adapter.rb,
lib/cancan/ability/strong_parameter_support.rb,
lib/cancan/model_adapters/conditions_extractor.rb,
lib/cancan/model_adapters/active_record_adapter.rb,
lib/cancan/model_adapters/conditions_normalizer.rb,
lib/cancan/model_adapters/active_record_4_adapter.rb,
lib/cancan/model_adapters/active_record_5_adapter.rb

Overview

this class is responsible of normalizing the hash of conditions by exploding has_many through associations when a condition is defined with an has_many thorugh association this is exploded in all its parts TODO: it could identify STI and normalize it

Defined Under Namespace

Modules: Ability, ConditionsMatcher, ControllerAdditions, ControllerResourceBuilder, ControllerResourceFinder, ControllerResourceLoader, ControllerResourceNameFinder, ControllerResourceSanitizer, ModelAdapters, ModelAdditions, ParameterValidators, Relevant, UnauthorizedMessageResolver Classes: AccessDenied, AttributeArgumentError, AuthorizationNotPerformed, BlockAndConditionsError, ControllerResource, Error, ImplementationRemoved, NotImplemented, Rule, RulesCompressor, WrongAssociationName

Constant Summary collapse

VERSION =
'3.2.0'.freeze

Class Method Summary collapse

Class Method Details

.accessible_by_strategyObject

Determines how CanCan should build queries when calling accessible_by, if the query will contain a join. The default strategy is ‘:subquery`.

# config/initializers/cancan.rb
CanCan.accessible_by_strategy = :subquery

Valid strategies are:

  • :subquery - Creates a nested query with all joins, wrapped by a

    WHERE IN query.
    
  • :left_join - Calls the joins directly using ‘left_joins`, and

    ensures records are unique using `distinct`. Note that
    `distinct` is not reliable in some cases. See
    https://github.com/CanCanCommunity/cancancan/pull/605
    


23
24
25
# File 'lib/cancan/config.rb', line 23

def self.accessible_by_strategy
  @accessible_by_strategy || default_accessible_by_strategy
end

.accessible_by_strategy=(value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cancan/config.rb', line 38

def self.accessible_by_strategy=(value)
  unless valid_accessible_by_strategies.include?(value)
    raise ArgumentError, "accessible_by_strategy must be one of #{valid_accessible_by_strategies.join(', ')}"
  end

  if value == :subquery && CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0')
    raise ArgumentError, 'accessible_by_strategy = :subquery requires ActiveRecord 5 or newer'
  end

  @accessible_by_strategy = value
end

.default_accessible_by_strategyObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/cancan/config.rb', line 27

def self.default_accessible_by_strategy
  if CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0')
    # see https://github.com/CanCanCommunity/cancancan/pull/655 for where this was added
    # the `subquery` strategy (from https://github.com/CanCanCommunity/cancancan/pull/619
    # only works in Rails 5 and higher
    :left_join
  else
    :subquery
  end
end

.valid_accessible_by_strategiesObject



4
5
6
7
8
# File 'lib/cancan/config.rb', line 4

def self.valid_accessible_by_strategies
  strategies = [:left_join]
  strategies << :subquery unless CanCan::ModelAdapters::ActiveRecordAdapter.version_lower?('5.0.0')
  strategies
end