class ControllerGenerator < Rails::Generators::NamedBase desc "Adds method 'control_me' to a Controller" def self.source_root @source_root ||= File.expand_path("../templates", __FILE__) end def add_helper_method if File.exist?(controller_file_name) inject_into_file(controller_file_name, controller_method_code, :after => after_txt) if after_txt else say "#{controller_file_name} does not exist. Please create it first before you can add a controller method to it!", :red end end protected def after_txt "#{class_name}Controller" end def controller_file_name File.join(Rails.root, "app/controllers/#{file_name}_controller.rb") end def controller_method_code %Q{ def control_me "Control me please!" end } end end