Sharing common code between Rails controllers with `Scoped` pattern

If you follow a strict REST / nested resources approach to building your Rails app, you might get sick of repeating common controller actions.

Try the Scoped concern pattern: a place to put shared code (setting variables, authorization) and slim down …


This content originally appeared on DEV Community and was authored by matt swanson

If you follow a strict REST / nested resources approach to building your Rails app, you might get sick of repeating common controller actions.

Try the Scoped concern pattern: a place to put shared code (setting variables, authorization) and slim down your controllers.

Usage

This particular pattern comes from DHH and Basecamp – a codebase that prides itself of using lots of tiny concerns to share bits of behavior.

While the savings of repeating the same before_actions to look up a Channel would be a fine benefit on it’s own, the naming convention of Scoped is such a great, sharp name. Playlists are “scoped” to a channel so it makes total sense that the corresponding controller would be “channel scoped”.

module ChannelScoped
  extend ActiveSupport::Concern

  included do
    before_action :set_channel, :authorize_channel
  end

  private

  def set_channel
    @channel = Channel.find(params[:channel_id])
  end

  def authorize_channel
    authorize @channel # check that user has access, etc
  end
end

class Channels::SubscriptionsController < ApplicationController
  include ChannelScoped
end

class Channels::VideosController < ApplicationController
  include ChannelScoped
end

class Channels::PlaylistsController < ApplicationController
  include ChannelScoped
end

Additional Resources

DHH Gist: Models for Nested Resources


This content originally appeared on DEV Community and was authored by matt swanson


Print Share Comment Cite Upload Translate Updates
APA

matt swanson | Sciencx (2021-03-10T13:00:00+00:00) Sharing common code between Rails controllers with `Scoped` pattern. Retrieved from https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/

MLA
" » Sharing common code between Rails controllers with `Scoped` pattern." matt swanson | Sciencx - Wednesday March 10, 2021, https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/
HARVARD
matt swanson | Sciencx Wednesday March 10, 2021 » Sharing common code between Rails controllers with `Scoped` pattern., viewed ,<https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/>
VANCOUVER
matt swanson | Sciencx - » Sharing common code between Rails controllers with `Scoped` pattern. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/
CHICAGO
" » Sharing common code between Rails controllers with `Scoped` pattern." matt swanson | Sciencx - Accessed . https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/
IEEE
" » Sharing common code between Rails controllers with `Scoped` pattern." matt swanson | Sciencx [Online]. Available: https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/. [Accessed: ]
rf:citation
» Sharing common code between Rails controllers with `Scoped` pattern | matt swanson | Sciencx | https://www.scien.cx/2021/03/10/sharing-common-code-between-rails-controllers-with-scoped-pattern/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.