Created
February 5, 2016 09:16
-
-
Save akuhn/c5ec3298dd7d5994fafe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Maps a Rails route to a dynamic action name using interpolation. | |
| # | |
| # get 'experiments/ajax/:endpoint', to: dynamic('experiments#ajax_%{endpoint}') | |
| class Dynamic | |
| def initialize(to) | |
| @to = to | |
| end | |
| def call(env) | |
| params = env['action_dispatch.request.path_parameters'] | |
| controller, action = @to.split('#') | |
| params[:action] = action = action % params | |
| params[:controller] = controller | |
| "#{controller.camelize}Controller".constantize.action(action).call(env) | |
| end | |
| def to_s | |
| @to | |
| end | |
| end | |
| def dynamic(to) | |
| Dynamic.new(to) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment