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

Routing constraint with hash and instance of class that responds to #matches? #24013

Closed
jcoyne opened this issue Mar 2, 2016 · 4 comments
Closed

Comments

@jcoyne
Copy link
Contributor

jcoyne commented Mar 2, 2016

Steps to reproduce

In order to match a character like / or : in a route I can add a constraint block like so:
get 'lib/:id' => 'catalog#show', constraints: { id: /ark:\/\d{5}\/f\w{7,9}/ }

This will match
'/lib/ark:/99999/fk41234567' => { controller: 'catalog', action: 'show', id: 'ark:/99999/fk41234567' }

Now, I'd like to add an additional constraint of an object that responds to matches?

class RouteMatcher
  def matches?(request)
     true
  end
end

get 'lib/:id' => 'catalog#show', constraints: RouteMatcher.new

However, now I've lost the ability to capture / in my id parameter.

Expected behavior

It would be great if you could have multiple constraints like this:

get 'lib/:id' => 'catalog#show', constraints: [
                   RouteMatcher.new,
                   { id: /ark:\/\d{5}\/f\w{7,9}/ }
                 ]

Actual behavior

Tell us what happens instead
ArgumentError: Invalid constraint

/Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:225:in `verify_callable_constraint': Invalid constraint: [#<AudioRoutingConcern:0x007fcf698a3f28>, {:id=>/ark:\/\d{5}\/f\w{7,9}/}] must respond to :call or :matches? (ArgumentError)
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:324:in `blocks'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:108:in `initialize'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:78:in `new'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:78:in `build'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:1560:in `add_route'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:1537:in `decomposed_match'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:1518:in `block in match'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:1508:in `each'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:1508:in `match'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:690:in `map_method'
        from /Users/justin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5.1/lib/action_dispatch/routing/mapper.rb:651:in `get'
        from /Users/justin/workspace/alexandria-v2/config/routes.rb:45:in `block in <top (required)>'

System configuration

Rails version: 4.5.1

Ruby version: 2.3.0

@vliegenthart
Copy link

I am willing to work on this if this behaviour would be approved by Rails members :)

@sikachu
Copy link
Member

sikachu commented Mar 4, 2016

What happen if you use *id instead of :id? I feel like that better match your intention as you want everything that comes after lib/.

@rails-bot
Copy link

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 5-0-stable branch or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot
Copy link

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 5-0-stable branch or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants