adding joins clause to accessible_by when conditions are across associations
This commit is contained in:
@@ -10,19 +10,19 @@ describe CanCan::ActiveRecordAdditions do
|
||||
end
|
||||
|
||||
it "should call where(:id => nil) when no ability is defined so no records are found" do
|
||||
stub(@model_class).where(:id => nil) { :no_where }
|
||||
stub(@model_class).where(:id => nil).stub!.joins(nil) { :no_where }
|
||||
@model_class.accessible_by(@ability, :read).should == :no_where
|
||||
end
|
||||
|
||||
it "should call where with matching ability conditions" do
|
||||
@ability.can :read, @model_class, :foo => 1
|
||||
stub(@model_class).where(:foo => 1) { :found_records }
|
||||
@ability.can :read, @model_class, :foo => {:bar => 1}
|
||||
stub(@model_class).where(:foo => { :bar => 1 }).stub!.joins([:foo]) { :found_records }
|
||||
@model_class.accessible_by(@ability, :read).should == :found_records
|
||||
end
|
||||
|
||||
it "should default to :read ability and use scoped when where isn't available" do
|
||||
@ability.can :read, @model_class, :foo => 1
|
||||
stub(@model_class).scoped(:conditions => {:foo => 1}) { :found_records }
|
||||
@ability.can :read, @model_class, :foo => {:bar => 1}
|
||||
stub(@model_class).scoped(:conditions => {:foo => {:bar => 1}}, :joins => [:foo]) { :found_records }
|
||||
@model_class.accessible_by(@ability).should == :found_records
|
||||
end
|
||||
end
|
||||
|
||||
33
spec/cancan/can_definition_spec.rb
Normal file
33
spec/cancan/can_definition_spec.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe CanCan::CanDefinition do
|
||||
before(:each) do
|
||||
@conditions = {}
|
||||
@can = CanCan::CanDefinition.new(true, :read, Integer, @conditions, nil)
|
||||
end
|
||||
|
||||
it "should return no association joins if none exist" do
|
||||
@can.association_joins.should be_nil
|
||||
end
|
||||
|
||||
it "should return no association for joins if just attributes" do
|
||||
@conditions[:foo] = :bar
|
||||
@can.association_joins.should be_nil
|
||||
end
|
||||
|
||||
it "should return single association for joins" do
|
||||
@conditions[:foo] = {:bar => 1}
|
||||
@can.association_joins.should == [:foo]
|
||||
end
|
||||
|
||||
it "should return multiple associations for joins" do
|
||||
@conditions[:foo] = {:bar => 1}
|
||||
@conditions[:test] = {1 => 2}
|
||||
@can.association_joins.map(&:to_s).sort.should == [:foo, :test].map(&:to_s).sort
|
||||
end
|
||||
|
||||
it "should return nested associations for joins" do
|
||||
@conditions[:foo] = {:bar => {1 => 2}}
|
||||
@can.association_joins.should == [{:foo => [:bar]}]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user