require 'rubygems/specification' require 'rails/generators/named_base' require 'rails/generators/resource_helpers' module Admin module Generators class ScaffoldControllerGenerator < Rails::Generators::NamedBase include Rails::Generators::ResourceHelpers source_root File.expand_path('templates', __dir__) class_option :prefix_name, banner: "admin", type: :string, default: "admin", desc: "Define the prefix of controller" class_option :parent_controller, banner: "admin", type: :string, default: "application", desc: "Define the parent controller" class_option :orm, banner: "NAME", type: :string, required: true, desc: "ORM to generate the controller for" argument :attributes, type: :array, default: [], banner: "field:type field:type" hook_for :resource_route, in: :rails do |resource_route| invoke resource_route, [prefixed_class_name] end def create_controller_files # I think there should be a better way to detect if jbuilder is in use # If you know it, please let me know template "controllers/controller.rb.erb", File.join('app/controllers', prefix, class_path, "#{controller_file_name}_controller.rb") # create_file "app/controllers/a.rb", "# Add initialization content here" end def copy_view_files available_views.each do |view| template_path = "views/erb/#{view}.html.erb.erb" template template_path, File.join("app/views", prefix, controller_file_path, "#{view}.html.erb") end end def create_test_files template "tests/test_unit/functional_test.rb.erb", File.join("test/controllers", prefix, controller_class_path, "#{controller_file_name}_controller_test.rb") end def attributes_hash return if attributes_names.empty? attributes_names.map do |name| if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?) "#{name}: 'secret'" else "#{name}: @#{singular_table_name}.#{name}" end end.sort.join(', ') end def prefix options[:prefix_name] end def prefixed_controller_class_name "#{prefix.camelcase}::#{controller_class_name}" end def parent_controller_class_name options[:parent_controller].camelcase end def prefixed_class_name "#{prefix.capitalize}::#{class_name}" end def prefixed_route_url "/#{prefix}#{route_url}" end def prefixed_plain_model_url "#{prefix}_#{singular_table_name}" end def prefixed_index_helper "#{prefix}_#{index_helper}" end def available_views %w(index edit show new _form) end def plural_table_name_camelcase plural_table_name.camelcase end def singular_table_name_camelcase singular_table_name.camelcase end end end end