0.0
No commit activity in last 3 years
No release in over 3 years
Description of ScopesRails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 4
 Project Readme

ScopesRails

gem install scopes_rails

Compatible with Rails 4.x

Manage scopes in Rails.

Without scopes_rails

class User < ActiveRecord::Base

  state_machine :state do
    state :active
    state :removed
    state :lost
    state :beaten
    
    # .etc
  end
  
  scope :active, -> { where state: :active }
  scope :removed, -> { where state: :removed }
  scope :lost, -> { where state: :lost }
  scope :beaten, -> { where state: :beaten }
  
  # etc.
end

Using scopes_rails

class User < ActiveRecord::Base
  state_machine :state do
    state :active
    state :removed
    state :lost
    state :beaten
    
    # .etc
  end
  
end
$ > rails generate scope user
create  app/scopes
create  app/scopes/user_scopes.rb
create  config/initializers/scopes_rails_initializer.rb

app/scopes/user_scopes.rb

require 'scopes_rails/state_machine_scopes'

module UserScopes
  extend ActiveSupport::Concern
  include StateMachinesScopes

  #included do
  #end
end
$ > rails c

> User.active
# output

Add your own scopes to the file.

app/scopes/user_scopes.rb

require 'scopes_rails/state_machine_scopes'

module UserScopes
  extend ActiveSupport::Concern
  include StateMachinesScopes

  included do
    scope :alive, -> { where.not state: :beaten }
  end
end

Remove StateMachineScopes from scopes file if you don't need it.

Testing

Add ScopesRailsIncluding.initialize_scopes in your test_helper

Example on Minitest

class ActiveSupport::TestCase
  ScopesRailsIncluding.initialize_scopes
end