Last active
March 19, 2018 00:01
-
-
Save wrburgess/9c434c1bb693fcb48219d6dd4ab00dfc to your computer and use it in GitHub Desktop.
Activatable concern for Rails 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddActivatableFieldsToModel < ActiveRecord::Migration[5.0] | |
def change | |
add_column :users, :archived, default: false | |
add_column :users, :test, default: false | |
add_column :users, :dummy, default: false | |
add_index :users, :archived | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/concerns/activatable.rb | |
module Activatable | |
extend ActiveSupport::Concern | |
included do | |
scope :actives, -> { where(archived: false, test: false, dummy: false) } | |
scope :inactives, -> { where("archived IS TRUE OR test IS TRUE OR dummy IS TRUE") } | |
scope :archives, -> { where(archived: true) } | |
scope :tests, -> { where(test: true) } | |
scope :dummies, -> { where(dummy: true) } | |
end | |
def active? | |
!archived && !test && !dummy | |
end | |
def activate! | |
update(archived: false, test: false, dummy: false) | |
end | |
def archive! | |
update(archived: true) | |
end | |
def unarchive! | |
update(archived: false) | |
end | |
def testify! | |
update(test: true) | |
end | |
def dummify! | |
update(dummy: true) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User < ActiveRecord::Base | |
include Activatable | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/users_spec.rb | |
require "rails_helper" | |
require "concerns/activatable_shared" | |
describe User, type: :model do | |
it_behaves_like "activatable" | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/factories/users.rb | |
FactoryGirl.define do | |
factory :user do | |
... | |
trait :archived do | |
archived { true } | |
end | |
trait :test do | |
test { true } | |
end | |
trait :dummy do | |
dummy { true } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment