From a6a2d6853711ccc42c87c6a06cf0002f62aa776c Mon Sep 17 00:00:00 2001 From: Nicolas Rodriguez Date: Fri, 25 May 2018 22:35:03 +0200 Subject: [PATCH] Fix https://github.com/ClosureTree/closure_tree/issues/308 --- README.md | 2 +- lib/closure_tree/has_closure_tree.rb | 1 + lib/closure_tree/support.rb | 3 ++- lib/closure_tree/support_flags.rb | 8 +------- spec/db/models.rb | 1 + 5 files changed, 6 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4e6a801c..30d36a5d 100644 --- a/README.md +++ b/README.md @@ -423,7 +423,7 @@ and in your model: ```ruby class OrderedTag < ActiveRecord::Base - has_closure_tree order: 'sort_order' + has_closure_tree order: 'sort_order', numeric_order: true end ``` diff --git a/lib/closure_tree/has_closure_tree.rb b/lib/closure_tree/has_closure_tree.rb index 0e7d61ac..7e08a835 100644 --- a/lib/closure_tree/has_closure_tree.rb +++ b/lib/closure_tree/has_closure_tree.rb @@ -8,6 +8,7 @@ def has_closure_tree(options = {}) :hierarchy_table_name, :name_column, :order, + :numeric_order, :touch, :with_advisory_lock ) diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index d464f675..7cdeb8d4 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -22,7 +22,8 @@ def initialize(model_class, options) :parent_column_name => 'parent_id', :dependent => :nullify, # or :destroy or :delete_all -- see the README :name_column => 'name', - :with_advisory_lock => true + :with_advisory_lock => true, + :numeric_order => false }.merge(options) raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path' if order_is_numeric? diff --git a/lib/closure_tree/support_flags.rb b/lib/closure_tree/support_flags.rb index 043e785f..cdf7fd8b 100644 --- a/lib/closure_tree/support_flags.rb +++ b/lib/closure_tree/support_flags.rb @@ -17,13 +17,7 @@ def order_option? end def order_is_numeric? - # skip if database is not connected. - return false unless ::ActiveRecord::Base.connected? - - # The table might not exist yet (in the case of ActiveRecord::Observer use, see issue 32) - return false if !order_option? || !model_class.table_exists? - c = model_class.columns_hash[order_column] - c && c.type == :integer + options[:numeric_order] end def subclass? diff --git a/spec/db/models.rb b/spec/db/models.rb index 74a0ef4f..939c13dc 100644 --- a/spec/db/models.rb +++ b/spec/db/models.rb @@ -81,6 +81,7 @@ class ContractType < ActiveRecord::Base class Label < ActiveRecord::Base # make sure order doesn't matter acts_as_tree :order => :column_whereby_ordering_is_inferred, # <- symbol, and not "sort_order" + :numeric_order => true, :parent_column_name => "mother_id", :dependent => :destroy