jcredding-is_tree 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = IsTree
2
+
3
+ == Description
4
+
5
+ Describe your gem here ...
6
+
7
+ == Installation
8
+
9
+ sudo gem install is_tree
10
+
11
+ == Usage
12
+
13
+ require 'is_tree'
14
+
15
+ == License
16
+
17
+ Copyright (c) <year> <copyright holders>
18
+
19
+ Permission is hereby granted, free of charge, to any person
20
+ obtaining a copy of this software and associated documentation
21
+ files (the "Software"), to deal in the Software without
22
+ restriction, including without limitation the rights to use,
23
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
24
+ copies of the Software, and to permit persons to whom the
25
+ Software is furnished to do so, subject to the following
26
+ conditions:
27
+
28
+ The above copyright notice and this permission notice shall be
29
+ included in all copies or substantial portions of the Software.
30
+
31
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
33
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
35
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
36
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
38
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/is_tree/version'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.name = 'is_tree'
9
+ s.version = IsTree::Version.to_s
10
+ s.has_rdoc = true
11
+ s.extra_rdoc_files = %w(README.rdoc)
12
+ s.rdoc_options = %w(--main README.rdoc)
13
+ s.summary = "gem version of acts_as_tree with some other features and tests"
14
+ s.author = 'Collin Redding'
15
+ s.email = 'jc.redding@gmail.com'
16
+ s.homepage = ''
17
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
18
+ # s.executables = ['is_tree']
19
+
20
+ s.add_dependency('rails', '~> 2.1.2')
21
+ end
22
+
23
+ Rake::GemPackageTask.new(spec) do |pkg|
24
+ pkg.gem_spec = spec
25
+ end
26
+
27
+ Rake::TestTask.new do |t|
28
+ t.libs << 'test'
29
+ t.test_files = FileList["test/**/*_test.rb"]
30
+ t.verbose = true
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+
36
+ Rcov::RcovTask.new(:coverage) do |t|
37
+ t.libs = ['test']
38
+ t.test_files = FileList["test/**/*_test.rb"]
39
+ t.verbose = true
40
+ t.rcov_opts = ['--text-report', "-x #{Gem.path}", '-x /Library/Ruby', '-x /usr/lib/ruby']
41
+ end
42
+
43
+ task :default => :coverage
44
+
45
+ rescue LoadError
46
+ warn "\n**** Install rcov (sudo gem install relevance-rcov) to get coverage stats ****\n"
47
+ task :default => :test
48
+ end
49
+
50
+
51
+ desc 'Generate the gemspec to serve this gem'
52
+ task :gemspec do
53
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
54
+ File.open(file, 'w') {|f| f << spec.to_ruby }
55
+ puts "Created gemspec: #{file}"
56
+ end
57
+
58
+ require 'cucumber'
59
+ require 'cucumber/rake/task'
60
+
61
+ Cucumber::Rake::Task.new(:features) do |t|
62
+ t.cucumber_opts = "test/features --format pretty"
63
+ end
data/lib/is_tree.rb ADDED
@@ -0,0 +1,5 @@
1
+ ["active_record/tree.rb"].each do |file|
2
+ require "is_tree/#{file}"
3
+ end
4
+
5
+ ActiveRecord::Base.send :include, IsTree::ActiveRecord::Tree
@@ -0,0 +1,73 @@
1
+ module IsTree
2
+ module ActiveRecord
3
+ module Tree
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def is_tree(options = {})
12
+ configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil}
13
+ configuration.merge!(options) if options.is_a?(Hash)
14
+
15
+ has_many_params = {:order => configuration[:order]}.merge(configuration[:dependent] ? {:dependent => configuration[:dependent]} : {})
16
+ association_params = {:class_name => self.name, :foreign_key => configuration[:foreign_key]}
17
+ root_scope_params = {:conditions => {configuration[:foreign_key] => nil}, :order => configuration[:order]}
18
+
19
+ belongs_to :parent, association_params.merge({:counter_cache => configuration[:counter_cache]})
20
+ has_many :children, association_params.merge(has_many_params)
21
+
22
+ named_scope :roots, root_scope_params
23
+ named_scope :with_parent, {:include => :parent}
24
+ named_scope :with_children, {:include => :children}
25
+
26
+ self.class_eval do
27
+ include IsTree::ActiveRecord::Tree::InstanceMethods
28
+
29
+ def self.root
30
+ self.roots.first
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ module InstanceMethods
39
+
40
+ def ancestors
41
+ node, nodes = self, []
42
+ nodes << node = node.parent while node.parent
43
+ nodes
44
+ end
45
+
46
+ # Returns the root node of the tree.
47
+ def root
48
+ node = self
49
+ node = node.parent while node.parent
50
+ node
51
+ end
52
+
53
+ def leaf?
54
+ self.children.empty?
55
+ end
56
+
57
+ def root?
58
+ self.parent.nil?
59
+ end
60
+
61
+ def siblings
62
+ self.self_and_siblings.reject{|node| node == self}
63
+ end
64
+
65
+ def self_and_siblings
66
+ self.parent ? self.parent.children : self.class.roots
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,13 @@
1
+ module IsTree
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 1
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcredding-is_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Collin Redding
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.1.2
24
+ version:
25
+ description:
26
+ email: jc.redding@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - README.rdoc
35
+ - Rakefile
36
+ - lib/is_tree
37
+ - lib/is_tree/active_record
38
+ - lib/is_tree/active_record/tree.rb
39
+ - lib/is_tree/version.rb
40
+ - lib/is_tree.rb
41
+ has_rdoc: false
42
+ homepage: ""
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.rdoc
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: gem version of acts_as_tree with some other features and tests
68
+ test_files: []
69
+