Sha256: d55ee5326fb06179630d9cc54541453805637f2a81e1e41fae8ebcadf29aedbc

Contents?: true

Size: 1.9 KB

Versions: 8

Compression:

Stored size: 1.9 KB

Contents

# Generates a controller for Mack applications.
# 
# Example:
#   rake generate:controller name=post
class ControllerGenerator < Genosaurus
  
  require_param :name
  
  def setup # :nodoc:
    @name_singular = param(:name).singular.underscore
    @name_plural = param(:name).plural.underscore
    @name_singular_camel = @name_singular.camelcase
    @name_plural_camel = @name_plural.camelcase
    @actions = []
    @actions = param(:actions).split(",") unless param(:actions).blank?
  end
  
  def after_generate # :nodoc:
    add_actions
    update_routes_file
    ControllerHelperGenerator.run(@options)
  end
  
  private
  def update_routes_file # :nodoc:
    unless @actions.empty?
      routes = Mack::Paths.config("routes.rb")
      rf = File.open(routes).read
      unless rf.match(".resource :#{@name_plural}")
        puts "Updating routes.rb"
        nrf = ""
        rf.each do |line|
          if line.match("Mack::Routes.build")
            x = line.match(/\|(.+)\|/).captures
            line << "\n  # Added by rake generate:controller name=#{param(:name)} actions=#{param(:actions)}"
            line << "\n  r.with_options(:controller => :#{@name_plural}) do |map|"
            @actions.each do |action|
              line << "\n    map.#{@name_plural}_#{action} \"/#{@name_plural}#{action == "index" ? "" : "/#{action}"}\", :action => :#{action}"
            end
            line << "\n  end # #{@name_plural}\n"
          end
          nrf << line
        end
        File.open(routes, "w") do |f|
          f.puts nrf
        end
      end
    end
  end
  
  def add_actions
    @actions.each do |action|
      template(action_template(action), File.join("app", "views", @name_plural, "#{action}.html.erb"))
    end    
  end
  
  def action_template(action) # :nodoc:
    %{
<h1>#{@name_plural_camel}Controller##{action}</h1>
<p>You can find me in app/views/#{@name_plural}/#{action}.html.erb</p>
    }.strip
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mack-0.7.1.1 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.7.1 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.8.0.100 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.8.0.101 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.8.1 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.8.2 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.8.0 lib/mack/generators/controller_generator/controller_generator.rb
mack-0.8.0.2 lib/mack/generators/controller_generator/controller_generator.rb