lib/has_states/stateable.rb in stateful_models-0.0.2 vs lib/has_states/stateable.rb in stateful_models-0.0.3

- old
+ new

@@ -4,20 +4,23 @@ module Stateable extend ActiveSupport::Concern included do has_many :states, class_name: 'HasStates::Base', - as: :stateable, - dependent: :destroy + as: :stateable, + dependent: :destroy end # Instance methods for managing states def add_state(type, status: 'pending', metadata: {}, state_class: HasStates::State) - states.create!( - type: state_class.name, - state_type: type, - status: status, - metadata: metadata - ) + states.create!(type: state_class.name, state_type: type, status: status, metadata: metadata) + end + + def current_state(type) + states.where(state_type: type).order(created_at: :desc).first + end + + def current_states(type) + states.where(state_type: type).order(created_at: :desc) end end end