gocardless/statesman

View on GitHub
lib/generators/statesman/templates/active_record_transition_model.rb.erb

Summary

Maintainability
Test Coverage
class <%= klass %> < <%= Statesman::Utils.rails_5_or_higher? ? 'ApplicationRecord' : 'ActiveRecord::Base' %>
  include Statesman::Adapters::ActiveRecordTransition

  # If your transition table doesn't have the default `updated_at` timestamp column,
  # you'll need to configure the `updated_timestamp_column` option, setting it to
  # another column name (e.g. `:updated_on`) or `nil`.
  #
  # self.updated_timestamp_column = :updated_on
  # self.updated_timestamp_column = nil

<%- unless Statesman::Utils.rails_4_or_higher? -%>
  attr_accessible :to_state, :metadata, :sort_key

<%- end -%>
  belongs_to :<%= parent_name %><%= class_name_option %>, inverse_of: :<%= table_name %>

  after_destroy :update_most_recent, if: :most_recent?

  private

  def update_most_recent
    last_transition = <%= parent_name %>.<%= table_name %>.order(:sort_key).last
    return unless last_transition.present?
    last_transition.update_column(:most_recent, true)
  end
end