Sha256: 82cbc856b5ecde8ff566158c0342e4dfddbb0fb38b3bf65bb133d024f29e4d5e

Contents?: true

Size: 916 Bytes

Versions: 2

Compression:

Stored size: 916 Bytes

Contents

# frozen_string_literal: true

module HasStates
  class Base < ActiveRecord::Base
    self.table_name = 'has_states_states'

    belongs_to :stateable, polymorphic: true

    validate :status_is_configured
    validate :state_type_is_configured

    after_save :trigger_callbacks, if: :saved_change_to_status?

    private

    def status_is_configured
      return if HasStates.configuration.valid_status?(
        stateable_type.constantize,
        state_type,
        status
      )

      errors.add(:status, 'is not configured')
    end

    def state_type_is_configured
      return if HasStates.configuration.valid_state_type?(
        stateable_type.constantize,
        state_type
      )

      errors.add(:state_type, 'is not configured')
    end

    def trigger_callbacks
      HasStates.configuration.matching_callbacks(self).each do |callback|
        callback.call(self)
      end
    end
  end
end 

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stateful_models-0.0.3 lib/has_states/base.rb
stateful_models-0.0.2 lib/has_states/base.rb