cleanup whitespace

This commit is contained in:
Ryan Bates 2010-12-30 14:43:25 -08:00
parent bbb02f7c8f
commit 8628aa0038
2 changed files with 40 additions and 40 deletions

View File

@ -9,18 +9,18 @@ module CanCan
query_without_mongoid_support(action, subject) query_without_mongoid_support(action, subject)
end end
end end
def query_with_mongoid_support(action, subject) def query_with_mongoid_support(action, subject)
MongoidQuery.new(subject, relevant_rules_for_query(action, subject)) MongoidQuery.new(subject, relevant_rules_for_query(action, subject))
end end
end end
class MongoidQuery class MongoidQuery
def initialize(sanitizer, rules) def initialize(sanitizer, rules)
@sanitizer = sanitizer @sanitizer = sanitizer
@rules = rules @rules = rules
end end
def conditions def conditions
if @rules.size == 0 if @rules.size == 0
false_query false_query
@ -28,36 +28,36 @@ module CanCan
@rules.first.instance_variable_get(:@conditions) @rules.first.instance_variable_get(:@conditions)
end end
end end
def false_query def false_query
# this query is sure to return no results # this query is sure to return no results
{:_id => {'$exists' => false, '$type' => 7}} # type 7 is an ObjectID (default for _id) {:_id => {'$exists' => false, '$type' => 7}} # type 7 is an ObjectID (default for _id)
end end
end end
# customize to handle Mongoid queries in ability definitions conditions # customize to handle Mongoid queries in ability definitions conditions
# Mongoid Criteria are simpler to check than normal conditions hashes # Mongoid Criteria are simpler to check than normal conditions hashes
# When no conditions are given, true should be returned. # When no conditions are given, true should be returned.
# The default CanCan behavior relies on the fact that conditions.all? will return true when conditions is empty # The default CanCan behavior relies on the fact that conditions.all? will return true when conditions is empty
# The way ruby handles all? for empty hashes can be unexpected: # The way ruby handles all? for empty hashes can be unexpected:
# {}.all?{|a| a == 5} # {}.all?{|a| a == 5}
# => true # => true
# {}.all?{|a| a != 5} # {}.all?{|a| a != 5}
# => true # => true
class Rule class Rule
def matches_conditions_hash_with_mongoid_subject?(subject, conditions = @conditions) def matches_conditions_hash_with_mongoid_subject?(subject, conditions = @conditions)
if defined?(::Mongoid) && subject.class.include?(::Mongoid::Document) && conditions.any?{|k,v| !k.kind_of?(Symbol)} if defined?(::Mongoid) && subject.class.include?(::Mongoid::Document) && conditions.any?{|k,v| !k.kind_of?(Symbol)}
if conditions.empty? if conditions.empty?
true true
else else
subject.class.where(conditions).include?(subject) # just use Mongoid's where function subject.class.where(conditions).include?(subject) # just use Mongoid's where function
end end
else else
matches_conditions_hash_without_mongoid_subject? subject, conditions matches_conditions_hash_without_mongoid_subject? subject, conditions
end end
end end
# could use alias_method_chain, but it's not worth adding activesupport as a gem dependency # could use alias_method_chain, but it's not worth adding activesupport as a gem dependency
alias_method :matches_conditions_hash_without_mongoid_subject?, :matches_conditions_hash? alias_method :matches_conditions_hash_without_mongoid_subject?, :matches_conditions_hash?
alias_method :matches_conditions_hash?, :matches_conditions_hash_with_mongoid_subject? alias_method :matches_conditions_hash?, :matches_conditions_hash_with_mongoid_subject?
end end
@ -71,40 +71,40 @@ module CanCan
# is usually called from a controller and passed the +current_ability+. # is usually called from a controller and passed the +current_ability+.
# #
# @articles = Article.accessible_by(current_ability) # @articles = Article.accessible_by(current_ability)
# #
# Here only the articles which the user is able to read will be returned. # Here only the articles which the user is able to read will be returned.
# If the user does not have permission to read any articles then an empty # If the user does not have permission to read any articles then an empty
# result is returned. Since this is a scope it can be combined with any # result is returned. Since this is a scope it can be combined with any
# other scopes or pagination. # other scopes or pagination.
# #
# An alternative action can optionally be passed as a second argument. # An alternative action can optionally be passed as a second argument.
# #
# @articles = Article.accessible_by(current_ability, :update) # @articles = Article.accessible_by(current_ability, :update)
# #
# Here only the articles which the user can update are returned. This # Here only the articles which the user can update are returned. This
# internally uses Ability#conditions method, see that for more information. # internally uses Ability#conditions method, see that for more information.
def accessible_by(ability, action = :read) def accessible_by(ability, action = :read)
query = ability.query(action, self) query = ability.query(action, self)
where(query.conditions) where(query.conditions)
end end
end end
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
end end
end end
end end
# Info on monkeypatching Mongoid : # Info on monkeypatching Mongoid :
# http://log.mazniak.org/post/719062325/monkey-patching-activesupport-concern-and-you#footer # http://log.mazniak.org/post/719062325/monkey-patching-activesupport-concern-and-you#footer
if defined?(::Mongoid) if defined?(::Mongoid)
module Mongoid module Mongoid
module Components module Components
old_block = @_included_block old_block = @_included_block
@_included_block = Proc.new do @_included_block = Proc.new do
class_eval(&old_block) if old_block class_eval(&old_block) if old_block
include CanCan::MongoidAdditions include CanCan::MongoidAdditions
end end
end end
end end
end end

