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

Use with_connection instead of calling release_connection directly #320

Closed
wants to merge 2 commits into from
Closed
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
56 changes: 28 additions & 28 deletions lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
module ClosureTree
module HasClosureTree
def has_closure_tree(options = {})
options.assert_valid_keys(
:parent_column_name,
:dependent,
:hierarchy_class_name,
:hierarchy_table_name,
:name_column,
:order,
:dont_order_roots,
:numeric_order,
:touch,
:with_advisory_lock
)
connection_pool.with_connection {
options.assert_valid_keys(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the check could happen before (or outside) the connection_pool.with_connection block to avoid useless connections?

:parent_column_name,
:dependent,
:hierarchy_class_name,
:hierarchy_table_name,
:name_column,
:order,
:dont_order_roots,
:numeric_order,
:touch,
:with_advisory_lock
)

class_attribute :_ct
self._ct = ClosureTree::Support.new(self, options)
class_attribute :_ct
self._ct = ClosureTree::Support.new(self, options)

# Auto-inject the hierarchy table
# See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb
class_attribute :hierarchy_class
self.hierarchy_class = _ct.hierarchy_class_for_model
# Auto-inject the hierarchy table
# See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb
class_attribute :hierarchy_class
self.hierarchy_class = _ct.hierarchy_class_for_model

# tests fail if you include Model before HierarchyMaintenance wtf
include ClosureTree::HierarchyMaintenance
include ClosureTree::Model
include ClosureTree::Finders
include ClosureTree::HashTree
include ClosureTree::Digraphs
# tests fail if you include Model before HierarchyMaintenance wtf
include ClosureTree::HierarchyMaintenance
include ClosureTree::Model
include ClosureTree::Finders
include ClosureTree::HashTree
include ClosureTree::Digraphs

include ClosureTree::DeterministicOrdering if _ct.order_option?
include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric?

connection_pool.release_connection
include ClosureTree::DeterministicOrdering if _ct.order_option?
include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric?
}
rescue StandardError => e
raise e unless ClosureTree.configuration.database_less
end
Expand Down
5 changes: 5 additions & 0 deletions spec/pool_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require 'spec_helper'

describe 'Configuration' do
before(:each) do
# Make sure we start up with no active connection
ActiveRecord::Base.connection_pool.release_connection
end

it 'returns connection to the pool after has_closure_tree setup' do
class TypeDuplicate < ActiveRecord::Base
self.table_name = "namespace_type#{table_name_suffix}"
Expand Down