trusty-layouts-extension 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/MIT-LICENSE +98 -0
  2. data/README.md +115 -0
  3. data/Rakefile +144 -0
  4. data/VERSION +1 -0
  5. data/app/models/haml_filter.rb +5 -0
  6. data/app/models/rails_page.rb +39 -0
  7. data/app/views/layouts/trusty.html.haml +1 -0
  8. data/config/initializers/trusty_config.rb +1 -0
  9. data/config/routes.rb +0 -0
  10. data/layouts_extension.rb +21 -0
  11. data/lib/haml_layouts/models/layout.rb +33 -0
  12. data/lib/haml_layouts/models/page.rb +33 -0
  13. data/lib/layouts/engine.rb +5 -0
  14. data/lib/nested_layouts/tags/core.rb +150 -0
  15. data/lib/share_layouts/controllers/action_controller.rb +26 -0
  16. data/lib/share_layouts/helpers/action_view.rb +48 -0
  17. data/lib/tasks/layouts_extension_tasks.rake +55 -0
  18. data/lib/trusty-layouts-extension.rb +1 -0
  19. data/spec/controllers/share_controller_spec.rb +119 -0
  20. data/spec/datasets/layouts_layouts.rb +36 -0
  21. data/spec/datasets/layouts_pages.rb +43 -0
  22. data/spec/lib/haml_layouts/haml_layouts_extension_spec.rb +22 -0
  23. data/spec/lib/haml_layouts/models/layout_spec.rb +36 -0
  24. data/spec/lib/haml_layouts/models/page_spec.rb +40 -0
  25. data/spec/lib/nested_layouts/nested_layouts_extension_spec.rb +16 -0
  26. data/spec/lib/nested_layouts/tags/core_spec.rb +147 -0
  27. data/spec/lib/share_layouts/controllers/action_controller_spec.rb +44 -0
  28. data/spec/lib/share_layouts/helpers/action_view_spec.rb +171 -0
  29. data/spec/lib/share_layouts/share_layouts_extension_spec.rb +22 -0
  30. data/spec/models/haml_filter_spec.rb +0 -0
  31. data/spec/models/rails_page_spec.rb +63 -0
  32. data/spec/spec.opts +3 -0
  33. data/spec/spec_helper.rb +22 -0
  34. data/trusty-layouts-extension.gemspec +31 -0
  35. data/vendor/plugins/rails_upgrade/MIT-LICENSE +20 -0
  36. data/vendor/plugins/rails_upgrade/README.rdoc +26 -0
  37. data/vendor/plugins/rails_upgrade/Rakefile +22 -0
  38. data/vendor/plugins/rails_upgrade/init.rb +2 -0
  39. data/vendor/plugins/rails_upgrade/install.rb +38 -0
  40. data/vendor/plugins/rails_upgrade/lib/application_checker.rb +506 -0
  41. data/vendor/plugins/rails_upgrade/lib/gemfile_generator.rb +95 -0
  42. data/vendor/plugins/rails_upgrade/lib/new_configuration_generator.rb +59 -0
  43. data/vendor/plugins/rails_upgrade/lib/rails_upgrade.rb +0 -0
  44. data/vendor/plugins/rails_upgrade/lib/routes_upgrader.rb +344 -0
  45. data/vendor/plugins/rails_upgrade/lib/tasks/rails_upgrade_tasks.rake +79 -0
  46. data/vendor/plugins/rails_upgrade/test/application_checker_test.rb +344 -0
  47. data/vendor/plugins/rails_upgrade/test/gemfile_generator_test.rb +72 -0
  48. data/vendor/plugins/rails_upgrade/test/new_configuration_generator_test.rb +63 -0
  49. data/vendor/plugins/rails_upgrade/test/routes_upgrader_test.rb +218 -0
  50. data/vendor/plugins/rails_upgrade/test/test_helper.rb +5 -0
  51. data/vendor/plugins/rails_upgrade/uninstall.rb +1 -0
  52. metadata +134 -0