View File

@ -11,7 +11,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
class MongoidProject class MongoidProject
include Mongoid::Document include Mongoid::Document
include CanCan::MongoidAdditions include CanCan::MongoidAdditions
referenced_in :mongoid_category referenced_in :mongoid_category
class << self class << self
@ -42,7 +42,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
before(:all) do before(:all) do
@mongoid_class = Object.send(:remove_const, :Mongoid) @mongoid_class = Object.send(:remove_const, :Mongoid)
end end
after(:all) do after(:all) do
Object.const_set(:Mongoid, @mongoid_class) Object.const_set(:Mongoid, @mongoid_class)
end end
@ -51,12 +51,12 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
stub(@model_class).scoped { :scoped_stub } stub(@model_class).scoped { :scoped_stub }
@ability = Object.new @ability = Object.new
@ability.extend(CanCan::Ability) @ability.extend(CanCan::Ability)
@ability.can :read, @model_class @ability.can :read, @model_class
lambda { lambda {
@ability.can? :read, @model_class.new @ability.can? :read, @model_class.new
}.should_not raise_error }.should_not raise_error
end end
end end
context "Mongoid defined" do context "Mongoid defined" do
@ -69,8 +69,8 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
after(:each) do after(:each) do
Mongoid.master.collections.select do |collection| Mongoid.master.collections.select do |collection|
collection.name !~ /system/ collection.name !~ /system/
end.each(&:drop) end.each(&:drop)
end end
it "should compare properties on mongoid documents with the conditions hash" do it "should compare properties on mongoid documents with the conditions hash" do
model = @model_class.new model = @model_class.new
@ -93,16 +93,16 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
dude = @model_class.create :title => 'Dude' dude = @model_class.create :title => 'Dude'
@model_class.accessible_by(@ability, :read).should == [sir] @model_class.accessible_by(@ability, :read).should == [sir]
end end
it "should return everything when the defined ability is manage all" do it "should return everything when the defined ability is manage all" do
@ability.can :manage, :all @ability.can :manage, :all
sir = @model_class.create :title => 'Sir' sir = @model_class.create :title => 'Sir'
lord = @model_class.create :title => 'Lord' lord = @model_class.create :title => 'Lord'
dude = @model_class.create :title => 'Dude' dude = @model_class.create :title => 'Dude'
@model_class.accessible_by(@ability, :read).entries.should == [sir, lord, dude] @model_class.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
end end
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
@ -119,7 +119,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
describe "activates only when there are Criteria in the hash" do describe "activates only when there are Criteria in the hash" do
it "Calls where on the model class when there are criteria" do it "Calls where on the model class when there are criteria" do
obj = @model_class.create :title => 'Bird' obj = @model_class.create :title => 'Bird'
@conditions = {:title.nin => ["Fork", "Spoon"]} @conditions = {:title.nin => ["Fork", "Spoon"]}
mock(@model_class).where(@conditions) {[obj]} mock(@model_class).where(@conditions) {[obj]}
@ability.can :read, @model_class, @conditions @ability.can :read, @model_class, @conditions
@ability.should be_able_to(:read, obj) @ability.should be_able_to(:read, obj)
@ -150,7 +150,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
obj2 = @model_class.create :titles => ['Palatin', 'Margrave', 'Marquis'] obj2 = @model_class.create :titles => ['Palatin', 'Margrave', 'Marquis']
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
it "should handle :field.exists" do it "should handle :field.exists" do
obj = @model_class.create :titles => ['Palatin', 'Margrave'] obj = @model_class.create :titles => ['Palatin', 'Margrave']
@ -170,12 +170,12 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
obj2 = @model_class.create :age => 40 obj2 = @model_class.create :age => 40
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
end end
it "should call where with matching ability conditions" do it "should call where with matching ability conditions" do
obj = @model_class.create :foo => {:bar => 1} obj = @model_class.create :foo => {:bar => 1}
@ability.can :read, @model_class, :foo => {:bar => 1} @ability.can :read, @model_class, :foo => {:bar => 1}
@model_class.accessible_by(@ability, :read).entries.first.should == obj @model_class.accessible_by(@ability, :read).entries.first.should == obj
end end