Fix bug with MongoidAdditions throwing a NameError when Mongoid is not defined by always checking if Mongoid is defined before referencing Mongoid-related constants
Also add spec for this bug
This commit is contained in:
		
							parent
							
								
									ebb8e1bf8b
								
							
						
					
					
						commit
						e14e1edec2
					
				@ -1,10 +1,9 @@
 | 
				
			|||||||
module CanCan
 | 
					module CanCan
 | 
				
			||||||
 | 
					 | 
				
			||||||
  module Ability
 | 
					  module Ability
 | 
				
			||||||
    # 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 :query_without_mongoid_support, :query
 | 
					    alias_method :query_without_mongoid_support, :query
 | 
				
			||||||
    def query(action, subject)
 | 
					    def query(action, subject)
 | 
				
			||||||
      if Object.const_defined?(:Mongoid) && subject <= CanCan::MongoidAdditions
 | 
					      if defined?(::Mongoid) && subject <= CanCan::MongoidAdditions
 | 
				
			||||||
        query_with_mongoid_support(action, subject)
 | 
					        query_with_mongoid_support(action, subject)
 | 
				
			||||||
      else
 | 
					      else
 | 
				
			||||||
        query_without_mongoid_support(action, subject)
 | 
					        query_without_mongoid_support(action, subject)
 | 
				
			||||||
@ -47,7 +46,7 @@ module CanCan
 | 
				
			|||||||
  #   => 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 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
 | 
				
			||||||
@ -98,7 +97,7 @@ 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
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
 | 
					require 'mongoid' # require mongoid first so MongoidAdditions are loaded
 | 
				
			||||||
require "spec_helper"
 | 
					require "spec_helper"
 | 
				
			||||||
require 'mongoid'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MongoidCategory
 | 
					class MongoidCategory
 | 
				
			||||||
  include Mongoid::Document
 | 
					  include Mongoid::Document
 | 
				
			||||||
@ -38,6 +38,29 @@ Mongoid.configure do |config|
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe CanCan::MongoidAdditions do
 | 
					describe CanCan::MongoidAdditions do
 | 
				
			||||||
 | 
					  context "Mongoid not defined" do
 | 
				
			||||||
 | 
					    before(:all) do
 | 
				
			||||||
 | 
					      @mongoid_class = Object.send(:remove_const, :Mongoid)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    after(:all) do
 | 
				
			||||||
 | 
					      Object.const_set(:Mongoid, @mongoid_class)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    it "should not raise an error for ActiveRecord models" do
 | 
				
			||||||
 | 
					      @model_class = Class.new(Project)
 | 
				
			||||||
 | 
					      stub(@model_class).scoped { :scoped_stub }
 | 
				
			||||||
 | 
					      @model_class.send(:include, CanCan::ActiveRecordAdditions)
 | 
				
			||||||
 | 
					      @ability = Object.new
 | 
				
			||||||
 | 
					      @ability.extend(CanCan::Ability)
 | 
				
			||||||
 | 
					          
 | 
				
			||||||
 | 
					      @ability.can :read, @model_class
 | 
				
			||||||
 | 
					      lambda {    
 | 
				
			||||||
 | 
					        @ability.can? :read, @model_class.new 
 | 
				
			||||||
 | 
					      }.should_not raise_error
 | 
				
			||||||
 | 
					    end    
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  context "Mongoid defined" do
 | 
				
			||||||
    before(:each) do
 | 
					    before(:each) do
 | 
				
			||||||
      @model_class = MongoidProject
 | 
					      @model_class = MongoidProject
 | 
				
			||||||
      @ability = Object.new
 | 
					      @ability = Object.new
 | 
				
			||||||
@ -163,4 +186,5 @@ describe CanCan::MongoidAdditions do
 | 
				
			|||||||
        @model_class.accessible_by(@ability)
 | 
					        @model_class.accessible_by(@ability)
 | 
				
			||||||
      }.should raise_error(CanCan::Error)
 | 
					      }.should raise_error(CanCan::Error)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user