closure_tree 3.6.6 → 3.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # Closure Tree [![Build Status](https://secure.travis-ci.org/mceachen/closure_tree.png?branch=master)](http://travis-ci.org/mceachen/closure_tree)
2
2
 
3
- Closure Tree is a mostly-API-compatible replacement for the
3
+ ### Closure_tree lets your ActiveRecord models act as nodes in a [tree data structure](http://en.wikipedia.org/wiki/Tree_%28data_structure%29)
4
+
5
+ Common applications include modeling hierarchical data, like tags, page graphs in CMSes,
6
+ and tracking user referrals.
7
+
8
+ Mostly API-compatible with other popular nesting gems for Rails, like
4
9
  [ancestry](https://github.com/stefankroes/ancestry),
5
10
  [acts_as_tree](https://github.com/amerine/acts_as_tree) and
6
- [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set/) gems, giving you:
11
+ [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set/),
12
+ closure_tree has some great features:
7
13
 
8
14
  * __Best-in-class select performance__:
9
15
  * Fetch your whole ancestor lineage in 1 SELECT.
@@ -361,6 +367,12 @@ Closure tree is [tested under every combination](http://travis-ci.org/#!/mceache
361
367
 
362
368
  ## Change log
363
369
 
370
+ ### 3.6.7
371
+
372
+ * Added workaround for ActiveRecord::Observer usage pre-db-creation. Addresses
373
+ [issue 32](https://github.com/mceachen/closure_tree/issues/32).
374
+ Thanks, [Don Morrison](https://github.com/elskwid)!
375
+
364
376
  ### 3.6.6
365
377
 
366
378
  * Added support for Rails 4's [strong parameter](https://github.com/rails/strong_parameters).
@@ -457,7 +457,8 @@ module ClosureTree
457
457
  end
458
458
 
459
459
  def order_is_numeric
460
- return false unless order_option
460
+ # The table might not exist yet (in the case of ActiveRecord::Observer use, see issue 32)
461
+ return false if order_option.nil? || !self.table_exists?
461
462
  c = ct_class.columns_hash[order_option]
462
463
  c && c.type == :integer
463
464
  end
@@ -1,3 +1,3 @@
1
1
  module ClosureTree
2
- VERSION = "3.6.6" unless defined?(::ClosureTree::VERSION)
2
+ VERSION = "3.6.7" unless defined?(::ClosureTree::VERSION)
3
3
  end
@@ -1,10 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
- def nuke_db
3
+ def delete_all_labels
4
4
  LabelHierarchy.delete_all
5
5
  Label.delete_all
6
6
  end
7
7
 
8
+ def create_label_hierarchy
9
+ @d1 = Label.find_or_create_by_path %w(a1 b1 c1 d1)
10
+ @c1 = @d1.parent
11
+ @b1 = @c1.parent
12
+ @a1 = @b1.parent
13
+ @d2 = Label.find_or_create_by_path %w(a1 b1 c2 d2)
14
+ @c2 = @d2.parent
15
+ @d3 = Label.find_or_create_by_path %w(a2 b2 c3 d3)
16
+ @c3 = @d3.parent
17
+ @b2 = @c3.parent
18
+ @a2 = @b2.parent
19
+ Label.update_all("sort_order = id")
20
+ end
21
+
8
22
  describe Label do
9
23
  context "Base Label class" do
10
24
  it "should find or create by path" do
@@ -43,7 +57,7 @@ describe Label do
43
57
  context "Mixed class tree" do
44
58
  it "should support mixed type ancestors" do
45
59
  [Label, DateLabel, DirectoryLabel, EventLabel].permutation do |classes|
46
- nuke_db
60
+ delete_all_labels
47
61
  classes.each { |c| c.all.should(be_empty, "class #{c} wasn't cleaned out") }
48
62
  names = ('A'..'Z').to_a.first(classes.size)
49
63
  instances = classes.collect { |clazz| clazz.new(:name => names.shift) }
@@ -66,18 +80,8 @@ describe Label do
66
80
  end
67
81
  context "find_all_by_generation" do
68
82
  before :all do
69
- nuke_db
70
- @d1 = Label.find_or_create_by_path %w(a1 b1 c1 d1)
71
- @c1 = @d1.parent
72
- @b1 = @c1.parent
73
- @a1 = @b1.parent
74
- @d2 = Label.find_or_create_by_path %w(a1 b1 c2 d2)
75
- @c2 = @d2.parent
76
- @d3 = Label.find_or_create_by_path %w(a2 b2 c3 d3)
77
- @c3 = @d3.parent
78
- @b2 = @c3.parent
79
- @a2 = @b2.parent
80
- Label.update_all("sort_order = id")
83
+ delete_all_labels
84
+ create_label_hierarchy
81
85
  end
82
86
 
83
87
  it "finds roots from the class method" do
@@ -115,6 +119,27 @@ describe Label do
115
119
  end
116
120
  end
117
121
 
122
+ context "loading through self_and_ scopes" do
123
+ before :all do
124
+ delete_all_labels
125
+ create_label_hierarchy
126
+ end
127
+
128
+ it "self_and_descendants should result in one select" do
129
+ DB_QUERIES.clear
130
+ a1_array = @a1.self_and_descendants
131
+ a1_array.collect { |ea| ea.name }.should == %w(a1 b1 c1 c2 d1 d2)
132
+ DB_QUERIES.size.should == 1
133
+ end
134
+
135
+ it "self_and_ancestors should result in one select" do
136
+ DB_QUERIES.clear
137
+ d1_array = @d1.self_and_ancestors
138
+ d1_array.collect { |ea| ea.name }.should == %w(d1 c1 b1 a1)
139
+ DB_QUERIES.size.should == 1
140
+ end
141
+ end
142
+
118
143
  context "deterministically orders with polymorphic siblings" do
119
144
  before :each do
120
145
  @parent = Label.create!(:name => "parent")
@@ -140,7 +165,7 @@ describe Label do
140
165
  @a.reload.sort_order.should be < @c.reload.sort_order
141
166
  end
142
167
  end
143
-
168
+
144
169
  it "behaves like the readme" do
145
170
  root = Label.create(:name => "root")
146
171
  a = Label.create(:name => "a", :parent => root)
@@ -161,7 +186,7 @@ describe Label do
161
186
  end
162
187
 
163
188
  context "Deterministic siblings sort with custom integer column" do
164
- nuke_db
189
+ delete_all_labels
165
190
  fixtures :labels
166
191
 
167
192
  before :each do
@@ -30,9 +30,23 @@ require 'db/schema'
30
30
  require 'support/models'
31
31
  require 'rspec/rails' # TODO: clean this up-- I don't want to pull the elephant through the mouse hole just for fixture support
32
32
 
33
+ DB_QUERIES = []
34
+
35
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
36
+ def log_with_query_append(query, *args, &block)
37
+ DB_QUERIES << query
38
+ log_without_query_append(query, *args, &block)
39
+ end
40
+
41
+ alias_method_chain :log, :query_append
42
+ end
43
+
33
44
  RSpec.configure do |config|
34
45
  config.fixture_path = "#{plugin_test_dir}/fixtures"
35
46
  # true runs the tests 1 second faster, but then you can't
36
47
  # see what's going on while debuggering with different db connections.
37
48
  config.use_transactional_fixtures = false
49
+ config.after(:each) do
50
+ DB_QUERIES.clear
51
+ end
38
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: closure_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.6
4
+ version: 3.6.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-24 00:00:00.000000000 Z
12
+ date: 2012-12-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -208,7 +208,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
208
208
  version: '0'
209
209
  segments:
210
210
  - 0
211
- hash: 2617295587215713218
211
+ hash: -486781145760134167
212
212
  required_rubygems_version: !ruby/object:Gem::Requirement
213
213
  none: false
214
214
  requirements:
@@ -217,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  version: '0'
218
218
  segments:
219
219
  - 0
220
- hash: 2617295587215713218
220
+ hash: -486781145760134167
221
221
  requirements: []
222
222
  rubyforge_project:
223
223
  rubygems_version: 1.8.23