module ActionDispatch::Routing::Mapper::Concerns
def concern(name, callable = nil, &block)
Any routing helpers can be used inside a concern. If using a callable, they're
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
your application, which would be out of place in your routes file.
Or, using a callable object, you might implement something more specific to
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|
through the concern:
actions available on certain resources, passing standard resource options
parameter. So, using a block, you might do something as simple as limit the
Options may also be used by concerns defined in a block by accepting a block
* A hash of options which the concern object may use
* The current mapper
two parameters:
The concern object, if supplied, should respond to `call`, which will receive
by passing that object as the second parameter.
Concerns may be defined inline, using a block, or handled by another object,
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