Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Replace meta_where with squeel, fix pending rspec tests. #711

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cancan.gemspec
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions lib/cancan/model_adapters/active_record_adapter.rb
Expand Up @@ -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

Expand Down
15 changes: 5 additions & 10 deletions spec/cancan/model_adapters/active_record_adapter_spec.rb
Expand Up @@ -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)))
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,6 +3,7 @@

require "sqlite3"
require "active_record"
require "squeel"

case ENV["MODEL_ADAPTER"]
when "data_mapper"
Expand All @@ -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

Expand Down