Skip to content

Loading…

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

Open
wants to merge 1 commit into from

4 participants

@fillman

This is not finish. A lot of new features will come up using squeel. But at least it should work for most users having old styled meta_where conditions.

@andhapp
Collaborator

@filip Nice work.

AFAIK, with 2.0, plans are to rewrite the entire meta_where related code and make it more extendable for the future. I will run this past @ryanb and get his thoughts on it.

@fillman

Sure, maybe there's somewhere a todo list or plans list for 2.0 branch so I can read what is needed and help you guys?

@the8472

pending "does ActiveRecord no longer support a deep nested hash of conditions?"

This would only work with squeel. Not when combining cancan with vanilla active record.

Not that this would really change anything as this feature is currently broken anyway with newer rails versions.

@ogerman ogerman added a commit to ogerman/cancan that referenced this pull request
@fillman fillman merge pull #711 by fillman (Replace meta_where with squeel, fix pendi…
…ng rspec tests)
423ae86
@xhoy

Dear submitter, Since cancan/raynB hasn't been active for more than 6 months and no body else then ryam himself has commit permissions the cancan project is on a stand still.
Since cancan has several issues including missing support for rails 4 cancan is moving forward to cancancan. More details on: #994

If your feel that your pull request or bug is still applicable (and hasn't been merged in to cancan) it would be really appreciated if you would resubmit it to cancancan (https://github.com/cancancommunity/cancancan)

We hope to see you on the other side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Something went wrong with that request. Please try again.