module JsonVoorhees class MassiveScaffoldGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) argument :module_name, :type => :string argument :resource_name, :type => :string argument :api_version, :type => :string, :default => "1" argument :attributes, type: :array, default: [], banner: "field:type field:type" class_option :both, :type => :boolean, :default => true, :description => "Run the rails scaffold in addition to this one" class_option :model, :type => :boolean, :default => true, :description => "Create model stub tests" class_option :request, :type => :boolean, :default => true, :description => "Create request stub tests" class_option :routing, :type => :boolean, :default => true, :description => "Create routing stub tests" class_option :arcadex, :type => :boolean, :default => true, :description => "Send requests with an arcadex header" class_option :active_admin, :type => :boolean, :default => true, :description => "Generate the admin resource" def sprint inside "engines/#{module_name.underscore}" do if options.both? # We should skip making the migration if this is not version 1 if api_version != "1" run "rails g scaffold #{resource_name} #{attributes.join(" ")} --no-migration " else run "rails g scaffold #{resource_name} #{attributes.join(" ")}" end end if options.arcadex? run "rails g json_voorhees:engine_scaffold #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")}" else run "rails g json_voorhees:engine_scaffold #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")} --skip-arcadex" end correct_routes end if options.arcadex? run "rails g json_voorhees:app_scaffold #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")}" else run "rails g json_voorhees:app_scaffold #{module_name} #{resource_name} #{api_version} #{attributes.join(" ")} --skip-arcadex" end if options.active_admin? #run "rails generate active_admin:resource #{module_name.underscore.downcase}::#{resource_name.singularize.underscore.downcase}" make_custom_active_admin end end private def correct_routes gsub_file 'config/routes.rb', "resources :#{resource_plural}", "#resources :#{resource_plural}" gsub_file 'config/routes.rb', "scope '1' do\n", "scope '1' do\nresources :#{resource_plural}, controller: \'api/v1/#{resource_plural}\'\n" end def make_custom_active_admin template "active_admin_register.rb.erb", "app/admin/#{module_name.underscore}_#{resource_singular}.rb" end def params_list params = [] attributes.each do |pair| elem = pair.split(/:/)[0] field = ":#{elem}" params << field end return params.join(",") end def resource_singular resource_name.underscore.singularize end def resource_plural resource_name.underscore.pluralize end def resource_camel resource_name.camelize.singularize end def menu_name resource_name.camelize.pluralize end def module_camel module_name.camelize end def module_snake module_name.underscore.downcase end end end