class BeautifulCancancanGenerator

encoding : utf-8

Public Instance Methods

install_cancancan() click to toggle source

argument :model, :type => :string, :desc => “Name of model (ex: user)”

# File lib/generators/beautiful_cancancan_generator.rb, line 10
def install_cancancan
  model = "user"

  gem("cancancan", "3.2.1")

  Bundler.with_unbundled_env do
    run "bundle install"
  end

  # Because generators doesn't work !
  copy_file("app/models/ability.rb")

  # Why that doesn't work... boring...
  #puts rails_command("generate cancan:ability", capture: true)
  # Why that doesn't work too... boring...
  #generate("cancan:ability")

  # current_user method need for CanCan
  current_user_method = ""
  if model != "user" then
    current_user_method = "
def current_user
  current_#{model}
end"
  end

  # Exception for AccessDenied
  inject_into_file("app/controllers/application_controller.rb", "
rescue_from CanCan::AccessDenied do |exception|
  respond_to do |format|
    format.json { head :forbidden, content_type: 'text/html' }
    format.html { redirect_to root_url, :alert => exception.message }
    format.js   { head :forbidden, content_type: 'text/html' }
  end
end
#{current_user_method}
", :after => "class ApplicationController < ActionController::Base\n")

  # Access controlled by CanCanCan (in beautiful_scaffold)
  inject_into_file("app/controllers/application_controller.rb", "#", :before => "before_action :authenticate_#{model}!, :except => [:dashboard]")
end