From df3446ee884ea8ea4e1caff96e2bd9dbd4ab3924 Mon Sep 17 00:00:00 2001 From: Deividas J Date: Thu, 4 Apr 2019 16:40:45 +0300 Subject: [PATCH] Added: database_type to configuration --- lib/closure_tree/configuration.rb | 1 + lib/closure_tree/numeric_order_support.rb | 11 +++++++++++ lib/closure_tree/support.rb | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/closure_tree/configuration.rb b/lib/closure_tree/configuration.rb index b34c547d..5e64fd71 100644 --- a/lib/closure_tree/configuration.rb +++ b/lib/closure_tree/configuration.rb @@ -1,6 +1,7 @@ module ClosureTree class Configuration # :nodoc: attr_accessor :database_less + attr_accessor :database_type def initialize @database_less = ENV['DATABASE_URL'].to_s.include?('//user:pass@127.0.0.1/') diff --git a/lib/closure_tree/numeric_order_support.rb b/lib/closure_tree/numeric_order_support.rb index 0223a415..e7f240b6 100644 --- a/lib/closure_tree/numeric_order_support.rb +++ b/lib/closure_tree/numeric_order_support.rb @@ -12,6 +12,17 @@ def self.adapter_for_connection(connection) end end + def self.adapter_for_database_type(database_type) + case database_type + when :postgresql + ::ClosureTree::NumericOrderSupport::PostgreSQLAdapter + when :mysql + ::ClosureTree::NumericOrderSupport::MysqlAdapter + else + ::ClosureTree::NumericOrderSupport::GenericAdapter + end + end + module MysqlAdapter def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil) return if parent_id.nil? && dont_order_roots diff --git a/lib/closure_tree/support.rb b/lib/closure_tree/support.rb index 75108d79..75b2788a 100644 --- a/lib/closure_tree/support.rb +++ b/lib/closure_tree/support.rb @@ -27,7 +27,11 @@ def initialize(model_class, options) }.merge(options) raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path' if order_is_numeric? - extend NumericOrderSupport.adapter_for_connection(connection) + if ClosureTree.configuration.database_type + extend NumericOrderSupport.adapter_for_database_type(ClosureTree.configuration.database_type) + else + extend NumericOrderSupport.adapter_for_connection(connection) + end end end