Module: ConcernsOnRails::Models::Activatable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/activatable.rb
Overview
Boolean active/inactive toggle backed by a single column.
class Subscription < ApplicationRecord
include ConcernsOnRails::Activatable
activatable_by # defaults to :active
# activatable_by :enabled # custom column name
end
Subscription.active # WHERE active = TRUE
Subscription.inactive # WHERE active = FALSE OR active IS NULL
NULL is treated as inactive, mirroring how unset booleans behave in most apps.
Note: SoftDeletable also defines a ‘.active` scope (alias of `.without_deleted`). If both concerns are included on the same model, the later one wins.
Constant Summary collapse
- DEFAULT_FIELD =
:active
Instance Method Summary collapse
- #activate! ⇒ Object
- #active? ⇒ Boolean
- #deactivate! ⇒ Object
- #inactive? ⇒ Boolean
- #toggle_active! ⇒ Object
Instance Method Details
#activate! ⇒ Object
52 53 54 |
# File 'lib/concerns_on_rails/models/activatable.rb', line 52 def activate! update(self.class.activatable_field => true) end |
#active? ⇒ Boolean
44 45 46 |
# File 'lib/concerns_on_rails/models/activatable.rb', line 44 def active? self[self.class.activatable_field] == true end |
#deactivate! ⇒ Object
56 57 58 |
# File 'lib/concerns_on_rails/models/activatable.rb', line 56 def deactivate! update(self.class.activatable_field => false) end |
#inactive? ⇒ Boolean
48 49 50 |
# File 'lib/concerns_on_rails/models/activatable.rb', line 48 def inactive? !active? end |
#toggle_active! ⇒ Object
60 61 62 |
# File 'lib/concerns_on_rails/models/activatable.rb', line 60 def toggle_active! active? ? deactivate! : activate! end |