public
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack.git
Click here to lend your support to: mack and make a donation at www.pledgie.com !
ControllerGenerator now adds actions to the routes file. [#25 state:resolved]
markbates (author)
Tue Aug 05 09:01:23 -0700 2008
commit  9502e60855e78a9c9319e45ce7a75162a92d7cb1
tree    16aaf8c46a35ba6f3ac3b1cf6f869a219964c10f
parent  32524881c1f10c595d6d918a618284eba6a63969
...
16
17
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
20
21
22
 
23
24
25
...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
 
54
55
56
57
0
@@ -16,10 +16,42 @@ class ControllerGenerator < Genosaurus
0
   end
0
   
0
   def after_generate # :nodoc:
0
+    add_actions
0
+    update_routes_file
0
+    ControllerHelperGenerator.run(@options)
0
+  end
0
+  
0
+  private
0
+  def update_routes_file # :nodoc:
0
+    unless @actions.empty?
0
+      routes = File.join(Mack.root, "config", "routes.rb")
0
+      rf = File.open(routes).read
0
+      unless rf.match(".resource :#{@name_plural}")
0
+        puts "Updating routes.rb"
0
+        nrf = ""
0
+        rf.each do |line|
0
+          if line.match("Mack::Routes.build")
0
+            x = line.match(/\|(.+)\|/).captures
0
+            line << "\n  # Added by rake generate:controller name=#{param(:name)} actions=#{param(:actions)}\n"
0
+            line << "\n  r.with_options(:controller => :#{@name_plural}) do |map|\n"
0
+            @actions.each do |action|
0
+              line << "\n    map.#{@name_plural}_#{action}_url \"/#{@name_plural}#{action == "index" ? "" : "/#{action}"}\", :action => :#{action}"
0
+            end
0
+            line << "\n  end # #{@name_plural}\n"
0
+          end
0
+          nrf << line
0
+        end
0
+        File.open(routes, "w") do |f|
0
+          f.puts nrf
0
+        end
0
+      end
0
+    end
0
+  end
0
+  
0
+  def add_actions
0
     @actions.each do |action|
0
       template(action_template(action), File.join("app", "views", @name_plural, "#{action}.html.erb"))
0
-    end
0
-    ControllerHelperGenerator.run(@options)
0
+    end    
0
   end
0
   
0
   def action_template(action) # :nodoc:
...
12
13
14
 
 
15
16
17
18
 
 
 
19
20
21
...
36
37
38
 
 
 
 
 
 
 
 
 
39
40
41
...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
0
@@ -12,10 +12,15 @@ describe ControllerGenerator do
0
   
0
   before(:each) do
0
     common_cleanup
0
+    @route_file_loc = File.join(Mack.root, "config", "routes.rb")
0
+    @original_route_file = File.read(@route_file_loc)
0
   end
0
   
0
   after(:each) do
0
     common_cleanup
0
+    File.open(@route_file_loc, "w") do |f|
0
+      f.puts @original_route_file
0
+    end
0
   end
0
   
0
   it "should require a name parameter" do
0
@@ -36,6 +41,15 @@ describe ControllerGenerator do
0
     File.read(show).should match(/<p>You can find me in app\/views\/zoos\/show.html.erb<\/p>/)
0
   end
0
   
0
+  it "should update the routes file if there are actions" do
0
+    ControllerGenerator.run("name" => "zoo", "actions" => "index,show")
0
+    File.read(@route_file_loc).should match(/  # Added by rake generate:controller name=zoo actions=index,show/)
0
+    File.read(@route_file_loc).should match(/  r.with_options\(:controller => :zoos\) do \|map\|/)
0
+    File.read(@route_file_loc).should match(/    map.zoos_index_url "\/zoos", :action => :index/)
0
+    File.read(@route_file_loc).should match(/    map.zoos_show_url "\/zoos\/show", :action => :show/)
0
+    File.read(@route_file_loc).should match(/  end # zoos/)
0
+  end
0
+  
0
   it "should generate a controller" do
0
     file = File.join(Mack.root, "app", "controllers", "zoos_controller.rb")
0
     File.should_not be_exists(file)

Comments