module JsonVoorhees class SetupAppGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) #argument :user_class, :type => :string 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 engine for the user class" class_option :test, :type => :boolean, :default => false, :description => "Set true if running tests to skip things like bundle update and git" class_option :admin, :type => :boolean, :default => true, :description => "Create an admin page" def sprint create_file_structure dev_test_gems other_gems run_bundle rspec api_controller include_middleware if options.admin? make_admin end if options.user? people end routes make_controllers #seed db? #fancier admin? run_git end private def make_admin if options.user? #copy admin page with user engine copy_file "views/admin_with_user", "app/views/main/admin.html.erb" else #copy admin page without user engine copy_file "views/admin_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" 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 end def include_middleware inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do <<-'RUBY' #So Rails-Api doesn't remove all of the middleware config.api_only = false RUBY end end def routes route "root to: \"app_index#app\"" route "get \'admin\' => \'main#admin\', as: \"admin\" #admin_path" end def make_controllers generate "controller", "app_index app" run "rm -f app/controllers/app_index_controller.rb" copy_file "app_index_controller.rb", "app/controllers/app_index_controller.rb" generate "controller", "main admin" run "rm -f spec/controllers/main_controller_spec.rb" #Copy the views over copy_file "views/app", "app/views/app_index/app.html.erb" copy_file "views/app_index", "app/views/layouts/app_index.html.erb" end def run_db_migrations run "rake db:migrate RAILS_ENV=development" run "rake db:migrate RAILS_ENV=test" run "rake db:migrate RAILS_ENV=production" end def run_git if !options.test? git :init git add: "." run "git commit -m \"Initial commit\"" end end def people run "rails plugin new engines/people --mountable" inside('engines/people') do #This needs to run the generator for setting up engines run "rails g json_voorhees:setup_engine people" add_generator #Now it needs to create the user class run "rails g scaffold user username:string:uniq email:string:uniq password_digest:string" #Add this to the routes and gemfile end #Need to add the engine to the main_apps gemfile route "mount People::Engine, at: \'/\'" if options.arcadex? copy_user_files_over route "mount Arcadex::Engine, at: \'/\'" end insert_people_engine run_bundle run "rake railties:install:migrations" run_db_migrations end def add_generator inject_into_file "people.gemspec", after: "s.test_files = Dir[\"test/**/*\"]\n" do <<-'RUBY' s.add_development_dependency "json_voorhees" RUBY end end def insert_people_engine inject_into_file 'Gemfile', after: "source \'https://rubygems.org\'\n" do <<-'RUBY' gem 'people', :path => "engines/people" RUBY end end def copy_user_files_over template "user/user_authorizations.rb", "gems/authorization/lib/authorization/v1/people/user.rb" run "rm -f gems/authorization/lib/authorization.rb" template "user/include_authorization.rb", "gems/authorization/lib/authorization.rb" template "user/user_controller.rb", "engines/people/app/controllers/people/api/v1/users_controller.rb" run "rm -f engines/people/app/models/people/user.rb" template "user/user_model.rb", "engines/people/app/models/people/user.rb" run "rm -f engines/people/config/routes.rb" template "user/user_routes.rb", "engines/people/config/routes.rb" template "user/user_serializer.rb", "engines/people/app/serializers/people/user_serializer.rb" template "user/specs/factory_girl.rb", "spec/factories/people_user_factory.rb" template "user/specs/model_specs.rb", "spec/engines/people/api/v1/models/user_spec.rb" template "user/specs/request_specs.rb", "spec/engines/people/api/v1/requests/user_spec.rb" template "user/specs/route_specs.rb", "spec/engines/people/api/v1/routing/user_spec.rb" end def api_controller if options.arcadex? copy_file "api_controller_with_arcadex.rb", "app/controllers/api/v1/api_controller.rb" else copy_file "api_controller_no_arcadex.rb", "app/controllers/api/v1/api_controller.rb" end end def rspec generate "rspec:install" run "rm -f .rspec" copy_file "hidden_rspec.rb", ".rspec" copy_file "rspec_factory_girl.rb", "spec/support/factory_girl.rb" inject_into_file 'spec/rails_helper.rb', after: "RSpec.configure do |config|\n" do <<-'RUBY' config.include ::Requests::JsonHelpers, :type => :request config.include ::Requests::JsonHelpers, :type => :controller RUBY end copy_file "json_helpers.rb", "spec/support/request_helpers.rb" end def run_bundle if !options.test? run "bundle update" run "bundle install" end end def other_gems gem 'authorization', :path => "gems/authorization" # This ties in nicely with Ember, 0.8.0 is the stable version gem "active_model_serializers", "~> 0.8.0" # This is the api of choice now gem "rails-api" gem 'kaminari' gem 'bcrypt', '~> 3.1.7' gem 'type_cartographer' gem 'devise', "~> 3.2.4" if options.arcadex? gem 'arcadex', '~> 1.0.4' end end def dev_test_gems gem_group :development, :test do #gem 'sqlite3', '~> 1.3.9' # dev & test database gem 'figaro', '~> 0.7.0' # env variables gem 'annotate', ">=2.6.0" gem "better_errors", '~> 1.1.0' gem "binding_of_caller", "~> 0.7.1" # needed by better_errors or variable inspection gem 'rspec-rails', '~> 3.0.0' gem 'byebug', '~> 3.2.0' gem "factory_girl_rails", "~> 4.0" gem "database_cleaner", '~> 1.3.0' end end def create_file_structure run "rm -rf test" run "rm -f README.rdoc" run "echo '# Describe your application here' > README.md" run "mkdir engines && touch engines/.gitkeep" run "mkdir gems && touch gems/.gitkeep" run "mkdir notes && touch notes/.gitkeep" run "rails plugin new gems/authorization" end end end