22

Just curious about the best practices for Rails in where I put a custom constraints class that's used as a constraint in config/routes.rb. Seems like Rails.root/lib is where all user classes go. Is that appropriate for this? Should I be creating a directory inside for constraints? 2 empty directories exist there now, assets and tasks. Are there conventions for this?

at.
  • 50,922
  • 104
  • 292
  • 461

2 Answers2

24

lib/ would be the appropriate place. If you want to make it cleaner, put it in lib/constraint/authenticated.rb and define your constraints like so

module Constraint
  class Authenticated
    def matches?(request)
      # stuff
    end
  end
end

and in your routes.rb

constraints Constraint::Authenticated.new do
  match 'account' => 'account#index'
end
axsuul
  • 7,370
  • 9
  • 54
  • 71
  • Didn't know you could specify constraints that way. Didn't see that in the Rails Guide, http://guides.rubyonrails.org/routing.html. – at. Oct 24 '12 at 06:41
  • 7
    I have basically the same exact set up - except I get a `NameError: uninitialized constant Constraint` error. When I remove the module, and just reference `MyConstraint.new` (with no namespace), it works. Why does wrapping this in a module throw such an error? I really want that namespace > – Todd Jan 05 '16 at 16:10
12

This is strange that the guide says nothing about that path, but lib/constraints is even listed in the API (ActionDispatch::Routing::Mapper::Scoping):

You are able to move this logic out into a class if it is too complex for routes. This class must have a matches? method defined on it which either returns true if the user should be given access to that route, or false if the user should not.

class Iphone
  def self.matches?(request)
    request.env["HTTP_USER_AGENT"] =~ /iPhone/
  end
end

An expected place for this code would be lib/constraints.

estum
  • 121
  • 1
  • 4
  • 7
    While the API suggests `lib/constraints`, I think `app/constraints` is a more appropriate place because the latter is loaded automatically but the former is not. This is just _my_ practice, though. – Tsutomu Feb 08 '14 at 01:54
  • 4
    Whether it's loaded automatically in development shouldn't matter. I make the decision depending on whether that particular class is domain-specific, following this blog post: http://blog.codeclimate.com/blog/2012/02/07/what-code-goes-in-the-lib-directory/ – lime Jun 13 '14 at 15:52