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