switching data mapper to new adapter
This commit is contained in:
parent
e8d298c223
commit
f5dce44697
|
@ -1,32 +1,26 @@
|
||||||
module CanCan
|
module CanCan
|
||||||
module Ability
|
module ModelAdapters
|
||||||
# could use alias_method_chain, but it's not worth adding activesupport as a gem dependency
|
class DataMapperAdapter < AbstractAdapter
|
||||||
alias_method :query_without_data_mapper_support, :query
|
def self.for_class?(model_class)
|
||||||
def query(action, subject)
|
model_class <= DataMapper::Resource
|
||||||
if Object.const_defined?('DataMapper') && subject <= DataMapper::Resource
|
|
||||||
query_with_data_mapper_support(action, subject)
|
|
||||||
else
|
|
||||||
query_without_data_mapper_support(action, subject)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_with_data_mapper_support(action, subject)
|
def database_records
|
||||||
DataMapperQuery.new(subject, relevant_rules_for_query(action, subject))
|
scope = @model_class.all(:conditions => ['true=false'])
|
||||||
|
conditions.each do |condition|
|
||||||
|
scope += @model_class.all(:conditions => condition)
|
||||||
end
|
end
|
||||||
end
|
scope
|
||||||
|
|
||||||
class DataMapperQuery
|
|
||||||
def initialize(sanitizer, rules)
|
|
||||||
@sanitizer = sanitizer
|
|
||||||
@rules = rules
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def conditions
|
def conditions
|
||||||
@rules.map {|r| r.instance_variable_get(:@conditions) }
|
@rules.map(&:conditions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# This module is automatically included into all Active Record models.
|
module CanCan
|
||||||
module DataMapperAdditions
|
module DataMapperAdditions
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Returns a scope which fetches only the records that the passed ability
|
# Returns a scope which fetches only the records that the passed ability
|
||||||
|
@ -47,14 +41,7 @@ module CanCan
|
||||||
# Here only the articles which the user can update are returned. This
|
# Here only the articles which the user can update are returned. This
|
||||||
# internally uses Ability#conditions method, see that for more information.
|
# internally uses Ability#conditions method, see that for more information.
|
||||||
def accessible_by(ability, action = :read)
|
def accessible_by(ability, action = :read)
|
||||||
query = ability.query(action, self)
|
ability.model_adapter(self, action).database_records
|
||||||
|
|
||||||
scope = all(:conditions => ['true=false'])
|
|
||||||
query.conditions.each do |condition|
|
|
||||||
scope += all(:conditions => condition)
|
|
||||||
end
|
|
||||||
|
|
||||||
return scope
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
if ENV["MODEL_ADAPTER"] == "data_mapper"
|
if ENV["MODEL_ADAPTER"] == "data_mapper"
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
describe CanCan::DataMapperAdditions do
|
describe CanCan::ModelAdapters::DataMapperAdapter do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@model_class = Class.new
|
@model_class = Class.new
|
||||||
@model_class.class_eval do
|
@model_class.class_eval do
|
||||||
|
@ -13,6 +13,12 @@ if ENV["MODEL_ADAPTER"] == "data_mapper"
|
||||||
@ability.extend(CanCan::Ability)
|
@ability.extend(CanCan::Ability)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should be for only data mapper classes" do
|
||||||
|
CanCan::ModelAdapters::DataMapperAdapter.should_not be_for_class(Object)
|
||||||
|
CanCan::ModelAdapters::DataMapperAdapter.should be_for_class(@model_class)
|
||||||
|
CanCan::ModelAdapters::AbstractAdapter.adapter_class(@model_class).should == CanCan::ModelAdapters::DataMapperAdapter
|
||||||
|
end
|
||||||
|
|
||||||
it "should return no records when no ability is defined so no records are found" do
|
it "should return no records when no ability is defined so no records are found" do
|
||||||
@model_class.accessible_by(@ability, :read).should == 'no-match:'
|
@model_class.accessible_by(@ability, :read).should == 'no-match:'
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user