Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[57812] Changed CustomOptions to become a hierarchy #16749

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/models/custom_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ class CustomField < ApplicationRecord
# to work as desired.
# Without it, the after_commit callbacks of acts_as_list will prevent the touch to happen.
# https://github.com/rails/rails/issues/26726

has_one :custom_option,
-> { where(parent_id: nil) },
dependent: :delete,
inverse_of: "custom_field"
has_many :custom_options,
-> { order(position: :asc) },
->(current_custom_field) do
where(parent_id: current_custom_field.custom_option.id).order(position: :asc)
end,
dependent: :delete_all,
inverse_of: "custom_field"

accepts_nested_attributes_for :custom_options

acts_as_list scope: [:type]
Expand Down
2 changes: 2 additions & 0 deletions app/models/custom_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class CustomOption < ApplicationRecord

before_destroy :assure_at_least_one_option

has_closure_tree order: "position", numeric_order: true

def to_s
value
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240917092432_add_parent_id_to_custom_option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddParentIdToCustomOption < ActiveRecord::Migration[7.1]
def change
add_column :custom_options, :parent_id, :integer
end
end
16 changes: 16 additions & 0 deletions db/migrate/20240917092519_create_custom_option_hierarchies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateCustomOptionHierarchies < ActiveRecord::Migration[7.1]
def change
create_table :custom_option_hierarchies, id: false do |t|
Copy link
Member

@akabiru akabiru Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 I would leave the id present- ActiveRecord won't play well with nil primary key. For example, #destroy won't work which means you can't leverage things like AR callbacks or the in-house DeleteService which relies on #destroy out of the box.

Of course you have the option to define composite primary keys but I've recently learnt the hard way the change in data structure can be messy

t.integer :ancestor_id, null: false
t.integer :descendant_id, null: false
t.integer :generations, null: false
end

add_index :custom_option_hierarchies, %i[ancestor_id descendant_id generations],
unique: true,
name: "custom_option_anc_desc_idx"

add_index :custom_option_hierarchies, %i[descendant_id],
name: "custom_option_desc_idx"
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240918090313_add_short_to_custom_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddShortToCustomOptions < ActiveRecord::Migration[7.1]
def change
add_column :custom_options, :short, :text
end
end
Loading