module JsonVoorhees class AppMakeAdminGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) class_option :arcadex, :type => :boolean, :default => true, :description => "Include arcadex gem and authentication in api_controller" class_option :user, :type => :boolean, :default => true, :description => "Create an admin page with user class" class_option :active_admin, :type => :boolean, :default => true, :description => "Setup the active_admin gem, the gem needs to be already added" def sprint if options.active_admin? make_active_admin make_dashboard else make_admin end 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.allow_comments = false config.site_title_link = "/" RUBY end end def make_admin if options.user? && options.arcadex? #copy admin page with user engine copy_file "views/admin_home_with_user", "app/views/main/admin.html.erb" else #copy admin page without user engine copy_file "views/admin_home_no_user", "app/views/main/admin.html.erb" end run "rm -f app/views/layouts/application.html.erb" copy_file "views/application", "app/views/layouts/application.html.erb" template "views/_header.html.erb", "app/views/layouts/_header.html.erb" copy_file "views/_footer.html.erb", "app/views/layouts/_footer.html.erb" inject_into_file 'app/controllers/application_controller.rb', after: "protect_from_forgery with: :exception\n" do <<-'RUBY' #This needs to be put inside a config file. but this is good for now #This only requires the password for the admin section of the website http_basic_authenticate_with name: "admin", password: "password" RUBY end run "rm -f app/assets/stylesheets/application.css" copy_file "views/application.css.scss", "app/assets/stylesheets/application.css.scss" append_file "app/assets/javascripts/application.js", "//= require bootstrap-sprockets" end end end