module ActionDispatch::Routing::Mapper::Concerns
def concern(name, callable = nil, &block)
callable, they're accessible from the Mapper that's passed to
Any routing helpers can be used inside a concern. If using a
end
concerns :purchasable, returnable: false
resources :pets do
resources :electronics, concerns: :purchasable
resources :toys, concerns: :purchasable
concern :purchasable, Purchasable.new(returnable: true)
# routes.rb
end
end
mapper.resources :returns if options[:returnable]
mapper.resources :receipts
mapper.resources :purchases
options = @defaults.merge(options)
def call(mapper, options = {})
end
@defaults = defaults
def initialize(defaults = {})
class Purchasable
# purchasable.rb
routes file.
specific to your application, which would be out of place in your
Or, using a callable object, you might implement something more
end
concerns :commentable, only: [:index, :show]
# Don't allow comments on archived posts
resources :archived_posts do
resources :posts, concerns: :commentable
end
resources :comments, options
concern :commentable do |options|
standard resource options through the concern:
simple as limit the actions available on certain resources, passing
a block parameter. So, using a block, you might do something as
Options may also be used by concerns defined in a block by accepting
* A hash of options which the concern object may use
* The current mapper
which will receive two parameters:
The concern object, if supplied, should respond to call,
another object, by passing that object as the second parameter.
Concerns may be defined inline, using a block, or handled by
Define a routing concern using a name.
def concern(name, callable = nil, &block) callable ||= lambda { |mapper, options| mapper.instance_exec(options, &block) } @concerns[name] = callable end