Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`rails g scaffold` or `rails g scaffold_controller` never invokes my override of Erb::Generators::ScaffoldGenerator. I can workaround by setting `config.generators.template_engine :all` and moving the override there. That feels wrong.
config.generators do |g|
g.template_engine :all
g.fallbacks[:all] = :erb
end
# lib/generators/erb/scaffold/scaffold_generator.rb
# See http://stackoverflow.com/questions/4696954/how-to-have-the-scaffold-to-generate-another-partial-view-template-file
# require 'rails/generators/erb'
# require 'rails/generators/resource_helpers'
require 'rails/generators/erb/scaffold/scaffold_generator' # see http://stackoverflow.com/questions/16320882/rails-generate-both-html-and-js-views-with-scaffold
module Erb # :nodoc:
module Generators # :nodoc:
class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator # :nodoc:
puts "Source: #{__FILE__}\n"
# include Rails::Generators::ResourceHelpers
source_root File.expand_path(File.join('..', 'templates'), __FILE__)
argument :attributes, type: :array, default: [], banner: "field:type field:type"
def create_root_folder
empty_directory File.join("app/views", controller_file_path)
end
def copy_view_files
available_views.each do |view|
filename = filename_with_extensions(view)
template filename, File.join("app/views", controller_file_path, filename)
end
end
protected
def available_views
%w(index edit show new _form foo)
end
end
end
end
# lib/generators/scaffold_controller/scaffold_controller_generator.rb
require 'rails/generators/resource_helpers'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Rails
module Generators
class ScaffoldControllerGenerator < NamedBase # :nodoc:
puts "Source: #{__FILE__}"
include ResourceHelpers
check_class_collision suffix: "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"
def create_controller_files
template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end
hook_for :template_engine, :test_framework, as: :scaffold do |invoked|
# debugger
# invoke invoked # loads Erb::Generators::ScaffoldGenerator
invoke 'all:scaffold' # loads my All::Generators::ScaffoldGenerator
end
# Invoke the helper using the controller name (pluralized)
hook_for :helper, as: :scaffold do |invoked|
invoke invoked, [ controller_name ]
end
end
end
end
# lib/generators/scaffold/scaffold_generator.rb
# See https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb
# Called on `rails g scaffold`
require 'rails/generators/rails/resource/resource_generator'
module Rails
module Generators
class ScaffoldGenerator < ResourceGenerator # :nodoc:
puts "HIT #{__FILE__}"
remove_hook_for :resource_controller
remove_class_option :actions
class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets"
class_option :stylesheet_engine, desc: "Engine for Stylesheets"
class_option :assets, type: :boolean
class_option :resource_route, type: :boolean
def handle_skip
@options = @options.merge(stylesheets: false) unless options[:assets]
@options = @options.merge(stylesheet_engine: false) unless options[:stylesheets]
end
hook_for :scaffold_controller, required: true do |invoked|
# invoke invoked
invoke :scaffold_controller
end
hook_for :assets do |assets|
invoke assets, [controller_name]
end
hook_for :stylesheet_engine do |stylesheet_engine|
if behavior == :invoke
invoke stylesheet_engine, [controller_name]
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment