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

Add an option database_type #338

Open
wants to merge 1 commit into
base: master
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
3 changes: 2 additions & 1 deletion lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def has_closure_tree(options = {})
:dont_order_roots,
:numeric_order,
:touch,
:with_advisory_lock
:with_advisory_lock,
:database_type
)

class_attribute :_ct
Expand Down
11 changes: 11 additions & 0 deletions lib/closure_tree/numeric_order_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ def self.adapter_for_connection(connection)
end
end

def self.adapter_for_database_type(database_type)
case database_type
when :postgres
::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
Expand Down
14 changes: 13 additions & 1 deletion lib/closure_tree/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ 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)
database_type = database_type_from_options || database_type_from_connection
extend NumericOrderSupport.adapter_for_database_type(database_type)
end
end

Expand Down Expand Up @@ -170,5 +171,16 @@ def create(model_class, attributes)
def create!(model_class, attributes)
create(model_class, attributes).tap { |ea| ea.save! }
end

def database_type_from_connection
das = WithAdvisoryLock::DatabaseAdapterSupport.new(connection)
if das.postgresql?
:postgres
elsif das.mysql?
:mysql
else
:generic
end
end
end
end
4 changes: 4 additions & 0 deletions lib/closure_tree/support_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ def has_inheritance_column?(hash = columns_hash)
def has_name?
model_class.new.attributes.include? options[:name_column]
end

def database_type_from_options
options[:database_type]
end
end
end