Skip to content
This repository

redefining base_class #86

Merged
merged 1 commit into from about 1 year ago

2 participants

NOX73 StefanH
NOX73

redefining the standard RoR method http://apidock.com/rails/ActiveRecord/Base/base_class/class causes conflicts with other gems, for example acts-as-taggable-on uses this method to define the tag type and redefining the method changes its work logic

StefanH StefanH merged commit 4e57f32 into from
StefanH StefanH closed this
StefanH
Collaborator
StefanH commented

Tnx! great fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 21, 2011
NOX73 NOX73 base_class replace ancestry_base_class 4e57f32
This page is out of date. Refresh to see the latest.
20 lib/ancestry/class_methods.rb
@@ -2,12 +2,12 @@ module Ancestry
2 2 module ClassMethods
3 3 # Fetch tree node if necessary
4 4 def to_node object
5   - if object.is_a?(self.base_class) then object else find(object) end
  5 + if object.is_a?(self.ancestry_base_class) then object else find(object) end
6 6 end
7 7
8 8 # Scope on relative depth options
9 9 def scope_depth depth_options, depth
10   - depth_options.inject(self.base_class) do |scope, option|
  10 + depth_options.inject(self.ancestry_base_class) do |scope, option|
11 11 scope_name, relative_depth = option
12 12 if [:before_depth, :to_depth, :at_depth, :from_depth, :after_depth].include? scope_name
13 13 scope.send scope_name, depth + relative_depth
@@ -31,9 +31,9 @@ def orphan_strategy= orphan_strategy
31 31 def arrange options = {}
32 32 scope =
33 33 if options[:order].nil?
34   - self.base_class.ordered_by_ancestry
  34 + self.ancestry_base_class.ordered_by_ancestry
35 35 else
36   - self.base_class.ordered_by_ancestry_and options.delete(:order)
  36 + self.ancestry_base_class.ordered_by_ancestry_and options.delete(:order)
37 37 end
38 38 # Get all nodes ordered by ancestry and start sorting them into an empty hash
39 39 arrange_nodes scope.all(options)
@@ -74,7 +74,7 @@ def check_ancestry_integrity! options = {}
74 74 parents = {}
75 75 exceptions = [] if options[:report] == :list
76 76 # For each node ...
77   - self.base_class.find_each do |node|
  77 + self.ancestry_base_class.find_each do |node|
78 78 begin
79 79 # ... check validity of ancestry column
80 80 if !node.valid? and !node.errors[node.class.ancestry_column].blank?
@@ -108,9 +108,9 @@ def check_ancestry_integrity! options = {}
108 108 def restore_ancestry_integrity!
109 109 parents = {}
110 110 # Wrap the whole thing in a transaction ...
111   - self.base_class.transaction do
  111 + self.ancestry_base_class.transaction do
112 112 # For each node ...
113   - self.base_class.find_each do |node|
  113 + self.ancestry_base_class.find_each do |node|
114 114 # ... set its ancestry to nil if invalid
115 115 if !node.valid? and !node.errors[node.class.ancestry_column].blank?
116 116 node.without_ancestry_callbacks do
@@ -128,7 +128,7 @@ def restore_ancestry_integrity!
128 128 parents[node.id] = nil if parent == node.id
129 129 end
130 130 # For each node ...
131   - self.base_class.find_each do |node|
  131 + self.ancestry_base_class.find_each do |node|
132 132 # ... rebuild ancestry from parents array
133 133 ancestry, parent = nil, parents[node.id]
134 134 until parent.nil?
@@ -143,7 +143,7 @@ def restore_ancestry_integrity!
143 143
144 144 # Build ancestry from parent id's for migration purposes
145 145 def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
146   - self.base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
  146 + self.ancestry_base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
147 147 node.without_ancestry_callbacks do
148 148 node.update_attribute ancestry_column, ancestry
149 149 end
@@ -154,7 +154,7 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
154 154 # Rebuild depth cache if it got corrupted or if depth caching was just turned on
155 155 def rebuild_depth_cache!
156 156 raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
157   - self.base_class.find_each do |node|
  157 + self.ancestry_base_class.find_each do |node|
158 158 node.update_attribute depth_cache_column, node.depth
159 159 end
160 160 end
4 lib/ancestry/has_ancestry.rb
@@ -27,8 +27,8 @@ def has_ancestry options = {}
27 27 self.orphan_strategy = options[:orphan_strategy] || :destroy
28 28
29 29 # Save self as base class (for STI)
30   - cattr_accessor :base_class
31   - self.base_class = self
  30 + cattr_accessor :ancestry_base_class
  31 + self.ancestry_base_class = self
32 32
33 33 # Validate format of ancestry column value
34 34 primary_key_format = options[:primary_key_format] || /[0-9]+/
58 lib/ancestry/instance_methods.rb
@@ -10,13 +10,13 @@ def update_descendants_with_new_ancestry
10 10 # Skip this if callbacks are disabled
11 11 unless ancestry_callbacks_disabled?
12 12 # If node is valid, not a new record and ancestry was updated ...
13   - if changed.include?(self.base_class.ancestry_column.to_s) && !new_record? && valid?
  13 + if changed.include?(self.ancestry_base_class.ancestry_column.to_s) && !new_record? && valid?
14 14 # ... for each descendant ...
15 15 descendants.each do |descendant|
16 16 # ... replace old ancestry with new ancestry
17 17 descendant.without_ancestry_callbacks do
18 18 descendant.update_attribute(
19   - self.base_class.ancestry_column,
  19 + self.ancestry_base_class.ancestry_column,
20 20 descendant.read_attribute(descendant.class.ancestry_column).gsub(
21 21 /^#{self.child_ancestry}/,
22 22 if read_attribute(self.class.ancestry_column).blank? then id.to_s else "#{read_attribute self.class.ancestry_column }/#{id}" end
@@ -35,21 +35,21 @@ def apply_orphan_strategy
35 35 # If this isn't a new record ...
36 36 unless new_record?
37 37 # ... make al children root if orphan strategy is rootify
38   - if self.base_class.orphan_strategy == :rootify
  38 + if self.ancestry_base_class.orphan_strategy == :rootify
39 39 descendants.each do |descendant|
40 40 descendant.without_ancestry_callbacks do
41 41 descendant.update_attribute descendant.class.ancestry_column, (if descendant.ancestry == child_ancestry then nil else descendant.ancestry.gsub(/^#{child_ancestry}\//, '') end)
42 42 end
43 43 end
44 44 # ... destroy all descendants if orphan strategy is destroy
45   - elsif self.base_class.orphan_strategy == :destroy
  45 + elsif self.ancestry_base_class.orphan_strategy == :destroy
46 46 descendants.all.each do |descendant|
47 47 descendant.without_ancestry_callbacks do
48 48 descendant.destroy
49 49 end
50 50 end
51 51 # ... throw an exception if it has children and orphan strategy is restrict
52   - elsif self.base_class.orphan_strategy == :restrict
  52 + elsif self.ancestry_base_class.orphan_strategy == :restrict
53 53 raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
54 54 end
55 55 end
@@ -61,20 +61,20 @@ def child_ancestry
61 61 # New records cannot have children
62 62 raise Ancestry::AncestryException.new('No child ancestry for new record. Save record before performing tree operations.') if new_record?
63 63
64   - if self.send("#{self.base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.base_class.ancestry_column}_was"}/#{id}" end
  64 + if self.send("#{self.ancestry_base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.ancestry_base_class.ancestry_column}_was"}/#{id}" end
65 65 end
66 66
67 67 # Ancestors
68 68 def ancestor_ids
69   - read_attribute(self.base_class.ancestry_column).to_s.split('/').map { |id| cast_primary_key(id) }
  69 + read_attribute(self.ancestry_base_class.ancestry_column).to_s.split('/').map { |id| cast_primary_key(id) }
70 70 end
71 71
72 72 def ancestor_conditions
73   - {self.base_class.primary_key => ancestor_ids}
  73 + {self.ancestry_base_class.primary_key => ancestor_ids}
74 74 end
75 75
76 76 def ancestors depth_options = {}
77   - self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => ancestor_conditions
  77 + self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => ancestor_conditions
78 78 end
79 79
80 80 def path_ids
@@ -82,11 +82,11 @@ def path_ids
82 82 end
83 83
84 84 def path_conditions
85   - {self.base_class.primary_key => path_ids}
  85 + {self.ancestry_base_class.primary_key => path_ids}
86 86 end
87 87
88 88 def path depth_options = {}
89   - self.base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => path_conditions
  89 + self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.scoped :conditions => path_conditions
90 90 end
91 91
92 92 def depth
@@ -94,16 +94,16 @@ def depth
94 94 end
95 95
96 96 def cache_depth
97   - write_attribute self.base_class.depth_cache_column, depth
  97 + write_attribute self.ancestry_base_class.depth_cache_column, depth
98 98 end
99 99
100 100 # Parent
101 101 def parent= parent
102   - write_attribute(self.base_class.ancestry_column, if parent.blank? then nil else parent.child_ancestry end)
  102 + write_attribute(self.ancestry_base_class.ancestry_column, if parent.blank? then nil else parent.child_ancestry end)
103 103 end
104 104
105 105 def parent_id= parent_id
106   - self.parent = if parent_id.blank? then nil else self.base_class.find(parent_id) end
  106 + self.parent = if parent_id.blank? then nil else self.ancestry_base_class.find(parent_id) end
107 107 end
108 108
109 109 def parent_id
@@ -111,7 +111,7 @@ def parent_id
111 111 end
112 112
113 113 def parent
114   - if parent_id.blank? then nil else self.base_class.find(parent_id) end
  114 + if parent_id.blank? then nil else self.ancestry_base_class.find(parent_id) end
115 115 end
116 116
117 117 # Root
@@ -120,24 +120,24 @@ def root_id
120 120 end
121 121
122 122 def root
123   - if root_id == id then self else self.base_class.find(root_id) end
  123 + if root_id == id then self else self.ancestry_base_class.find(root_id) end
124 124 end
125 125
126 126 def is_root?
127   - read_attribute(self.base_class.ancestry_column).blank?
  127 + read_attribute(self.ancestry_base_class.ancestry_column).blank?
128 128 end
129 129
130 130 # Children
131 131 def child_conditions
132   - {self.base_class.ancestry_column => child_ancestry}
  132 + {self.ancestry_base_class.ancestry_column => child_ancestry}
133 133 end
134 134
135 135 def children
136   - self.base_class.scoped :conditions => child_conditions
  136 + self.ancestry_base_class.scoped :conditions => child_conditions
137 137 end
138 138
139 139 def child_ids
140   - children.all(:select => self.base_class.primary_key).map(&self.base_class.primary_key.to_sym)
  140 + children.all(:select => self.ancestry_base_class.primary_key).map(&self.ancestry_base_class.primary_key.to_sym)
141 141 end
142 142
143 143 def has_children?
@@ -150,15 +150,15 @@ def is_childless?
150 150
151 151 # Siblings
152 152 def sibling_conditions
153   - {self.base_class.ancestry_column => read_attribute(self.base_class.ancestry_column)}
  153 + {self.ancestry_base_class.ancestry_column => read_attribute(self.ancestry_base_class.ancestry_column)}
154 154 end
155 155
156 156 def siblings
157   - self.base_class.scoped :conditions => sibling_conditions
  157 + self.ancestry_base_class.scoped :conditions => sibling_conditions
158 158 end
159 159
160 160 def sibling_ids
161   - siblings.all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
  161 + siblings.all(:select => self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
162 162 end
163 163
164 164 def has_siblings?
@@ -171,28 +171,28 @@ def is_only_child?
171 171
172 172 # Descendants
173 173 def descendant_conditions
174   - ["#{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
  174 + ["#{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} like ? or #{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
175 175 end
176 176
177 177 def descendants depth_options = {}
178   - self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => descendant_conditions
  178 + self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => descendant_conditions
179 179 end
180 180
181 181 def descendant_ids depth_options = {}
182   - descendants(depth_options).all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
  182 + descendants(depth_options).all(:select => self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
183 183 end
184 184
185 185 # Subtree
186 186 def subtree_conditions
187   - ["#{self.base_class.table_name}.#{self.base_class.primary_key} = ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
  187 + ["#{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.primary_key} = ? or #{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} like ? or #{self.ancestry_base_class.table_name}.#{self.ancestry_base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
188 188 end
189 189
190 190 def subtree depth_options = {}
191   - self.base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => subtree_conditions
  191 + self.ancestry_base_class.ordered_by_ancestry.scope_depth(depth_options, depth).scoped :conditions => subtree_conditions
192 192 end
193 193
194 194 def subtree_ids depth_options = {}
195   - subtree(depth_options).all(:select => self.base_class.primary_key).collect(&self.base_class.primary_key.to_sym)
  195 + subtree(depth_options).all(:select => self.ancestry_base_class.primary_key).collect(&self.ancestry_base_class.primary_key.to_sym)
196 196 end
197 197
198 198 # Callback disabling

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.