Skip to content

Instantly share code, notes, and snippets.

@johndel
Created April 2, 2013 23:55
Show Gist options
  • Save johndel/5297272 to your computer and use it in GitHub Desktop.
Save johndel/5297272 to your computer and use it in GitHub Desktop.
Generator for initializing gsadmin setup
module Gsadmin
module Generators
class InitializerGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
desc "Gsadmin installation generator."
def welcome
say "Thank you for using gsadmin, if you have any questions, my email is [email protected]", :cyan
end
def check_if_rails_project
unless File.exists?("Gemfile")
say "Missing Gemfile! Is this a rails project? If yes, something went really wrong, create your Gemfile and retry.", :red
unless yes?("It may not work properly. Do you want to continue?", :red)
abort("Exit...")
end
end
unless File.exists?(Rails.root.join("config/routes.rb"))
say "Missing routes.rb! Is this a rails project? If yes, something went really wrong, create your routes.rb and retry.", :red
unless yes?("It may not work properly. Do you want to continue?", :red)
abort("Exit...")
end
end
end
def install_devise
say("gsadmin needs devise.", :yellow)
if yes?("Do you want to install devise right now? (click yes if you don't have installed already)", :yellow)
append_file "Gemfile", "\n", :force => true
gem "devise"
generate "devise:install"
end
end
def install_devise_model
if yes?("Do you have a devise model already?", :yellow)
@devise_model = ask("What is his model name? [default: admin]", :yellow)
@devise_model = "admin" if @devise_model.empty?
say("Ok, make sure that you have this in your routes.rb, before the mounted engine route:", :yellow)
say(" devise_for :#{@devise_model.pluralize}, path_names: {sign_in: 'login', sign_out: 'logout'}", :cyan)
else
say("It's ok, I will generate one for you.", :yellow)
@devise_model = ask("What would you like the user model to be called? [default: admin]", :yellow)
@devise_model = "admin" if @devise_model.empty?
say "Now setting up devise with model name '#{@devise_model}':"
generate "devise", @devise_model
gsub_file "config/routes.rb", " devise_for \'\/.+\'", ""
insert_into_file "config/routes.rb", before: "end" do
" devise_for :#{@devise_model.pluralize}, path_names: {sign_in: 'login', sign_out: 'logout'}\n"
end
end
end
def add_engine_route
if yes?("Is this the first time you are installing gsadmin gem?", :yellow)
say "I'll add the appropriate routes", :yellow
@namespace_route = ask("Where do you want the engine to be installed? (for example under /admin/:yourapp) [default: admin]", :yellow)
@namespace_route = "admin" if @namespace_route.empty?
insert_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
"authenticate :#{@devise_model} do\n"
" mount Gsadmin::Engine => '/#{@namespace_route}', :as => '#{@namespace_route}'\n"
"end\n"
end
say "You can change the routes of the engine any time you want from the routes.rb file.", :green
say "If you need a reminder, just check the gem's wiki on github.", :green
end
end
def create_initializer
template "initializer.erb", "config/initializers/gsadmin.rb"
end
def create_migrations
#run("rake gsadmin:install:migrations")
end
def goodbye
say("Run rake db:create and db:migrate for the database", :yellow)
say "Thank you for installing gsadmin! If you have questions, send me an email at [email protected].", :cyan
end
end # End of Class
end # End of Module
end # End of Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment