Class: Babylon::Generator::ControllerGenerator

Inherits:
Templater::Generator
  • Object
show all
Defined in:
lib/babylon/generator.rb

Overview

Generates a new controller, with the corresponding stanzas and routes.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



85
86
87
# File 'lib/babylon/generator.rb', line 85

def self.source_root
  File.join(File.dirname(__FILE__), '../../templates/babylon/app/controllers')
end

Instance Method Details

#add_route_for_actions_in_controller(actions, controller) ⇒ Object

This is a hack since Templater doesn’t offer any simple way to edit files right now…



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/babylon/generator.rb', line 99

def add_route_for_actions_in_controller(actions, controller)
  sentinel = "Babylon.router.draw do"
  router_path = "config/routes.rb"
  actions.each do |action|
    to_inject = "xpath(\"#{action[2]}\").to(:controller => \"#{controller}\", :action => \"#{action[0]}\").priority(#{action[1]})"
    if File.exist?(router_path)
      content = File.read(router_path).gsub(/(#{Regexp.escape(sentinel)})/mi){|match| "#{match}\n\t#{to_inject}"}
      File.open(router_path, 'wb') { |file| file.write(content) }
    end
  end
end

#controller_actionsObject



89
90
91
# File 'lib/babylon/generator.rb', line 89

def controller_actions
  @controller_actions ||= actions_arg.map { |a| a.split(":") }
end

#controller_class_nameObject



93
94
95
# File 'lib/babylon/generator.rb', line 93

def controller_class_name
  "#{controller_name.capitalize}Controller"
end