schneikai
3/22/2014 - 8:24 PM

Mock/Stub CanCan and Devise in test/dummy app

Mock/Stub CanCan and Devise in test/dummy app

# lib/katalog/engine.rb

module Katalog
  class Engine < ::Rails::Engine
    isolate_namespace Katalog

    config.to_prepare do
      # Katalog can use CanCan to restrict access but it is optional.
      # If CanCan is not present load this module that stubs the methods we would need.
      ApplicationController.send :include, Katalog::CanCan unless Object.const_defined?('CanCan')
    end
  end
end
# lib/katalog/cancan.rb

# Katalog can use CanCan to restrict access but it is optional.
# If CanCan is not present this module is added to the host apps application
# controller to stubs the methods we would need.

# There is a gits "cancan load_and_authorize_resource mock.rb" check that out 
# if you need "load_and_authorize_resource" too.
module Katalog
  module CanCan
    extend ActiveSupport::Concern

    module ClassMethods
      def authorize_resource(*args)
        true
      end
    end
  end
end
# test/dummy/app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Stub Devise current user method so we don't need to have Devise installed in
  # the dummy app.
  def current_user
    User.find_or_create_by_id(1)
  end
end