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 606f5371..df69a3d9 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 9dd7604c..b3a58e0b 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