1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
== Generator Commands ==
You may have noticed above that you can used one of the built-in rails migration commands `migration_template`. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.
This section describes how you you can create your own commands to add and remove a line of text from 'config/routes.rb'.
To start, add the following test method:
*vendor/plugins/yaffle/test/route_generator_test.rb*
[source, ruby]
-----------------------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
require 'rails_generator'
require 'rails_generator/scripts/generate'
require 'rails_generator/scripts/destroy'
class RouteGeneratorTest < Test::Unit::TestCase
def setup
FileUtils.mkdir_p(File.join(fake_rails_root, "config"))
end
def teardown
FileUtils.rm_r(fake_rails_root)
end
def test_generates_route
content = <<-END
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
END
File.open(routes_path, 'wb') {|f| f.write(content) }
Rails::Generator::Scripts::Generate.new.run(["yaffle_route"], :destination => fake_rails_root)
assert_match /map\.yaffles/, File.read(routes_path)
end
def test_destroys_route
content = <<-END
ActionController::Routing::Routes.draw do |map|
map.yaffles
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
END
File.open(routes_path, 'wb') {|f| f.write(content) }
Rails::Generator::Scripts::Destroy.new.run(["yaffle_route"], :destination => fake_rails_root)
assert_no_match /map\.yaffles/, File.read(routes_path)
end
private
def fake_rails_root
File.join(File.dirname(__FILE__), "rails_root")
end
def routes_path
File.join(fake_rails_root, "config", "routes.rb")
end
end
-----------------------------------------------------------
Run `rake` to watch the test fail, then make the test pass add the following:
*vendor/plugins/yaffle/lib/yaffle.rb*
[source, ruby]
-----------------------------------------------------------
require "yaffle/commands"
-----------------------------------------------------------
*vendor/plugins/yaffle/lib/yaffle/commands.rb*
[source, ruby]
-----------------------------------------------------------
require 'rails_generator'
require 'rails_generator/commands'
module Yaffle #:nodoc:
module Generator #:nodoc:
module Commands #:nodoc:
module Create
def yaffle_route
logger.route "map.yaffle"
look_for = 'ActionController::Routing::Routes.draw do |map|'
unless options[:pretend]
gsub_file('config/routes.rb', /(#{Regexp.escape(look_for)})/mi){|match| "#{match}\n map.yaffles\n"}
end
end
end
module Destroy
def yaffle_route
logger.route "map.yaffle"
gsub_file 'config/routes.rb', /\n.+?map\.yaffles/mi, ''
end
end
module List
def yaffle_route
end
end
module Update
def yaffle_route
end
end
end
end
end
Rails::Generator::Commands::Create.send :include, Yaffle::Generator::Commands::Create
Rails::Generator::Commands::Destroy.send :include, Yaffle::Generator::Commands::Destroy
Rails::Generator::Commands::List.send :include, Yaffle::Generator::Commands::List
Rails::Generator::Commands::Update.send :include, Yaffle::Generator::Commands::Update
-----------------------------------------------------------
*vendor/plugins/yaffle/generators/yaffle/yaffle_route_generator.rb*
[source, ruby]
-----------------------------------------------------------
class YaffleRouteGenerator < Rails::Generator::Base
def manifest
record do |m|
m.yaffle_route
end
end
end
-----------------------------------------------------------
To see this work, type:
-----------------------------------------------------------
./script/generate yaffle_route
./script/destroy yaffle_route
-----------------------------------------------------------
.Editor's note:
NOTE: If you haven't set up the custom route from above, 'script/destroy' will fail and you'll have to remove it manually.
|