Skip to content

[Fix #401] Allow wrappers in routes annotation #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# rubocop:disable Metrics/ModuleLength

# == Annotate Routes
#
# Based on:
Expand Down Expand Up @@ -36,7 +38,10 @@ def content(line, maxs, options = {})
def header(options = {})
routes_map = app_routes_map(options)

out = ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
out = []
out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open]

out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
out += ['#']
return out if routes_map.size.zero?

Expand All @@ -51,7 +56,10 @@ def header(options = {})
out += ["# #{content(routes_map[0], maxs, options)}"]
end

out + routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close]

out
end

def do_annotations(options = {})
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/annotate_routes.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ task :annotate_routes => :environment do
options[:position_in_routes] = Annotate.fallback(ENV['position_in_routes'], ENV['position'])
options[:ignore_routes] = Annotate.fallback(ENV['ignore_routes'], nil)
options[:require] = ENV['require'] ? ENV['require'].split(',') : []
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
AnnotateRoutes.do_annotations(options)
end

Expand Down
15 changes: 15 additions & 0 deletions spec/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def mock_file(stubs = {})

AnnotateRoutes.do_annotations(format_markdown: true)
end

it 'wraps annotation if wrapper is specified' do
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with("
# START
# == Route Map
#
# Prefix Verb URI Pattern Controller#Action
# myaction1 GET /url1(.:format) mycontroller1#action
# myaction2 POST /url2(.:format) mycontroller2#action
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
# END\n")

AnnotateRoutes.do_annotations(wrapper_open: 'START', wrapper_close: 'END')
end
end

describe 'When adding' do
Expand Down