-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/dummy/app/models/foreign_key_status_order_transition.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class ForeignKeyStatusOrderTransition < ApplicationRecord | ||
include Statesman::Adapters::ActiveRecordTransition | ||
|
||
belongs_to :order, foreign_key: 'custom_fk_id', inverse_of: :foreign_key_status_order_transitions | ||
|
||
after_destroy :update_most_recent, if: :most_recent? | ||
|
||
private | ||
|
||
def update_most_recent | ||
last_transition = order.foreign_key_status_order_transitions.order(:sort_key).last | ||
return unless last_transition.present? | ||
last_transition.update_column(:most_recent, true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
test/dummy/db/migrate/20231004142606_create_foreign_key_status_order_transitions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class CreateForeignKeyStatusOrderTransitions < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :foreign_key_status_order_transitions do |t| | ||
t.string :to_state, null: false | ||
t.text :metadata, default: '{}' | ||
t.integer :sort_key, null: false | ||
t.integer :custom_fk_id, null: false | ||
t.boolean :most_recent, null: false | ||
|
||
# If you decide not to include an updated timestamp column in your transition | ||
# table, you'll need to configure the `updated_timestamp_column` setting in your | ||
# migration class. | ||
t.timestamps null: false | ||
end | ||
|
||
# Foreign keys are optional, but highly recommended | ||
add_foreign_key :foreign_key_status_order_transitions, :orders, column: :custom_fk_id | ||
|
||
add_index(:foreign_key_status_order_transitions, | ||
%i(custom_fk_id sort_key), | ||
unique: true, | ||
name: "index_foreign_key_status_order_transitions_parent_sort") | ||
add_index(:foreign_key_status_order_transitions, | ||
%i(custom_fk_id most_recent), | ||
unique: true, | ||
where: "most_recent", | ||
name: "index_foreign_key_status_order_transitions_parent_most_recent") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters