arboreal 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ module Arboreal
10
10
  def ancestor_ids
11
11
  ancestry_string.sub(/^-/, "").split("-").map { |x| x.to_i }
12
12
  end
13
-
13
+
14
14
  # return a scope matching all ancestors of this node
15
15
  def ancestors
16
16
  model_base_class.scoped(:conditions => ancestor_conditions, :order => :ancestry_string)
@@ -20,12 +20,12 @@ module Arboreal
20
20
  def descendants
21
21
  model_base_class.scoped(:conditions => descendant_conditions)
22
22
  end
23
-
23
+
24
24
  # return a scope matching all descendants of this node, AND the node itself
25
25
  def subtree
26
26
  model_base_class.scoped(:conditions => subtree_conditions)
27
27
  end
28
-
28
+
29
29
  # return a scope matching all siblings of this node (NOT including the node itself)
30
30
  def siblings
31
31
  model_base_class.scoped(:conditions => sibling_conditions)
@@ -35,17 +35,17 @@ module Arboreal
35
35
  def root
36
36
  ancestors.first || self
37
37
  end
38
-
38
+
39
39
  private
40
40
 
41
41
  def model_base_class
42
42
  self.class.base_class
43
43
  end
44
-
44
+
45
45
  def table_name
46
46
  self.class.table_name
47
47
  end
48
-
48
+
49
49
  def ancestor_conditions
50
50
  ["id in (?)", ancestor_ids]
51
51
  end
@@ -60,7 +60,7 @@ module Arboreal
60
60
  id, path_string + "%"
61
61
  ]
62
62
  end
63
-
63
+
64
64
  def sibling_conditions
65
65
  [
66
66
  "#{table_name}.id <> ? AND #{table_name}.parent_id = ?",
@@ -69,13 +69,14 @@ module Arboreal
69
69
  end
70
70
 
71
71
  def populate_ancestry_string
72
+ self.ancestry_string = nil if parent_id_changed?
72
73
  model_base_class.send(:with_exclusive_scope) do
73
- self.ancestry_string = parent ? parent.path_string : "-"
74
+ self.ancestry_string ||= parent ? parent.path_string : "-"
74
75
  end
75
76
  end
76
-
77
+
77
78
  def validate_parent_not_ancestor
78
- if self.id
79
+ if self.id
79
80
  if parent_id == self.id
80
81
  errors.add(:parent, "can't be the record itself")
81
82
  end
@@ -84,25 +85,25 @@ module Arboreal
84
85
  end
85
86
  end
86
87
  end
87
-
88
+
88
89
  def detect_ancestry_change
89
90
  if ancestry_string_changed? && !new_record?
90
91
  old_path_string = "#{ancestry_string_was}#{id}-"
91
92
  @ancestry_change = [old_path_string, path_string]
92
93
  end
93
94
  end
94
-
95
+
95
96
  def apply_ancestry_change_to_descendants
96
97
  if @ancestry_change
97
98
  old_ancestry_string, new_ancestry_string = *@ancestry_change
98
99
  connection.update(<<-SQL.squish)
99
- UPDATE #{table_name}
100
+ UPDATE #{table_name}
100
101
  SET ancestry_string = REPLACE(ancestry_string, '#{old_ancestry_string}', '#{new_ancestry_string}')
101
102
  WHERE ancestry_string LIKE '#{old_ancestry_string}%'
102
103
  SQL
103
104
  @ancestry_change = nil
104
105
  end
105
106
  end
106
-
107
+
107
108
  end
108
109
  end
@@ -1,3 +1,3 @@
1
1
  module Arboreal
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.1".freeze
3
3
  end
metadata CHANGED
@@ -1,38 +1,48 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arboreal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
4
  prerelease:
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mike Williams
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-02 00:00:00.000000000Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
16
- requirement: &70308116360600 !ruby/object:Gem::Requirement
15
+ type: :runtime
16
+ version_requirements: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: 2.3.0
22
- type: :runtime
22
+ name: activerecord
23
23
  prerelease: false
24
- version_requirements: *70308116360600
25
- - !ruby/object:Gem::Dependency
26
- name: activesupport
27
- requirement: &70308116360000 !ruby/object:Gem::Requirement
24
+ requirement: !ruby/object:Gem::Requirement
28
25
  none: false
29
26
  requirements:
30
27
  - - ! '>='
31
28
  - !ruby/object:Gem::Version
32
29
  version: 2.3.0
30
+ - !ruby/object:Gem::Dependency
33
31
  type: :runtime
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.3.0
38
+ name: activesupport
34
39
  prerelease: false
35
- version_requirements: *70308116360000
40
+ requirement: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
36
46
  description: ! 'Arboreal is yet another extension to ActiveRecord to support tree-shaped
37
47
  data structures.
38
48
 
@@ -90,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
100
  version: '0'
91
101
  requirements: []
92
102
  rubyforge_project:
93
- rubygems_version: 1.8.10
103
+ rubygems_version: 1.8.24
94
104
  signing_key:
95
105
  specification_version: 3
96
106
  summary: Efficient tree structures for ActiveRecord