Skip to content
Browse files

cleanup whitespace

  • Loading branch information...
1 parent bbb02f7 commit 8628aa003831ffdaa579ae8f22ce6335047008dc @ryanb committed Dec 30, 2010
Showing with 40 additions and 40 deletions.
  1. +25 −25 lib/cancan/model_adapters/mongoid_adapter.rb
  2. +15 −15 spec/cancan/model_adapters/mongoid_adapter_spec.rb
View
50 lib/cancan/model_adapters/mongoid_adapter.rb
@@ -9,55 +9,55 @@ def query(action, subject)
query_without_mongoid_support(action, subject)
end
end
-
+
def query_with_mongoid_support(action, subject)
MongoidQuery.new(subject, relevant_rules_for_query(action, subject))
end
end
-
+
class MongoidQuery
def initialize(sanitizer, rules)
@sanitizer = sanitizer
@rules = rules
- end
-
+ end
+
def conditions
if @rules.size == 0
false_query
else
@rules.first.instance_variable_get(:@conditions)
end
end
-
+
def false_query
# 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
# 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.
# 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:
- # {}.all?{|a| a == 5}
+ # {}.all?{|a| a == 5}
# => true
- # {}.all?{|a| a != 5}
+ # {}.all?{|a| a != 5}
# => true
class Rule
- 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 conditions.empty?
+ 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 conditions.empty?
true
else
subject.class.where(conditions).include?(subject) # just use Mongoid's where function
end
- else
+ else
matches_conditions_hash_without_mongoid_subject? subject, conditions
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?, :matches_conditions_hash_with_mongoid_subject?
end
@@ -71,40 +71,40 @@ module ClassMethods
# is usually called from a controller and passed the +current_ability+.
#
# @articles = Article.accessible_by(current_ability)
- #
+ #
# 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
# result is returned. Since this is a scope it can be combined with any
# other scopes or pagination.
- #
+ #
# An alternative action can optionally be passed as a second argument.
- #
+ #
# @articles = Article.accessible_by(current_ability, :update)
- #
+ #
# Here only the articles which the user can update are returned. This
# internally uses Ability#conditions method, see that for more information.
- def accessible_by(ability, action = :read)
- query = ability.query(action, self)
+ def accessible_by(ability, action = :read)
+ query = ability.query(action, self)
where(query.conditions)
end
end
-
+
def self.included(base)
base.extend ClassMethods
end
end
end
-# Info on monkeypatching Mongoid :
+# Info on monkeypatching Mongoid :
# http://log.mazniak.org/post/719062325/monkey-patching-activesupport-concern-and-you#footer
if defined?(::Mongoid)
module Mongoid
module Components
old_block = @_included_block
- @_included_block = Proc.new do
+ @_included_block = Proc.new do
class_eval(&old_block) if old_block
include CanCan::MongoidAdditions
end
end
- end
+ end
end
View
30 spec/cancan/model_adapters/mongoid_adapter_spec.rb
@@ -11,7 +11,7 @@ class MongoidCategory
class MongoidProject
include Mongoid::Document
include CanCan::MongoidAdditions
-
+
referenced_in :mongoid_category
class << self
@@ -42,7 +42,7 @@ def sanitize_hash(hash)
before(:all) do
@mongoid_class = Object.send(:remove_const, :Mongoid)
end
-
+
after(:all) do
Object.const_set(:Mongoid, @mongoid_class)
end
@@ -51,12 +51,12 @@ def sanitize_hash(hash)
stub(@model_class).scoped { :scoped_stub }
@ability = Object.new
@ability.extend(CanCan::Ability)
-
+
@ability.can :read, @model_class
- lambda {
- @ability.can? :read, @model_class.new
+ lambda {
+ @ability.can? :read, @model_class.new
}.should_not raise_error
- end
+ end
end
context "Mongoid defined" do
@@ -69,8 +69,8 @@ def sanitize_hash(hash)
after(:each) do
Mongoid.master.collections.select do |collection|
collection.name !~ /system/
- end.each(&:drop)
- end
+ end.each(&:drop)
+ end
it "should compare properties on mongoid documents with the conditions hash" do
model = @model_class.new
@@ -93,16 +93,16 @@ def sanitize_hash(hash)
dude = @model_class.create :title => 'Dude'
@model_class.accessible_by(@ability, :read).should == [sir]
- end
+ end
it "should return everything when the defined ability is manage all" do
@ability.can :manage, :all
sir = @model_class.create :title => 'Sir'
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]
- end
+ end
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
@@ -119,7 +119,7 @@ def sanitize_hash(hash)
describe "activates only when there are Criteria in the hash" do
it "Calls where on the model class when there are criteria" do
obj = @model_class.create :title => 'Bird'
- @conditions = {:title.nin => ["Fork", "Spoon"]}
+ @conditions = {:title.nin => ["Fork", "Spoon"]}
mock(@model_class).where(@conditions) {[obj]}
@ability.can :read, @model_class, @conditions
@ability.should be_able_to(:read, obj)
@@ -150,7 +150,7 @@ def sanitize_hash(hash)
obj2 = @model_class.create :titles => ['Palatin', 'Margrave', 'Marquis']
@ability.can?(:read, obj2).should == false
- end
+ end
it "should handle :field.exists" do
obj = @model_class.create :titles => ['Palatin', 'Margrave']
@@ -170,12 +170,12 @@ def sanitize_hash(hash)
obj2 = @model_class.create :age => 40
@ability.can?(:read, obj2).should == false
- end
+ end
end
it "should call where with matching ability conditions" do
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
end

0 comments on commit 8628aa0

Please sign in to comment.
Something went wrong with that request. Please try again.