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

Dirty tracking lost when setting parent with numeric_order option #414

Open
ryanb opened this issue Jan 23, 2023 · 1 comment
Open

Dirty tracking lost when setting parent with numeric_order option #414

ryanb opened this issue Jan 23, 2023 · 1 comment

Comments

@ryanb
Copy link

ryanb commented Jan 23, 2023

Say I have a model with numeric_order option:

class Category < ApplicationRecord
  has_closure_tree order: "position", numeric_order: true
end

With this setting we lose dirty tracking after setting the parent:

category.update! parent: parent_category, name: "Foo"
category.saved_change_to_name? # => false

All dirty tracking is lost since it ends up calling reload internally.

Perhaps this code could just reload the order attribute instead of the entire record? This way dirty tracking would persist.

@ryanb
Copy link
Author

ryanb commented Jan 24, 2023

For the record, I'm currently monkey-patching to work around this issue. Feel free to use this in the implementation.

# in an initializer:
module ClosureTree
  module NumericDeterministicOrdering
    def _ct_reorder_siblings(minimum_sort_order_value = nil)
      _ct.reorder_with_parent_id(_ct_parent_id, minimum_sort_order_value)
      if !destroyed? && _ct.order_column
        self[_ct.order_column] = self.class
          .where(self.class.primary_key => id_in_database)
          .pick(_ct.order_column)
        clear_attribute_change(_ct.order_column)
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant