Browse files

Replace meta_where with squeel, fix pending rspec tests.

  • Loading branch information...
1 parent aed9f26 commit d29bb486ef4067607945500c5febcedb10527e95 @fillman committed Aug 5, 2012
View
1 cancan.gemspec
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec", "~> 2.9.0"
s.add_development_dependency "rails", "~> 3.2.6"
s.add_development_dependency "sqlite3", "~> 1.3.5"
+ s.add_development_dependency "squeel", "~> 1.0.8"
s.add_development_dependency "dm-core", "~> 1.2.0"
s.add_development_dependency "dm-sqlite-adapter", "~> 1.2.0"
View
14 lib/cancan/model_adapters/active_record_adapter.rb
@@ -6,17 +6,17 @@ def self.for_class?(model_class)
end
def self.override_condition_matching?(subject, name, value)
- name.kind_of?(MetaWhere::Column) if defined? MetaWhere
+ name.kind_of?(Squeel::Nodes::Predicate) if defined? Squeel
end
def self.matches_condition?(subject, name, value)
- subject_value = subject.send(name.column)
- if name.method.to_s.ends_with? "_any"
- value.any? { |v| meta_where_match? subject_value, name.method.to_s.sub("_any", ""), v }
- elsif name.method.to_s.ends_with? "_all"
- value.all? { |v| meta_where_match? subject_value, name.method.to_s.sub("_all", ""), v }
+ subject_value = subject.send(name.expr)
+ if name.method_name.to_s.ends_with? "_any"
+ value.any? { |v| meta_where_match? subject_value, name.method_name.to_s.sub("_any", ""), v }
+ elsif name.method_name.to_s.ends_with? "_all"
+ value.all? { |v| meta_where_match? subject_value, name.method_name.to_s.sub("_all", ""), v }
else
- meta_where_match? subject_value, name.method, value
+ meta_where_match? subject_value, name.method_name, value
end
end
View
15 spec/cancan/model_adapters/active_record_adapter_spec.rb
@@ -92,7 +92,6 @@ class Comment < ActiveRecord::Base
end
it "only reads comments for visible categories through articles" do
- pending "does ActiveRecord no longer support a deep nested hash of conditions?"
@ability.can :read, :comments, :article => { :category => { :visible => true } }
comment1 = Comment.create!(:article => Article.create!(:category => Category.create!(:visible => true)))
comment2 = Comment.create!(:article => Article.create!(:category => Category.create!(:visible => false)))
@@ -219,19 +218,17 @@ class Comment < ActiveRecord::Base
end
it "restricts articles given a MetaWhere condition" do
- pending
@ability.can :read, :articles, :priority.lt => 2
article1 = Article.create!(:priority => 1)
article2 = Article.create!(:priority => 3)
Article.accessible_by(@ability).should == [article1]
- @ability.should be_able_to(:read, article1)
+ @ability.can?(:read, article1).should be_true
@ability.should_not be_able_to(:read, article2)
end
it "should merge MetaWhere and non-MetaWhere conditions" do
- pending
- @ability.can :read, Article, :priority.lt => 2
- @ability.can :read, Article, :priority => 1
+ @ability.can :read, :articles, :priority.lt => 2
+ @ability.can :read, :articles, :priority => 1
article1 = Article.create!(:priority => 1)
article2 = Article.create!(:priority => 3)
Article.accessible_by(@ability).should == [article1]
@@ -240,7 +237,6 @@ class Comment < ActiveRecord::Base
end
it "matches any MetaWhere condition" do
- pending
adapter = CanCan::ModelAdapters::ActiveRecordAdapter
article1 = Article.new(:priority => 1, :name => "Hello World")
adapter.matches_condition?(article1, :priority.eq, 1).should be_true
@@ -270,9 +266,8 @@ class Comment < ActiveRecord::Base
adapter.matches_condition?(article1, :name.like, "%helo%").should be_false
adapter.matches_condition?(article1, :name.like, "hello").should be_false
adapter.matches_condition?(article1, :name.like, "hello.world").should be_false
- # For some reason this is reporting "The not_matches MetaWhere condition is not supported."
- # adapter.matches_condition?(article1, :name.nlike, "%helo%").should be_true
- # adapter.matches_condition?(article1, :name.nlike, "%ello worl%").should be_false
+ adapter.matches_condition?(article1, :name.nlike, "%helo%").should be_true
+ adapter.matches_condition?(article1, :name.nlike, "%ello worl%").should be_false
end
end
end
View
8 spec/spec_helper.rb
@@ -3,6 +3,7 @@
require "sqlite3"
require "active_record"
+require "squeel"
case ENV["MODEL_ADAPTER"]
when "data_mapper"
@@ -24,6 +25,13 @@
config.run_all_when_everything_filtered = true
end
+Squeel.configure do |config|
+ config.load_core_extensions :symbol
+ config.alias_predicate :ne, :not_eq
+ config.alias_predicate :nin, :not_in
+ config.alias_predicate :nlike, :not_like
+end
+
class Ability
include CanCan::Ability

0 comments on commit d29bb48

Please sign in to comment.