Skip to content
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

Add post_processor to customize the output of each route #306

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ Options to configure JavaScript file generator. These options are only available
* Default: `Rails.application`
* `file` - a file location where generated routes are stored
* Default: `app/javascript/routes.js` if setup with Webpacker, otherwise `app/assets/javascripts/routes.js` if setup with Sprockets.

* `post_processor` - a proc that takes a string and returns a string. It is called after the each route is generated and can be used to modify the generated code.
* Default: `nil`
* Example: `->(code) { code + addition }`
<div id="formatter-options"></div>

#### Formatter Options
Expand Down
4 changes: 3 additions & 1 deletion lib/js_routes/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Configuration
application: -> { Rails.application },
module_type: 'ESM',
documentation: true,
post_processor: nil,
} #:nodoc:

attr_accessor(*DEFAULTS.keys)
Expand All @@ -32,9 +33,10 @@ def assign(attributes = nil, &block)
raise "Provide attributes or block"
end
tap(&block) if block
no_instant_call_attributes = [:post_processor]
if attributes
attributes.each do |attribute, value|
value = value.call if value.is_a?(Proc)
value = value.call if value.is_a?(Proc) && !no_instant_call_attributes.include?(attribute)
send(:"#{attribute}=", value)
end
end
Expand Down
16 changes: 11 additions & 5 deletions lib/js_routes/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def named_routes

def routes_object
return json({}) if @configuration.modern?
properties = routes_list.map do |comment, name, body|
"#{comment}#{name}: #{body}".indent(2)
properties = routes_list.map do |comment, name, body, route|
post_process("#{comment}#{name}: #{body}", route).indent(2)
end
"{\n" + properties.join(",\n\n") + "}\n"
end
Expand All @@ -112,15 +112,21 @@ def static_exports
"", name,
@configuration.dts? ?
"RouterExposedMethods['#{name}']" :
"__jsr.#{name}"
"__jsr.#{name}",
nil
]
end
end

def post_process(content, route)
@configuration.post_processor ? @configuration.post_processor.call(content, route) : content
end

def routes_export
return "" unless @configuration.modern?
[*static_exports, *routes_list].map do |comment, name, body|
"#{comment}export const #{name}#{export_separator}#{body};\n\n"
[*static_exports, *routes_list].map do |comment, name, body, route|
content = "#{comment}export const #{name}#{export_separator}#{body};\n\n"
post_process(content, route)
end.join
end

Expand Down
2 changes: 1 addition & 1 deletion lib/js_routes/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(configuration, route, parent_route = nil)

def helpers
helper_types.map do |absolute|
[ documentation, helper_name(absolute), body(absolute) ]
[ documentation, helper_name(absolute), body(absolute), @route ]
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/js_routes/module_types/dts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,26 @@
is_expected.to include("export const inbox_message_path = __jsr.r(")
end
end

context do
let(:post_processor) do
Proc.new do |content, route|
if route
pattern = route.path.spec.to_s.gsub('(.:format)', '').gsub('.:format', '')
content + %Q[\nexport const #{route.name}_pattern = '#{pattern}';\n\n]
else
content
end
end
end

let(:generated_js) do
JsRoutes.generate(**OPTIONS.merge(post_processor: post_processor), **extra_options)
end

it "exports route helpers" do
expect(generated_js).to include("export const inboxes_pattern = '/inboxes';")
expect(generated_js).to include("export const inbox_message_attachment_pattern = '/inboxes/:inbox_id/messages/:message_id/attachments/:id';")
end
end
end
19 changes: 19 additions & 0 deletions spec/js_routes/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,23 @@
expect(generated_js).not_to include("@returns")
end
end

describe "post_processor option" do
let(:post_processor) do
Proc.new do |content, route|
if route
pattern = route.path.spec.to_s.gsub('(.:format)', '').gsub('.:format', '')
content + %Q[,\n#{route.name}_pattern: '#{pattern}']
else
content
end
end
end
let(:_options) { {post_processor: post_processor} }

it "disables documentation generation" do
expect(generated_js).to include("inboxes_pattern: '/inboxes',")
expect(generated_js).to include("inbox_message_attachment_pattern: '/inboxes/:inbox_id/messages/:message_id/attachments/:id',")
end
end
end