Module: CanCan::ModelAdapters::ActiveRecordAdapter

Included in:
ActiveRecord3Adapter, ActiveRecord4Adapter
Defined in:
lib/cancan/model_adapters/active_record_adapter.rb

Instance Method Summary collapse

Instance Method Details

#conditionsObject

Returns conditions intended to be used inside a database query. Normally you will not call this method directly, but instead go through ModelAdditions#accessible_by.

If there is only one “can” definition, a hash of conditions will be returned matching the one defined.

can :manage, User, :id => 1
query(:manage, User).conditions # => { :id => 1 }

If there are multiple “can” definitions, a SQL string will be returned to handle complex cases.

can :manage, User, :id => 1
can :manage, User, :manager_id => 1
cannot :manage, User, :self_managed => true
query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))"


19
20
21
22
23
24
25
26
27
28
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 19

def conditions
  if @rules.size == 1 && @rules.first.base_behavior
    # Return the conditions directly if there's just one definition
    tableized_conditions(@rules.first.conditions).dup
  else
    @rules.reverse.inject(false_sql) do |sql, rule|
      merge_conditions(sql, tableized_conditions(rule.conditions).dup, rule.base_behavior)
    end
  end
end

#database_recordsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 63

def database_records
  if override_scope
    @model_class.where(nil).merge(override_scope)
  elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins)
    if mergeable_conditions?
      build_relation(conditions)
    else
      build_relation(*(@rules.map(&:conditions)))
    end
  else
    @model_class.all(:conditions => conditions, :joins => joins)
  end
end

#joinsObject

Returns the associations used in conditions for the :joins option of a search. See ModelAdditions#accessible_by



55
56
57
58
59
60
61
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 55

def joins
  joins_hash = {}
  @rules.each do |rule|
    merge_joins(joins_hash, rule.associations_hash)
  end
  clean_joins(joins_hash) unless joins_hash.empty?
end

#tableized_conditions(conditions, model_class = @model_class) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 30

def tableized_conditions(conditions, model_class = @model_class)
  return conditions unless conditions.kind_of? Hash
  conditions.inject({}) do |result_hash, (name, value)|
    if value.kind_of? Hash
      value = value.dup
      association_class = model_class.reflect_on_association(name).klass.name.constantize
      nested = value.inject({}) do |nested,(k,v)|
        if v.kind_of? Hash
          value.delete(k)
          nested[k] = v
        else
          result_hash[model_class.reflect_on_association(name).table_name.to_sym] = value
        end
        nested
      end
      result_hash.merge!(tableized_conditions(nested,association_class))
    else
      result_hash[name] = value
    end
    result_hash
  end
end