simple_tree 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_tree.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
@@ -0,0 +1,63 @@
1
+ require "simple_tree/version"
2
+ require "simple_tree/railtie" if defined? Rails
3
+
4
+ module SimpleTree
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+ module ClassMethods
9
+ def acts_as_simple_tree(options = {})
10
+ configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
11
+ configuration.update(options) if options.is_a?(Hash)
12
+
13
+ belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
14
+ has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
15
+
16
+ self.send(:include, SimpleTree::InstanceMethods)
17
+ end
18
+ end
19
+ module InstanceMethods
20
+
21
+ # Returns list of ancestors, starting from parent until root.
22
+ #
23
+ # subchild1.ancestors # => [child1, root]
24
+ def ancestors
25
+ node, nodes = self,[]
26
+ nodes << node = node.parent while node.parent
27
+ nodes
28
+ end
29
+
30
+ # Returns the root node of the tree.
31
+ def root
32
+ node = self
33
+ node = node.parent while node.parent
34
+ node
35
+ end
36
+
37
+ # Returns all siblings of the current node.
38
+ #
39
+ # subchild1.siblings # => [subchild2]
40
+ def siblings
41
+ self_and_siblings - [self]
42
+ end
43
+
44
+ # Returns all siblings and a reference to the current node.
45
+ #
46
+ # subchild1.self_and_siblings # => [subchild1, subchild2]
47
+ def self_and_siblings
48
+ parent ? parent.children : []
49
+ end
50
+
51
+ def descendants
52
+ return [] if children.empty?
53
+ nodes = children
54
+ children.each {|c| nodes + c.descendants}
55
+ nodes
56
+ end
57
+
58
+ def move_to_child_of(parent)
59
+ self.parent = parent
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,9 @@
1
+ module SimpleTree
2
+ class Railtie < Rails::Railtie
3
+ initializer 'simple_tree' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend SimpleTree::ClassMethods
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleTree
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "simple_tree/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "simple_tree"
7
+ s.version = SimpleTree::VERSION
8
+ s.authors = ["yinghai"]
9
+ s.email = ["yinghai.zhao@mosaic.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{simple tree gem}
12
+ s.description = %q{simple tree gem}
13
+
14
+ s.rubyforge_project = "simple_tree"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency 'with_model'
24
+ s.add_development_dependency 'rails', '~> 3.0.9'
25
+ s.add_development_dependency 'sqlite3'
26
+ # s.add_runtime_dependency "rest-client"
27
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe SimpleTree do
5
+ with_model :Test do
6
+ table do |t|
7
+ t.integer :parent_id
8
+ t.string :name
9
+ end
10
+ model do
11
+ include SimpleTree
12
+ acts_as_simple_tree
13
+ end
14
+ end
15
+
16
+ before do
17
+ @root = Test.create :name => 'root'
18
+ @c1 = Test.create :name => 'child1', :parent => @root
19
+ @c2 = Test.create :name => 'child1', :parent => @root
20
+ @gc1a = Test.create :name => 'child1', :parent => @c1
21
+ @gc2a = Test.create :name => 'child1', :parent => @c2
22
+ end
23
+
24
+ describe "#children" do
25
+ it "should return all children" do
26
+ @root.children.should == [@c1,@c2]
27
+ end
28
+ end
29
+
30
+ describe "#ancestors" do
31
+ it "should return all ancestors" do
32
+ @gc1a.ancestors.should == [@c1,@root]
33
+ end
34
+ end
35
+ describe "#root" do
36
+ it "should return root for descendants" do
37
+ @gc1a.root.should == @root
38
+ end
39
+ it "should return itself for root" do
40
+ @root.root.should == @root
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ require 'simple_tree'
2
+ require 'active_record'
3
+ require 'with_model'
4
+
5
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
6
+
7
+ RSpec.configure do |config|
8
+ config.extend WithModel
9
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - yinghai
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70330458747320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70330458747320
25
+ - !ruby/object:Gem::Dependency
26
+ name: with_model
27
+ requirement: &70330458746900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70330458746900
36
+ - !ruby/object:Gem::Dependency
37
+ name: rails
38
+ requirement: &70330458746400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.9
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70330458746400
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: &70330458745980 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70330458745980
58
+ description: simple tree gem
59
+ email:
60
+ - yinghai.zhao@mosaic.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - CHANGELOG.md
68
+ - Gemfile
69
+ - Rakefile
70
+ - lib/simple_tree.rb
71
+ - lib/simple_tree/railtie.rb
72
+ - lib/simple_tree/version.rb
73
+ - simple_tree.gemspec
74
+ - spec/simple_tree_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: ''
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project: simple_tree
96
+ rubygems_version: 1.8.17
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: simple tree gem
100
+ test_files:
101
+ - spec/simple_tree_spec.rb
102
+ - spec/spec_helper.rb