module HorsePower class MakeAdminGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) def sprint make_active_admin make_dashboard end private def make_dashboard run "rm -f app/admin/dashboard.rb" copy_file "dashboard.rb.erb", "app/admin/dashboard.rb" end def make_active_admin run "rails g active_admin:install --skip-users" template "defcon_admin_register.rb.erb", "app/admin/defcon_admin_user.rb" inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery with: :exception\n" do <<-'RUBY' protected def authenticate_admin_user! if ::Defcon.authenticate_admin_user!(session) return true else redirect_to defcon.defcon_login_path, alert: "Login!" return false end end def current_admin_user return ::Defcon.current_admin_user(session) end RUBY end find_and_replace_initializer end def find_and_replace_initializer gsub_file 'config/initializers/active_admin.rb', 'config.authentication_method = :authenticate_admin_user!', '#config.authentication_method = :authenticate_admin_user!' gsub_file 'config/initializers/active_admin.rb', 'config.current_user_method = :current_admin_user', '#config.current_user_method = :current_admin_user' gsub_file 'config/initializers/active_admin.rb', 'config.logout_link_path = :destroy_admin_user_session_path', '#config.logout_link_path = :destroy_admin_user_session_path' inject_into_file 'config/initializers/active_admin.rb', after: "ActiveAdmin.setup do |config|\n" do <<-'RUBY' # http auth for admin area config.authentication_method = :authenticate_admin_user! config.current_user_method = :current_admin_user config.logout_link_path = "/sessions/destroy" config.logout_link_method = :post config.comments = false config.site_title_link = "/" RUBY end end end end