@@ -0,0 +1,95 @@
1
+ module Rails
2
+ module Upgrading
3
+ class GemfileGenerator
4
+ def generate_new_gemfile
5
+ if has_environment?
6
+ generate_gemfile
7
+ else
8
+ raise FileNotFoundError, "Can't find environment.rb [config/environment.rb]!"
9
+ end
10
+ end
11
+
12
+ def has_environment?
13
+ File.exists?("config/environment.rb")
14
+ end
15
+
16
+ def environment_code
17
+ File.open("config/environment.rb").read
18
+ end
19
+
20
+ def generate_gemfile
21
+ environment_file = environment_code
22
+
23
+ # Get each line that starts with config.gem
24
+ gem_lines = environment_file.split("\n").select {|l| l =~ /^\s*config\.gem/}
25
+
26
+ # Toss those lines to a generator class; the lines are evaluated in the
27
+ # context of that instance.
28
+ config = GemfileGenerator.new
29
+ config.instance_eval(gem_lines.join("\n"))
30
+
31
+ config.output
32
+ end
33
+ end
34
+
35
+ class GemfileGenerator
36
+ # Creates a target for the config.gem calls
37
+ def config
38
+ self
39
+ end
40
+
41
+ def initialize
42
+ @gems = []
43
+ end
44
+
45
+ # Receive a call to add a gem to the list
46
+ def gem(name, options={})
47
+ data = {}
48
+
49
+ # Add new keys from old keys
50
+ data[:require] = options[:lib] if options[:lib]
51
+ data[:source] = options[:source] if options[:source]
52
+
53
+ version = options[:version]
54
+ @gems << [name, version, data]
55
+ end
56
+
57
+ # Generate the Gemfile output
58
+ def output
59
+ # Generic preamble, taken from Yehuda Katz's blog
60
+ preamble = <<STR
61
+ # Edit this Gemfile to bundle your application's dependencies.
62
+ # This preamble is the current preamble for Rails 3 apps; edit as needed.
63
+ source 'http://rubygems.org'
64
+
65
+ gem 'rails', '3.0.6'
66
+
67
+ STR
68
+ preamble + generate_upgraded_code
69
+ end
70
+
71
+ # Get Gemfile call for all the gems
72
+ def generate_upgraded_code
73
+ code = @gems.map do |name, version, data|
74
+ version_string = (version ? "'#{version}'" : nil)
75
+ source = data.delete(:source)
76
+
77
+ data_string = nil
78
+ unless data.empty?
79
+ data_string = data.to_a.map {|k, v| ":#{k} => '#{v}'"}.join(", ")
80
+ end
81
+
82
+ # If we have a source, generate a call to +source+ then output the
83
+ # gem call. Otherwise, just generate the gem requirement.
84
+ if source
85
+ str = ["'#{name}'", version_string, data_string].compact.join(", ")
86
+ "source '#{source}'\ngem #{str}"
87
+ else
88
+ str = ["'#{name}'", version_string, data_string].compact.join(", ")
89
+ "gem #{str}"
90
+ end
91
+ end.join("\n")
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module Rails
4
+ module Upgrading
5
+ class NewConfigurationGenerator
6
+ def generate_new_configurations
7
+ if has_environment?
8
+ generate_new_application_rb
9
+ else
10
+ raise FileNotFoundError, "Can't find environment.rb [config/environment.rb]!"
11
+ end
12
+ end
13
+
14
+ def has_environment?
15
+ File.exists?("config/environment.rb")
16
+ end
17
+
18
+ def environment_code
19
+ File.open("config/environment.rb").read
20
+ end
21
+
22
+ def generate_new_application_rb
23
+ environment_file = environment_code
24
+
25
+ initializer_code = ""
26
+ if matches = environment_file.match(/Rails\:\:Initializer\.run do \|config\|\n(.*)\nend/m)
27
+ initializer_code = matches[1]
28
+ else
29
+ raise "There doesn't seem to be a real environment.rb in your app. Are you sure config/environment.rb has the right contents?"
30
+ end
31
+
32
+ frame = "# Put this in config/application.rb
33
+ require File.expand_path('../boot', __FILE__)
34
+
35
+ require 'rails/all'
36
+
37
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
38
+
39
+ module #{app_name.classify}
40
+ class Application < Rails::Application
41
+ config.autoload_paths += [config.root.join('lib')]
42
+ config.encoding = 'utf-8'
43
+ %s
44
+ end
45
+ end"
46
+
47
+ frame % [indent(initializer_code)]
48
+ end
49
+
50
+ def indent(text)
51
+ text.split("\n").map {|l| " #{l}"}.join("\n")
52
+ end
53
+
54
+ def app_name
55
+ File.basename(Dir.pwd)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,344 @@
1
+ # TODO: Fix formatting on member/collection methods
2
+
3
+ module Rails
4
+ module Upgrading
5
+ module FakeRouter
6
+ module ActionController
7
+ module Routing
8
+ class Routes
9
+ def self.setup
10
+ @redrawer = Rails::Upgrading::RouteRedrawer.new
11
+ end
12
+
13
+ def self.redrawer
14
+ @redrawer
15
+ end
16
+
17
+ def self.draw
18
+ yield @redrawer
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ class RoutesUpgrader
26
+ def generate_new_routes
27
+ if has_routes_file?
28
+ upgrade_routes
29
+ else
30
+ raise FileNotFoundError, "Can't find your routes file [config/routes.rb]!"
31
+ end
32
+ end
33
+
34
+ def has_routes_file?
35
+ File.exists?("config/routes.rb")
36
+ end
37
+
38
+ def routes_code
39
+ File.read("config/routes.rb")
40
+ end
41
+
42
+ def upgrade_routes
43
+ FakeRouter::ActionController::Routing::Routes.setup
44
+
45
+ # Read and eval the file; our fake route mapper will capture
46
+ # the calls to draw routes and generate new route code
47
+ FakeRouter.module_eval(routes_code)
48
+
49
+ # Give the route set to the code generator and get its output
50
+ generator = RouteGenerator.new(FakeRouter::ActionController::Routing::Routes.redrawer.routes)
51
+ generator.generate
52
+ end
53
+ end
54
+
55
+ class RouteRedrawer
56
+ attr_accessor :routes
57
+
58
+ def self.stack
59
+ @stack
60
+ end
61
+
62
+ def self.stack=(val)
63
+ @stack = val
64
+ end
65
+
66
+ def initialize
67
+ @routes = []
68
+
69
+ # The old default route was actually two routes; we generate the new style
70
+ # one only if we haven't generated it for the first old default route.
71
+ @default_route_generated = false
72
+
73
+ # Setup the stack for parents; used use proper indentation
74
+ self.class.stack = [@routes]
75
+ end
76
+
77
+ def root(options)
78
+ debug "mapping root"
79
+ @routes << FakeRoute.new("/", options)
80
+ end
81
+
82
+ def connect(path, options={})
83
+ debug "connecting #{path}"
84
+
85
+ if (path == ":controller/:action/:id.:format" || path == ":controller/:action/:id")
86
+ if !@default_route_generated
87
+ current_parent << FakeRoute.new("/:controller(/:action(/:id))", {:default_route => true})
88
+
89
+ @default_route_generated = true
90
+ end
91
+ else
92
+ current_parent << FakeRoute.new(path, options)
93
+ end
94
+ end
95
+
96
+ def resources(*args, &block)
97
+ _res(FakeResourceRoute, args, &block)
98
+ end
99
+
100
+ def resource(*args, &block)
101
+ _res(FakeSingletonResourceRoute, args, &block)
102
+ end
103
+
104
+ def _res(klass, args)
105
+ if args.last.is_a?(Hash)
106
+ options = args.pop
107
+ debug "options #{options.inspect}"
108
+ end
109
+
110
+ args.each do |a|
111
+ current_parent << klass.new(a, options || {})
112
+ debug "mapping resources #{current_parent.last.name}"
113
+
114
+ if block_given?
115
+ parent = current_parent.last
116
+
117
+ parent = stack(parent) do
118
+ yield(self)
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ def namespace(name, options = {})
125
+ debug "mapping namespace #{name}"
126
+ namespace = FakeNamespace.new(name, options)
127
+
128
+ namespace = stack(namespace) do
129
+ yield(self)
130
+ end
131
+
132
+ current_parent << namespace
133
+ end
134
+
135
+ def method_missing(m, *args)
136
+ debug "named route: #{m}"
137
+ current_parent << FakeRoute.new(args.shift, args.pop, m.to_s)
138
+ end
139
+
140
+ def self.indent
141
+ ' ' * ((stack.length) * 2)
142
+ end
143
+
144
+ private
145
+ def debug(txt)
146
+ puts txt if ENV['DEBUG']
147
+ end
148
+
149
+ def stack(obj)
150
+ self.class.stack << obj
151
+ yield
152
+ self.class.stack.pop
153
+ end
154
+
155
+ def current_parent
156
+ self.class.stack.last
157
+ end
158
+ end
159
+
160
+ class RouteObject
161
+ def indent_lines(code_lines)
162
+ if code_lines.length > 1
163
+ code_lines.flatten.map {|l| "#{@indent}#{l.chomp}"}.join("\n") + "\n"
164
+ else
165
+ "#{@indent}#{code_lines.shift}"
166
+ end
167
+ end
168
+
169
+ def opts_to_string(opts)
170
+ opts.is_a?(Hash) ? opts.map {|k, v|
171
+ ":#{k} => " + (v.is_a?(Hash) ? ('{ ' + opts_to_string(v) + ' }') : "#{value_to_string(v)}")
172
+ }.join(", ") : opts.to_s
173
+ end
174
+
175
+ def value_to_string(value)
176
+ case value
177
+ when Regexp, Symbol, Array then value.inspect
178
+ when String then "'" + value.to_s + "'"
179
+ else value.to_s
180
+ end
181
+ end
182
+ end
183
+
184
+ class FakeNamespace < RouteObject
185
+ attr_accessor :routes, :name, :options
186
+
187
+ def initialize(name, options = {})
188
+ @routes = []
189
+ @name, @options = name, options
190
+ @indent = RouteRedrawer.indent
191
+ end
192
+
193
+ def to_route_code
194
+ if !@options.empty?
195
+ options = ', ' + opts_to_string(@options)
196
+ else
197
+ options = ''
198
+ end
199
+
200
+ lines = ["namespace :#{@name}#{options} do", @routes.map {|r| r.to_route_code}, "end"]
201
+
202
+ indent_lines(lines)
203
+ end
204
+
205
+ def <<(val)
206
+ @routes << val
207
+ end
208
+
209
+ def last
210
+ @routes.last
211
+ end
212
+ end
213
+
214
+ class FakeRoute < RouteObject
215
+ attr_accessor :name, :path, :options
216
+
217
+ def initialize(path, options, name = "")
218
+ @path = path
219
+ @options = options || {}
220
+ @name = name
221
+ @indent = RouteRedrawer.indent
222
+ end
223
+
224
+ def to_route_code
225
+ if @options[:default_route]
226
+ indent_lines ["match '#{@path}'"]
227
+ else
228
+ base = "match '%s' => '%s#%s'"
229
+ extra_options = []
230
+
231
+ if not name.empty?
232
+ extra_options << ":as => :#{name}"
233
+ end
234
+
235
+ if @options[:requirements]
236
+ @options[:constraints] = @options.delete(:requirements)
237
+ end
238
+
239
+ if @options[:conditions]
240
+ @options[:via] = @options.delete(:conditions).delete(:method)
241
+ end
242
+
243
+ @options ||= {}
244
+ base = (base % [@path, @options.delete(:controller), (@options.delete(:action) || "index")])
245
+ opts = opts_to_string(@options)
246
+
247
+ route_pieces = ([base] + extra_options + [opts])
248
+ route_pieces.delete("")
249
+
250
+ indent_lines [route_pieces.join(", ")]
251
+ end
252
+ end
253
+ end
254
+
255
+ class FakeResourceRoute < RouteObject
256
+ attr_accessor :name, :children
257
+
258
+ def initialize(name, options = {})
259
+ @name = name
260
+ @children = []
261
+ @options = options
262
+ @indent = RouteRedrawer.indent
263
+ end
264
+
265
+ def to_route_code
266
+ # preserve :only & :except options
267
+ copied_options = @options.reject { |k,v| ![:only, :except].member?(k) }
268
+ unless copied_options.empty?
269
+ copied_options_str = ", " + copied_options.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(",")
270
+ end
271
+
272
+ if !@children.empty? || @options.has_key?(:collection) || @options.has_key?(:member)
273
+ prefix = ["#{route_method} :#{@name}#{copied_options_str} do"]
274
+ lines = prefix + custom_methods + [@children.map {|r| r.to_route_code}.join("\n"), "end"]
275
+
276
+ indent_lines(lines)
277
+ else
278
+ base = "#{route_method} :%s#{copied_options_str}"
279
+ indent_lines [base % [@name]]
280
+ end
281
+ end
282
+
283
+ def custom_methods
284
+ collection_code = generate_custom_methods_for(:collection)
285
+ member_code = generate_custom_methods_for(:member)
286
+ [collection_code, member_code]
287
+ end
288
+
289
+ def generate_custom_methods_for(group)
290
+ return "" unless @options[group]
291
+
292
+ method_code = []
293
+
294
+ RouteRedrawer.stack << self
295
+ @options[group].each do |name, methods|
296
+ [*methods].each do |method|
297
+ method_code << "#{method} :#{name}"
298
+ end
299
+ end
300
+ RouteRedrawer.stack.pop
301
+
302
+ indent_lines ["#{group} do", method_code, "end"].flatten
303
+ end
304
+
305
+ def route_method
306
+ "resources"
307
+ end
308
+
309
+ def <<(val)
310
+ @children << val
311
+ end
312
+
313
+ def last
314
+ @children.last
315
+ end
316
+ end
317
+
318
+ class FakeSingletonResourceRoute < FakeResourceRoute
319
+ def route_method
320
+ "resource"
321
+ end
322
+ end
323
+
324
+ class RouteGenerator
325
+ def initialize(routes)
326
+ @routes = routes
327
+ @new_code = ""
328
+ end
329
+
330
+ def generate
331
+ @new_code = @routes.map do |r|
332
+ r.to_route_code
333
+ end.join("\n")
334
+
335
+ "#{app_name.underscore.classify}::Application.routes.draw do\n#{@new_code}\nend\n"
336
+ end
337
+
338
+ private
339
+ def app_name
340
+ File.basename(Dir.pwd)
341
+ end
342
+ end
343
+ end
344
+ end