class HscaffoldGenerator < Rails::Generator::NamedBase default_options :skip_timestamps => false, :skip_migration => false, :force_plural => false attr_reader :controller_name, :controller_class_path, :controller_file_path, :controller_class_nesting, :controller_class_nesting_depth, :controller_class_name, :controller_underscore_name, :controller_singular_name, :controller_plural_name alias_method :controller_file_name, :controller_underscore_name alias_method :controller_table_name, :controller_plural_name def initialize(runtime_args, runtime_options = {}) super if @name == @name.pluralize && !options[:force_plural] logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural." @name = @name.singularize assign_names!(@name) end @controller_name = @name.pluralize base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name) @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name) @controller_singular_name=base_name.singularize if @controller_class_nesting.empty? @controller_class_name = @controller_class_name_without_nesting else @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}" end end def manifest record do |m| # Check for class naming collisions. m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper") m.class_collisions(class_name) # Controller, helper, views, test and stylesheets directories. m.directory(File.join('app/controllers', controller_class_path)) m.directory(File.join('app/views', controller_class_path, controller_file_name)) for action in scaffold_views m.template( "view_#{action}.html.erb", File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb") ) end # Layout and stylesheet. m.template( 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") ) m.route_resources controller_file_name m.dependency 'hmodel', [singular_name] + @args, :collision => :skip end end protected # Override with your own usage banner. def banner "Usage: #{$0} scaffold ModelName [field:type, field:type]" end def add_options!(opt) opt.separator '' opt.separator 'Options:' opt.on("--skip-timestamps", "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v } opt.on("--skip-migration", "Don't generate a migration file for this model") { |v| options[:skip_migration] = v } opt.on("--force-plural", "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v } end def scaffold_views %w[ index show new edit _form ] end def model_name class_name.demodulize end def nested_to_class references = @args.select{|x| x =~ /references/} references.empty? ? nil : references.first.split(":")[0] end def nested_object "[@#{nested_to_class}, @#{file_name}]" if nested_to_class end def index_path prefix_path.length > 0 ? "[#{(prefix_path + [":#{plural_name}"]).join(", ")}]" : "#{plural_name}_url" end def prefix_path controller_class_nesting.split("::").map(&:underscore).map{|x| ":"+x} + (nested_to_class ? ["@#{nested_to_class}"] : []) end def object_path(object) prefix_path.length > 0 ? "[#{(prefix_path + ["#{object}"]).join(", ")}]" : "#{object}" end def edit_path(object) prefix_path.length > 0 ? "[#{([":edit"] + prefix_path + ["#{object}"]).join(", ")}]" : "edit_#{file_name}_url(#{object})" end def new_path prefix_path.length > 0 ? "[#{([":new"] + prefix_path + [":#{file_name}"]).join(", ")}]" : "new_#{file_name}_url" end end