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 else make_admin end end private def make_active_admin run "rails g active_admin:install --skip-users" 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 protected def authenticate authenticate_or_request_with_http_basic do |username, password| username == "admin" && password == "password" end 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.before_filter :authenticate config.authentication_method = false config.current_user_method = false config.allow_comments = false 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