dynamically detect which model adapter to use given a class

This commit is contained in:
Ryan Bates
2010-12-30 14:42:19 -08:00
parent cc30e838c0
commit bbb02f7c8f
9 changed files with 51 additions and 1 deletions

View File

@@ -343,6 +343,14 @@ describe CanCan::Ability do
end
end
it "should determine model adapter class by asking AbstractAdapter" do
model_class = Object.new
adapter_class = Object.new
stub(CanCan::ModelAdapters::AbstractAdapter).adapter_class(model_class) { adapter_class }
stub(adapter_class).new(model_class, []) { :adapter_instance }
@ability.model_adapter(model_class, :read).should == :adapter_instance
end
describe "unauthorized message" do
after(:each) do
I18n.backend = nil

View File

@@ -33,6 +33,12 @@ if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
@comment_table = Comment.table_name
end
it "should be for only active record classes" do
CanCan::ModelAdapters::ActiveRecordAdapter.should_not be_for_class(Object)
CanCan::ModelAdapters::ActiveRecordAdapter.should be_for_class(Article)
CanCan::ModelAdapters::AbstractAdapter.adapter_class(Article).should == CanCan::ModelAdapters::ActiveRecordAdapter
end
it "should not fetch any records when no abilities are defined" do
Article.create!
Article.accessible_by(@ability).should be_empty

View File

@@ -0,0 +1,7 @@
require "spec_helper"
describe CanCan::ModelAdapters::DefaultAdapter do
it "should be default for generic classes" do
CanCan::ModelAdapters::AbstractAdapter.adapter_class(Object).should == CanCan::ModelAdapters::DefaultAdapter
end
end