Module: Renalware::ExplicitStateModel::ClassMethods
- Defined in:
- app/models/concerns/renalware/explicit_state_model.rb
Instance Method Summary collapse
-
#has_states(*states) ⇒ Object
Adds a ActiveRecord scopes for each state defined; e.g.
- #state_class_name(name) ⇒ Object
-
#state_scope(name, state) ⇒ Object
Allows for custom scope names, example:.
Instance Method Details
#has_states(*states) ⇒ Object
Adds a ActiveRecord scopes for each state defined; e.g. ‘Letter.draft`
rubocop :disable Naming/PredicateName
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/concerns/renalware/explicit_state_model.rb', line 57 def has_states(*states) states.each do |state| state_scope(state, state) define_method "#{state}?" do send(:state) == state.to_s end end define_singleton_method "states" do states end end |
#state_class_name(name) ⇒ Object
87 88 89 |
# File 'app/models/concerns/renalware/explicit_state_model.rb', line 87 def state_class_name(name) const_get(name.to_s.classify).to_s end |
#state_scope(name, state) ⇒ Object
Allows for custom scope names, example:
class Letter
has_states :draft, :approved
state_scope :reviewable, :draft
end
Letter.reviewable # => returns draft letters
81 82 83 84 85 |
# File 'app/models/concerns/renalware/explicit_state_model.rb', line 81 def state_scope(name, state) instance_eval do scope name, -> { where(type: state_class_name(state)) } end end |