Project

rescoped

0.0
No release in over 3 years
Low commit activity in last 3 years
Remove includes or joins from an activerecord scope
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
>= 0
>= 0
~> 10.0
~> 3.2
>= 0.76.0

Runtime

>= 4.0.0
 Project Readme

Rescoped

Build Status

Rescoped allows you to remove specific relations from an activerecord :joins or :includes with some new utilities: remove_includes, remove_joins, and remove_left_outer_joins

Usage

Consider the following scope:

class Photo < ApplicationRecord

  scope :public_api, lambda {
    includes(
      :users,
      :likes,
      :region
    ).viewable.recent
  }
end

Rescoped let's you keep an existing scope but without the same relations structure. Here's an example of keeping the :public_api scope but removing one of the relations from the :includes

# Avoid an includes(:relation).joins(:relation) and instead restructure as a simple INNER JOIN

Photo.public_api.remove_includes(:region).joins(:region)

Some other simple examples:

  1. Remove any number of relations from :left_outer_joins.
Photo.left_outer_joins(:users, :likes, :region).remove_left_outer_joins(:users, :likes)
  1. Remove a single relation from the :joins
Photo.joins(:users, :likes).remove_joins(:users)
  1. The rescoped utilities can be used anywhere in the chain:
Photo.includes(:users, :likes).left_joins(:regions).viewable.recent.remove_includes(:users)

Installation

gem 'rescoped'

License

The gem is available as open source under the terms of the MIT License.

Author

Geoff Ereth (github@geoffereth.com)