Class: CanCan::ModelAdapters::DataMapperAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/cancan/model_adapters/data_mapper_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

adapter_class, inherited, #initialize, matches_condition?, override_condition_matching?

Constructor Details

This class inherits a constructor from CanCan::ModelAdapters::AbstractAdapter

Class Method Details

.find(model_class, id) ⇒ Object



8
9
10
# File 'lib/cancan/model_adapters/data_mapper_adapter.rb', line 8

def self.find(model_class, id)
  model_class.get(id)
end

.for_class?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/cancan/model_adapters/data_mapper_adapter.rb', line 4

def self.for_class?(model_class)
  model_class <= DataMapper::Resource
end

.matches_conditions_hash?(subject, conditions) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/cancan/model_adapters/data_mapper_adapter.rb', line 16

def self.matches_conditions_hash?(subject, conditions)
  collection = DataMapper::Collection.new(subject.query, [ subject ])
  !!collection.first(conditions)
end

.override_conditions_hash_matching?(subject, conditions) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/cancan/model_adapters/data_mapper_adapter.rb', line 12

def self.override_conditions_hash_matching?(subject, conditions)
  conditions.any? { |k,v| !k.kind_of?(Symbol) }
end

Instance Method Details

#database_recordsObject



21
22
23
24
25
26
27
28
29
# File 'lib/cancan/model_adapters/data_mapper_adapter.rb', line 21

def database_records
  scope = @model_class.all(:conditions => ["0 = 1"])
  cans, cannots = @rules.partition { |r| r.base_behavior }
  return scope if cans.empty?
  # apply unions first, then differences. this mean cannot overrides can
  cans.each    { |r| scope += @model_class.all(:conditions => r.conditions) }
  cannots.each { |r| scope -= @model_class.all(:conditions => r.conditions) }
  scope
end