class SchofieldControllerGenerator < Rails::Generator::NamedBase def initialize(runtime_args, runtime_options = {}) super usage if runtime_args.empty? @args = runtime_args.dup base_name = @args.shift assign_names!(base_name) end def manifest record do |m| template_directory = options[:parent].nil? ? 'unnested' : 'nested' sco_class = class_name.gsub('Admin::', '').singularize sco_humanized_uc = sco_class.underscore.humanize sco_underscored = @singular_name.singularize sco_underscored_plural = sco_underscored.pluralize sco_titleized_plural = sco_underscored.pluralize.titleize sco_humanized = sco_humanized_uc.downcase sco_parent_class = options[:parent] ? options[:parent].classify : '' sco_parent_underscored = sco_parent_class.underscore.downcase sco_parent_underscored_plural = sco_parent_underscored.pluralize sco_parent_titleized_plural = sco_parent_underscored.titleize.pluralize assigns = { :sco_underscored => sco_underscored, :sco_underscored_plural => sco_underscored_plural, :sco_class => sco_class, :sco_humanized_uc => sco_humanized_uc, :sco_parent_underscored => sco_parent_underscored, :sco_parent_underscored_plural => sco_parent_underscored_plural, :sco_parent_titleized_plural => sco_parent_titleized_plural, :sco_parent_class => sco_parent_class, :sco_titleized_plural => sco_titleized_plural, :sco_humanized => sco_humanized, :non_restful_actions => non_restful_actions } # Check for class naming collisions. m.class_collisions class_path, "#{class_name}Controller", "#{class_name}Helper" # Controller, helper, views, and spec directories. m.directory File.join('app/controllers', class_path) m.directory File.join('app/helpers', class_path) m.directory File.join('app/views', class_path, file_name) m.directory File.join('spec/controllers', class_path) m.directory File.join('spec/helpers', class_path) m.directory File.join('spec/views', class_path, file_name) # Controller spec, class, and helper. m.template 'controller_spec.rb', File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb") m.template 'helper_spec.rb', File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb") m.template "#{template_directory}/controller.rb", File.join('app/controllers', class_path, "#{file_name}_controller.rb"), :assigns => assigns m.template 'controller:helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb") # Spec and view template for each action. actions.each do |action| m.template 'view_spec.rb', File.join('spec/views', class_path, file_name, "#{action}.haml_spec.rb"), :assigns => { :action => action, :model => file_name } m.template "#{template_directory}/#{action}.rb", File.join('app/views', class_path, file_name, "#{action}.haml"), :assigns => assigns if actions_with_view.include?(action) end end end def actions_with_view non_restful_actions + %w( index show new edit ) end def restful_actions %w( index show new create edit update destroy sort ) end def non_restful_actions restful_actions - %w( index show new create edit update destroy sort ) end protected def add_options!(opt) opt.separator '' opt.separator 'Options:' opt.on('--parent PARENT', 'Specify the parent model') { |v| options[:parent] = v if v.present? } end def banner "Usage: #{$0} schofield_controller ControllerName [--parent PARENT]" end end