Sha256: 7e5ad225635ee544f0cd1082984810f2286e11eeb1e7d4f8f389dc58f72f8a66

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

require 'sugar-high/file'
require 'sugar-high/module'
require 'cream'
require 'rails3_artifactor'
require 'logging_assist'

module Cancan
  module Generators 
    class ConfigGenerator < Rails::Generators::Base        
      desc "Configure CanCan"

      class_option :logfile, :type => :string,   :default => nil,   :desc => "Logfile location"
      class_option :gems,    :type => :boolean,  :default => false, :desc => "Add gems to gemfile?"       

      def cancan_configure
      	logger.add_logfile :logfile => logfile if logfile        
        cancan_gems if gems?
        cancan_exception_handling
      end

  	  protected

      include Rails3::Assist::BasicLogger
      extend Rails3::Assist::UseMacro

      use_helpers :controller, :app      

      def gems?
        options[:gems]        
      end

      def logfile
        options[:logfile]
      end

      def cancan_gems
        gem 'cancan'  
        gem 'cancan-rest-links'
        bundle_install 'cancan', 'cancan-permits'
      end

      def bundle_install *gems
        run "bundle install #{gems.jon(' ')}"
      end

      # CanCan access denied exception handling
      def cancan_exception_handling
        logger.debug "Rescue exists? #{rescue_exists?}"
        return if rescue_exists?        
  
        logger.debug "Insert rescue into application controller"
        insert_into_controller :application, :after => "ActionController::Base\n" do
          %{
  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = exception.message
    redirect_to root_url
  end
  }
        end
      end
       
      def rescue_exists? 
        File.read(controller_file :application) =~ /rescue_from CanCan::AccessDenied/
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cream-0.7.6 lib/generators/cancan/config/config_generator.rb
cream-0.7.5 lib/generators/cancan/config/config_generator.rb
cream-0.7.3 lib/generators/cancan/config/config_generator.rb
cream-0.7.2 lib/generators/cancan/config/config_generator.rb