activerecord-recursive_tree_scopes 0.1.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: efa3ff153255f4cac37a076ac482b8d13ef8825d
4
+ data.tar.gz: b5918f4bda320f432e0b51711423a815c9fa8a9b
5
+ SHA512:
6
+ metadata.gz: 82b635e671e577ddca6a369351f1d98fc013b027330472241498e546c9c33844e7cbec8c34b97ebadc742126190d2e4b5a69cf80dfcb02e3d3decf985687925d
7
+ data.tar.gz: adb8ffcf9ff36ba16d98fc5b20edf9456c27f5da2b9b739a809db7d789c5d1daa7cd7083ac5cc3b9ee7c163c342b6403ccc0adce3a67208a4ae93f731714097b
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,25 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
7
+ - rbx-19mode
8
+ gemfile:
9
+ - .travis/activerecord-3.1.gemfile
10
+ - .travis/activerecord-3.2.gemfile
11
+ - .travis/activerecord-4.0.gemfile
12
+ env:
13
+ - DB=postgresql
14
+ script:
15
+ - rm spec/database.yml
16
+ - mv spec/database.yml.travis spec/database.yml
17
+ - bundle exec rspec spec
18
+ before_script:
19
+ - psql -c 'create database activerecordrecursivetreerelationstest' -U postgres
20
+
21
+ matrix:
22
+ exclude:
23
+ - rvm: 1.9.2
24
+ gemfile: .travis/activerecord-4.0.gemfile
25
+ env: DB=postgresql
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 3.0.0'
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ platforms :jruby do
8
+ gem 'activerecord-jdbcpostgresql-adapter'
9
+ end
10
+ platforms :ruby do
11
+ gem 'pg'
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 3.1.0'
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ platforms :jruby do
8
+ gem 'activerecord-jdbcpostgresql-adapter'
9
+ end
10
+ platforms :ruby do
11
+ gem 'pg'
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 3.2.0'
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ platforms :jruby do
8
+ gem 'activerecord-jdbcpostgresql-adapter'
9
+ end
10
+ platforms :ruby do
11
+ gem 'pg'
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.0.0'
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ platforms :jruby do
8
+ gem 'activerecord-jdbcpostgresql-adapter'
9
+ end
10
+ platforms :ruby do
11
+ gem 'pg'
12
+ end
13
+ end
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '>= 3'
4
+
5
+ group :development do
6
+ gem 'rspec'
7
+ gem 'rdoc'
8
+ gem 'bundler'
9
+ gem 'jeweler'
10
+ end
11
+
12
+ group :test do
13
+ platforms :jruby do
14
+ gem 'activerecord-jdbcpostgresql-adapter'
15
+ end
16
+ platforms :ruby do
17
+ gem 'pg'
18
+ end
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,101 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (4.0.0)
5
+ activesupport (= 4.0.0)
6
+ builder (~> 3.1.0)
7
+ activerecord (4.0.0)
8
+ activemodel (= 4.0.0)
9
+ activerecord-deprecated_finders (~> 1.0.2)
10
+ activesupport (= 4.0.0)
11
+ arel (~> 4.0.0)
12
+ activerecord-deprecated_finders (1.0.3)
13
+ activerecord-jdbc-adapter (1.3.1)
14
+ activerecord (>= 2.2)
15
+ activerecord-jdbcpostgresql-adapter (1.3.1)
16
+ activerecord-jdbc-adapter (~> 1.3.1)
17
+ jdbc-postgres (>= 9.1)
18
+ activesupport (4.0.0)
19
+ i18n (~> 0.6, >= 0.6.4)
20
+ minitest (~> 4.2)
21
+ multi_json (~> 1.3)
22
+ thread_safe (~> 0.1)
23
+ tzinfo (~> 0.3.37)
24
+ addressable (2.3.5)
25
+ arel (4.0.0)
26
+ atomic (1.1.14)
27
+ atomic (1.1.14-java)
28
+ builder (3.1.4)
29
+ diff-lcs (1.1.3)
30
+ faraday (0.8.8)
31
+ multipart-post (~> 1.2.0)
32
+ git (1.2.6)
33
+ github_api (0.10.1)
34
+ addressable
35
+ faraday (~> 0.8.1)
36
+ hashie (>= 1.2)
37
+ multi_json (~> 1.4)
38
+ nokogiri (~> 1.5.2)
39
+ oauth2
40
+ hashie (2.0.5)
41
+ highline (1.6.19)
42
+ httpauth (0.2.0)
43
+ i18n (0.6.5)
44
+ jdbc-postgres (9.2.1002.1)
45
+ jeweler (1.8.7)
46
+ builder
47
+ bundler (~> 1.0)
48
+ git (>= 1.2.5)
49
+ github_api (= 0.10.1)
50
+ highline (>= 1.6.15)
51
+ nokogiri (= 1.5.10)
52
+ rake
53
+ rdoc
54
+ json (1.8.0)
55
+ json (1.8.0-java)
56
+ jwt (0.1.8)
57
+ multi_json (>= 1.5)
58
+ minitest (4.7.5)
59
+ multi_json (1.8.0)
60
+ multi_xml (0.5.5)
61
+ multipart-post (1.2.0)
62
+ nokogiri (1.5.10)
63
+ nokogiri (1.5.10-java)
64
+ oauth2 (0.9.2)
65
+ faraday (~> 0.8)
66
+ httpauth (~> 0.2)
67
+ jwt (~> 0.1.4)
68
+ multi_json (~> 1.0)
69
+ multi_xml (~> 0.5)
70
+ rack (~> 1.2)
71
+ pg (0.17.0)
72
+ rack (1.5.2)
73
+ rake (10.1.0)
74
+ rdoc (3.12.2)
75
+ json (~> 1.4)
76
+ rspec (2.8.0)
77
+ rspec-core (~> 2.8.0)
78
+ rspec-expectations (~> 2.8.0)
79
+ rspec-mocks (~> 2.8.0)
80
+ rspec-core (2.8.0)
81
+ rspec-expectations (2.8.0)
82
+ diff-lcs (~> 1.1.2)
83
+ rspec-mocks (2.8.0)
84
+ thread_safe (0.1.3)
85
+ atomic
86
+ thread_safe (0.1.3-java)
87
+ atomic
88
+ tzinfo (0.3.37)
89
+
90
+ PLATFORMS
91
+ java
92
+ ruby
93
+
94
+ DEPENDENCIES
95
+ activerecord (>= 3)
96
+ activerecord-jdbcpostgresql-adapter
97
+ bundler
98
+ jeweler
99
+ pg
100
+ rdoc
101
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 John Wulff
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # ActiveRecord Recursive Tree Scopes
2
+
3
+ [![Build Status](https://travis-ci.org/jwulff/activerecord-recursive_tree_scopes.png?branch=master)](https://travis-ci.org/jwulff/activerecord-recursive_tree_scopes)
4
+ [![Code Climate](https://codeclimate.com/github/jwulff/activerecord-recursive_tree_scopes.png)](https://codeclimate.com/github/jwulff/activerecord-recursive_tree_scopes)
5
+
6
+ Using ActiveRecord scopes, recursively traverse trees using a **single SQL
7
+ query**.
8
+
9
+ Let's say you've got an ActiveRecord model `Employee` with attributes `id`,
10
+ `name`, and `manager_id`. Using stock belongs_to and has_many relations it's
11
+ easy to query for an `Employee`'s manager and directly managed `Employee`'s.
12
+
13
+ ```ruby
14
+ class Employee < ActiveRecord::Base
15
+ belongs_to :manager, class_name: 'Employee'
16
+ has_many :directly_managed, class_name: 'Employee', foreign_key: :manager_id
17
+ ...
18
+ ```
19
+
20
+ **ActiveRecord Recursive Tree Scopes** provides two scopes. These scopes,
21
+ using a **single SQL query**, match all ancestors or descendants for a record
22
+ in a tree.
23
+
24
+ ```ruby
25
+ ...
26
+ has_ancestors :managers, key: :manager_id
27
+ has_descendants :managed, key: :manager_id
28
+ end
29
+ ```
30
+
31
+ ## A Single Query
32
+
33
+ Yep, a single query. Thanks to PostgreSQL's [`WITH RECURSIVE`](http://www.postgresql.org/docs/9.2/static/queries-with.html)
34
+ it's possible to recursively match records in a single query.
35
+
36
+ Using the model above as an example, let's say you've got an Employee with an
37
+ `id` of 42. Here's the SQL that would be generated for `employee.managed`
38
+ ```sql
39
+ SELECT "employees".*
40
+ FROM "employees"
41
+ WHERE (
42
+ employees.id IN (
43
+ WITH RECURSIVE descendants_search(id, path) AS (
44
+ SELECT id, ARRAY[id]
45
+ FROM employees
46
+ WHERE id = 42
47
+ UNION ALL
48
+ SELECT employees.id, path || employees.id
49
+ FROM descendants_search
50
+ JOIN employees
51
+ ON employees.manager_id = descendants_search.id
52
+ WHERE NOT employees.id = ANY(path)
53
+ )
54
+ SELECT id
55
+ FROM descendants_search
56
+ WHERE id != 42
57
+ ORDER BY path
58
+ )
59
+ )
60
+ ORDER BY employees.id
61
+ ```
62
+
63
+
64
+ ## Friendly
65
+
66
+ Go ahead, chain away:
67
+ ```ruby
68
+ employee.managers.where(name: 'Bob').exists?
69
+ ```
70
+ ```sql
71
+ SELECT "employees".*
72
+ FROM "employees"
73
+ WHERE
74
+ "employees"."name" = 'Bob' AND
75
+ (
76
+ employees.id IN (
77
+ WITH RECURSIVE descendants_search(id, path) AS (
78
+ SELECT id, ARRAY[id]
79
+ FROM employees
80
+ WHERE id = 42
81
+ UNION ALL
82
+ SELECT employees.id, path || employees.id
83
+ FROM descendants_search
84
+ JOIN employees
85
+ ON employees.manager_id = descendants_search.id
86
+ WHERE NOT employees.id = ANY(path)
87
+ )
88
+ SELECT id
89
+ FROM descendants_search
90
+ WHERE id != 42
91
+ ORDER BY path
92
+ )
93
+ )
94
+ ORDER BY employees.id
95
+ ```
96
+
97
+
98
+ ## Requirements
99
+ * ActiveRecord >= 3.0.0
100
+ * PostgreSQL >= 8.4
101
+
102
+
103
+ ## Installation
104
+
105
+ Add `gem 'activerecord-recursive_tree_scopes'` to your Gemfile.
106
+
107
+
108
+ ## Thanks
109
+
110
+ Thanks to [Joshua Davey](https://github.com/jgdavey) who's
111
+ [blog post](http://hashrocket.com/blog/posts/recursive-sql-in-activerecord)
112
+ inspired this gem.
113
+
114
+
115
+ ## Copyright
116
+
117
+ Copyright (c) 2013 John Wulff. See LICENSE.txt for
118
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "activerecord-recursive_tree_scopes"
18
+ gem.homepage = "http://github.com/jwulff/activerecord-recursive_tree_scopes"
19
+ gem.license = "MIT"
20
+ gem.summary = "Using an ActiveRecord scope, recursively query trees."
21
+ gem.description = "Using an ActiveRecord scope, recursively query trees."
22
+ gem.email = "johnw@orcasnet.com"
23
+ gem.authors = ["John Wulff"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "activerecord-recursive_tree_scopes #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "activerecord-recursive_tree_scopes"
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Wulff"]
12
+ s.date = "2013-10-01"
13
+ s.description = "Using an ActiveRecord scope, recursively query trees."
14
+ s.email = "johnw@orcasnet.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".travis.yml",
23
+ ".travis/activerecord-3.0.gemfile",
24
+ ".travis/activerecord-3.1.gemfile",
25
+ ".travis/activerecord-3.2.gemfile",
26
+ ".travis/activerecord-4.0.gemfile",
27
+ "Gemfile",
28
+ "Gemfile.lock",
29
+ "LICENSE.txt",
30
+ "README.md",
31
+ "Rakefile",
32
+ "VERSION",
33
+ "activerecord-recursive_tree_scopes.gemspec",
34
+ "lib/activerecord-recursive_tree_scopes.rb",
35
+ "spec/activerecord-recursive_tree_scopes_spec.rb",
36
+ "spec/database.yml",
37
+ "spec/database.yml.travis",
38
+ "spec/spec_helper.rb",
39
+ "spec/support/create_schema.rb",
40
+ "spec/support/employee.rb"
41
+ ]
42
+ s.homepage = "http://github.com/jwulff/activerecord-recursive_tree_scopes"
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = "2.0.6"
46
+ s.summary = "Using an ActiveRecord scope, recursively query trees."
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 4
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<activerecord>, [">= 3"])
53
+ s.add_development_dependency(%q<rspec>, [">= 0"])
54
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
55
+ s.add_development_dependency(%q<bundler>, [">= 0"])
56
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<activerecord>, [">= 3"])
59
+ s.add_dependency(%q<rspec>, [">= 0"])
60
+ s.add_dependency(%q<rdoc>, [">= 0"])
61
+ s.add_dependency(%q<bundler>, [">= 0"])
62
+ s.add_dependency(%q<jeweler>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<activerecord>, [">= 3"])
66
+ s.add_dependency(%q<rspec>, [">= 0"])
67
+ s.add_dependency(%q<rdoc>, [">= 0"])
68
+ s.add_dependency(%q<bundler>, [">= 0"])
69
+ s.add_dependency(%q<jeweler>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,84 @@
1
+ module RecursiveTreeScopes
2
+ module ModelMixin
3
+ def has_ancestors(ancestors_name = :ancestors, options = {})
4
+ options[:key] ||= :parent_id
5
+
6
+ scope ancestors_name, lambda{ |record|
7
+ RecursiveTreeScopes::Scopes.ancestors_for(record, options[:key])
8
+ }
9
+
10
+ class_eval <<-EOF
11
+ def #{ancestors_name}
12
+ self.class.#{ancestors_name}(self)
13
+ end
14
+ EOF
15
+ end
16
+
17
+ def has_descendants(descendants_name = :descendants, options = {})
18
+ options[:key] ||= :parent_id
19
+
20
+ scope descendants_name, lambda{ |record|
21
+ RecursiveTreeScopes::Scopes.descendants_for(record, options[:key])
22
+ }
23
+
24
+ class_eval <<-EOF
25
+ def #{descendants_name}
26
+ self.class.#{descendants_name}(self)
27
+ end
28
+ EOF
29
+ end
30
+ end
31
+
32
+ class Scopes
33
+ class << self
34
+ def ancestors_for(instance, key)
35
+ instance.class.where("#{instance.class.table_name}.id IN (#{ancestors_sql_for instance, key})").order("#{instance.class.table_name}.id")
36
+ end
37
+
38
+ def ancestors_sql_for(instance, key)
39
+ tree_sql = <<-SQL
40
+ WITH RECURSIVE ancestor_search(id, #{key}, path) AS (
41
+ SELECT id, #{key}, ARRAY[id]
42
+ FROM #{instance.class.table_name}
43
+ WHERE id = #{instance.id}
44
+ UNION ALL
45
+ SELECT #{instance.class.table_name}.id, #{instance.class.table_name}.#{key}, path || #{instance.class.table_name}.id
46
+ FROM #{instance.class.table_name}, ancestor_search
47
+ WHERE ancestor_search.#{key} = #{instance.class.table_name}.id
48
+ )
49
+ SELECT id
50
+ FROM ancestor_search
51
+ WHERE id != #{instance.id}
52
+ ORDER BY path
53
+ SQL
54
+ tree_sql.gsub(/\s{2,}/, ' ')
55
+ end
56
+
57
+ def descendants_for(instance, key)
58
+ instance.class.where("#{instance.class.table_name}.id IN (#{descendants_sql_for instance, key})").order("#{instance.class.table_name}.id")
59
+ end
60
+
61
+ def descendants_sql_for(instance, key)
62
+ tree_sql = <<-SQL
63
+ WITH RECURSIVE descendants_search(id, path) AS (
64
+ SELECT id, ARRAY[id]
65
+ FROM #{instance.class.table_name}
66
+ WHERE id = #{instance.id}
67
+ UNION ALL
68
+ SELECT #{instance.class.table_name}.id, path || #{instance.class.table_name}.id
69
+ FROM descendants_search
70
+ JOIN #{instance.class.table_name} ON #{instance.class.table_name}.#{key} = descendants_search.id
71
+ WHERE NOT #{instance.class.table_name}.id = ANY(path)
72
+ )
73
+ SELECT id
74
+ FROM descendants_search
75
+ WHERE id != #{instance.id}
76
+ ORDER BY path
77
+ SQL
78
+ tree_sql.gsub(/\s{2,}/, ' ')
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ ActiveRecord::Base.extend RecursiveTreeScopes::ModelMixin
@@ -0,0 +1,112 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActiverecordRecursiveTreeScopes" do
4
+ let!(:alonso) { Employee.create! name: 'Alonso' }
5
+ let!(:alfred) { Employee.create! name: 'Alfred' }
6
+ let!(:barry) { Employee.create! name: 'Barry', manager: alfred }
7
+ let!(:bob) { Employee.create! name: 'Bob', manager: alfred }
8
+ let!(:charles) { Employee.create! name: 'Charles', manager: barry }
9
+ let!(:cameron) { Employee.create! name: 'Cameron', manager: barry }
10
+ let!(:carl) { Employee.create! name: 'Carl', manager: barry }
11
+ let!(:dave) { Employee.create! name: 'Dave', manager: carl }
12
+ let!(:daryl) { Employee.create! name: 'Daryl', manager: charles }
13
+ let!(:dick) { Employee.create! name: 'Dick', manager: charles }
14
+ let!(:edward) { Employee.create! name: 'Edward', manager: dick }
15
+ let!(:frank) { Employee.create! name: 'Frank', manager: edward }
16
+
17
+ describe 'Alonso' do
18
+ subject { alonso }
19
+ its(:manager) { should be_nil }
20
+ its(:directly_managed) { should == [] }
21
+ its(:managers) { should == [] }
22
+ its(:managed) { should == [] }
23
+ end
24
+
25
+ describe 'Alfred' do
26
+ subject { alfred }
27
+ its(:manager) { should be_nil }
28
+ its(:directly_managed) { should == [ barry, bob ] }
29
+ its(:managers) { should == [] }
30
+ its(:managed) { should == [ barry, bob, charles, cameron, carl, dave, daryl, dick, edward, frank ] }
31
+ end
32
+
33
+ describe 'Barry' do
34
+ subject { barry }
35
+ its(:manager) { should == alfred }
36
+ its(:directly_managed) { should == [ charles, cameron, carl ] }
37
+ its(:managers) { should == [ alfred ] }
38
+ its(:managed) { should == [ charles, cameron, carl, dave, daryl, dick, edward, frank ] }
39
+ end
40
+
41
+ describe 'Bob' do
42
+ subject { bob }
43
+ its(:manager) { should == alfred }
44
+ its(:directly_managed) { should == [] }
45
+ its(:managers) { should == [ alfred ] }
46
+ its(:managed) { should == [] }
47
+ end
48
+
49
+ describe 'Charles' do
50
+ subject { charles }
51
+ its(:manager) { should == barry }
52
+ its(:directly_managed) { should == [ daryl, dick ] }
53
+ its(:managers) { should == [ alfred, barry ] }
54
+ its(:managed) { should == [ daryl, dick, edward, frank ] }
55
+ end
56
+
57
+ describe 'Cameron' do
58
+ subject { cameron }
59
+ its(:manager) { should == barry }
60
+ its(:directly_managed) { should == [] }
61
+ its(:managers) { should == [ alfred, barry ] }
62
+ its(:managed) { should == [] }
63
+ end
64
+
65
+ describe 'Carl' do
66
+ subject { carl }
67
+ its(:manager) { should == barry }
68
+ its(:directly_managed) { should == [ dave ] }
69
+ its(:managers) { should == [ alfred, barry ] }
70
+ its(:managed) { should == [ dave ] }
71
+ end
72
+
73
+ describe 'Dave' do
74
+ subject { dave }
75
+ its(:manager) { should == carl }
76
+ its(:directly_managed) { should == [] }
77
+ its(:managers) { should == [ alfred, barry, carl ] }
78
+ its(:managed) { should == [] }
79
+ end
80
+
81
+ describe 'Daryl' do
82
+ subject { daryl }
83
+ its(:manager) { should == charles }
84
+ its(:directly_managed) { should == [] }
85
+ its(:managers) { should == [ alfred, barry, charles ] }
86
+ its(:managed) { should == [] }
87
+ end
88
+
89
+ describe 'Dick' do
90
+ subject { dick }
91
+ its(:manager) { should == charles }
92
+ its(:directly_managed) { should == [ edward ] }
93
+ its(:managers) { should == [ alfred, barry, charles ] }
94
+ its(:managed) { should == [ edward, frank ] }
95
+ end
96
+
97
+ describe 'Edward' do
98
+ subject { edward }
99
+ its(:manager) { should == dick }
100
+ its(:directly_managed) { should == [ frank ] }
101
+ its(:managers) { should == [ alfred, barry, charles, dick ] }
102
+ its(:managed) { should == [ frank ] }
103
+ end
104
+
105
+ describe 'Frank' do
106
+ subject { frank }
107
+ its(:manager) { should == edward }
108
+ its(:directly_managed) { should == [] }
109
+ its(:managers) { should == [ alfred, barry, charles, dick, edward ] }
110
+ its(:managed) { should == [] }
111
+ end
112
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,5 @@
1
+ adapter: postgresql
2
+ database: activerecord-recursive_tree_relations-test
3
+ username: activerecord-recursive_tree_relations-test
4
+ password:
5
+ host: localhost
@@ -0,0 +1,5 @@
1
+ adapter: postgresql
2
+ username: postgres
3
+ password:
4
+ database: activerecordrecursivetreerelationstest
5
+ min_messages: ERROR
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_record'
5
+ require 'activerecord-recursive_tree_scopes'
6
+
7
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
8
+
9
+ ActiveRecord::Base.establish_connection(YAML.load(File.read(File.join(File.dirname(__FILE__), 'database.yml'))))
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:suite) do
13
+ CreateSchema.suppress_messages{ CreateSchema.migrate(:up) }
14
+ end
15
+
16
+ config.after(:suite) do
17
+ CreateSchema.suppress_messages{ CreateSchema.migrate(:down) }
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ class CreateSchema < ActiveRecord::Migration
2
+ def up
3
+ create_table :employees do |t|
4
+ t.string :name
5
+ t.integer :manager_id
6
+ end
7
+
8
+ execute "ALTER TABLE employees ADD CONSTRAINT employees_fk_manager_id FOREIGN KEY (manager_id) REFERENCES employees (id)"
9
+ end
10
+
11
+ def down
12
+ drop_table :employees
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ class Employee < ActiveRecord::Base
2
+ belongs_to :manager, class_name: 'Employee'
3
+ has_many :directly_managed, class_name: 'Employee', foreign_key: :manager_id
4
+
5
+ has_ancestors :managers, key: :manager_id
6
+ has_descendants :managed, key: :manager_id
7
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-recursive_tree_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - John Wulff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Using an ActiveRecord scope, recursively query trees.
84
+ email: johnw@orcasnet.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.md
90
+ files:
91
+ - .document
92
+ - .rspec
93
+ - .travis.yml
94
+ - .travis/activerecord-3.0.gemfile
95
+ - .travis/activerecord-3.1.gemfile
96
+ - .travis/activerecord-3.2.gemfile
97
+ - .travis/activerecord-4.0.gemfile
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - LICENSE.txt
101
+ - README.md
102
+ - Rakefile
103
+ - VERSION
104
+ - activerecord-recursive_tree_scopes.gemspec
105
+ - lib/activerecord-recursive_tree_scopes.rb
106
+ - spec/activerecord-recursive_tree_scopes_spec.rb
107
+ - spec/database.yml
108
+ - spec/database.yml.travis
109
+ - spec/spec_helper.rb
110
+ - spec/support/create_schema.rb
111
+ - spec/support/employee.rb
112
+ homepage: http://github.com/jwulff/activerecord-recursive_tree_scopes
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.0.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Using an ActiveRecord scope, recursively query trees.
136
+ test_files: []