module JsonVoorhees class SetupEngineGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) argument :module_name, :type => :string def sprint generate_layout create_api_controller add_gems setup_routes setup_application_controller run_bundle end private def setup_application_controller run "rm -f app/controllers/#{module_name.underscore}/application_controller.rb" template "application_controller.rb.erb", "app/controllers/#{module_name.underscore}/application_controller.rb" end def run_bundle run "bundle update" run "bundle install" end def setup_routes inject_into_file "config/routes.rb", after: "#{module_name.camelize}::Engine.routes.draw do\n" do <<-'RUBY' #root to: "controller_name#index" scope 'admin' do end scope 'api' do scope '1' do #/api/1/controller_name #resources :controller_name, controller: 'api/v1/controller_name' #resources :controller_name2, controller: 'api/v1/controller_name2' do #collection do #/api/1/controller_name2/action #post 'action', to: "api/v1/controller_name2#action" #end #end end end #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#- RUBY end end def add_gems inject_into_file "#{module_name.downcase}.gemspec", after: "s.test_files = Dir[\"test/**/*\"]\n" do <<-'RUBY' s.add_dependency "rails-api" s.add_dependency "type_cartographer" s.add_dependency "arcadex" s.add_dependency "active_model_serializers", "~> 0.8.0" s.add_dependency "bcrypt", "~> 3.1.7" s.add_development_dependency "json_voorhees" s.add_development_dependency "annotate", ">=2.6.0" RUBY end end def create_api_controller template "api_controller.rb.erb", "app/controllers/#{module_name.underscore}/api/v1/application_controller.rb" end def generate_layout run "mkdir app/views/layouts/#{module_name.downcase}/default/" run "mv app/views/layouts/#{module_name.downcase}/application.html.erb app/views/layouts/#{module_name.downcase}/default/application.html.erb" run "mkdir app/serializers" run "mkdir app/serializers/#{module_name.downcase} && touch app/serializers/#{module_name.downcase}/.gitkeep" run "rm -f README.rdoc && echo '# Describe your engine here' > README.md" end end end