From 4c5ba09f4cdda1788b230802e20df34854826928 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 29 Dec 2010 15:01:49 -0800 Subject: [PATCH] adding model adapter files in proper location with loading behavior --- Gemfile | 9 ++++++--- lib/cancan.rb | 6 ++++-- .../active_record_adapter.rb} | 0 .../data_mapper_adapter.rb} | 2 -- .../mongoid_adapter.rb} | 0 spec/cancan/model_adapters/data_mapper_adapter_spec.rb | 1 - spec/cancan/model_adapters/mongoid_adapter_spec.rb | 6 ++---- spec/spec_helper.rb | 2 ++ 8 files changed, 14 insertions(+), 12 deletions(-) rename lib/cancan/{active_record_additions.rb => model_adapters/active_record_adapter.rb} (100%) rename lib/cancan/{data_mapper_additions.rb => model_adapters/data_mapper_adapter.rb} (99%) rename lib/cancan/{mongoid_additions.rb => model_adapters/mongoid_adapter.rb} (100%) diff --git a/Gemfile b/Gemfile index 4e1c66e..e65ee88 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,13 @@ source "http://rubygems.org" -gemspec case ENV["MODEL_ADAPTER"] +when nil, "active_record" + gem "activerecord", :require => "active_record" +when "data_mapper" + gem "dm-core", "~> 1.0.2" when "mongoid" gem "bson_ext", "~> 1.1" gem "mongoid", "~> 2.0.0.beta.19" -when "data_mapper" - gem "dm-core", "~> 1.0.2" end + +gemspec diff --git a/lib/cancan.rb b/lib/cancan.rb index 1238cf5..03a5086 100644 --- a/lib/cancan.rb +++ b/lib/cancan.rb @@ -2,8 +2,10 @@ require 'cancan/ability' require 'cancan/rule' require 'cancan/controller_resource' require 'cancan/controller_additions' -require 'cancan/active_record_additions' -require 'cancan/mongoid_additions' require 'cancan/exceptions' require 'cancan/query' require 'cancan/inherited_resource' + +require 'cancan/model_adapters/active_record_adapter' if defined? ActiveRecord +require 'cancan/model_adapters/data_mapper_adapter' if defined? DataMapper +require 'cancan/model_adapters/mongoid_adapter' if defined? Mongoid diff --git a/lib/cancan/active_record_additions.rb b/lib/cancan/model_adapters/active_record_adapter.rb similarity index 100% rename from lib/cancan/active_record_additions.rb rename to lib/cancan/model_adapters/active_record_adapter.rb diff --git a/lib/cancan/data_mapper_additions.rb b/lib/cancan/model_adapters/data_mapper_adapter.rb similarity index 99% rename from lib/cancan/data_mapper_additions.rb rename to lib/cancan/model_adapters/data_mapper_adapter.rb index 679f5af..0723b4d 100644 --- a/lib/cancan/data_mapper_additions.rb +++ b/lib/cancan/model_adapters/data_mapper_adapter.rb @@ -1,5 +1,3 @@ -require 'dm-core' - module CanCan module Ability # could use alias_method_chain, but it's not worth adding activesupport as a gem dependency diff --git a/lib/cancan/mongoid_additions.rb b/lib/cancan/model_adapters/mongoid_adapter.rb similarity index 100% rename from lib/cancan/mongoid_additions.rb rename to lib/cancan/model_adapters/mongoid_adapter.rb diff --git a/spec/cancan/model_adapters/data_mapper_adapter_spec.rb b/spec/cancan/model_adapters/data_mapper_adapter_spec.rb index 4abe8ba..c7d6c55 100644 --- a/spec/cancan/model_adapters/data_mapper_adapter_spec.rb +++ b/spec/cancan/model_adapters/data_mapper_adapter_spec.rb @@ -1,6 +1,5 @@ if ENV["MODEL_ADAPTER"] == "data_mapper" require "spec_helper" - require 'cancan/data_mapper_additions' describe CanCan::DataMapperAdditions do before(:each) do diff --git a/spec/cancan/model_adapters/mongoid_adapter_spec.rb b/spec/cancan/model_adapters/mongoid_adapter_spec.rb index 66bb568..2a6d937 100644 --- a/spec/cancan/model_adapters/mongoid_adapter_spec.rb +++ b/spec/cancan/model_adapters/mongoid_adapter_spec.rb @@ -1,5 +1,4 @@ if ENV["MODEL_ADAPTER"] == "mongoid" - require 'mongoid' # require mongoid first so MongoidAdditions are loaded require "spec_helper" class MongoidCategory @@ -35,7 +34,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid" end Mongoid.configure do |config| - config.master = Mongo::Connection.new('127.0.0.1', 27017).db("cancan_mongoid_additions_spec") + config.master = Mongo::Connection.new('127.0.0.1', 27017).db("cancan_mongoid_spec") end describe CanCan::MongoidAdditions do @@ -50,7 +49,6 @@ if ENV["MODEL_ADAPTER"] == "mongoid" it "should not raise an error for ActiveRecord models" do @model_class = Class.new(Project) stub(@model_class).scoped { :scoped_stub } - @model_class.send(:include, CanCan::ActiveRecordAdditions) @ability = Object.new @ability.extend(CanCan::Ability) @@ -60,7 +58,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid" }.should_not raise_error end end - + context "Mongoid defined" do before(:each) do @model_class = MongoidProject diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index da6b77b..421855e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,8 @@ require 'rubygems' require 'bundler/setup' + Bundler.require(:default) + require 'supermodel' # shouldn't Bundler do this already? require 'active_support/all' require 'matchers'