Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kalashnikovisme/scopes_rails
Scopes managment
https://github.com/kalashnikovisme/scopes_rails
Last synced: 1 day ago
JSON representation
Scopes managment
- Host: GitHub
- URL: https://github.com/kalashnikovisme/scopes_rails
- Owner: kalashnikovisme
- License: mit
- Created: 2016-03-01T20:45:30.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-24T21:44:41.000Z (almost 9 years ago)
- Last Synced: 2024-12-10T03:27:07.908Z (about 1 month ago)
- Language: Ruby
- Size: 28.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ScopesRails
```
gem install scopes_rails
```
Compatible with `Rails 4.x`Manage scopes in Rails.
Without `scopes_rails`
```ruby
class User < ActiveRecord::Basestate_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`
```ruby
class User < ActiveRecord::Base
state_machine :state do
state :active
state :removed
state :lost
state :beaten
# .etc
end
end
``````shell
$ > 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*
```ruby
require 'scopes_rails/state_machine_scopes'module UserScopes
extend ActiveSupport::Concern
include StateMachinesScopes#included do
#end
end
``````ruby
$ > rails c> User.active
# output
```Add your own scopes to the file.
*app/scopes/user_scopes.rb*
```ruby
require 'scopes_rails/state_machine_scopes'module UserScopes
extend ActiveSupport::Concern
include StateMachinesScopesincluded 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
```ruby
class ActiveSupport::TestCase
ScopesRailsIncluding.initialize_scopes
end
```