Module: Customize::Inherited
- Defined in:
- lib/customize/inherited.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #ascent_ids ⇒ Object
- #ascents(options = {:include=>false}) ⇒ Object
- #children ⇒ Object
- #descent_ids ⇒ Object
- #descents ⇒ Object
- #inherit(parent) ⇒ Object
- #inherit_association(name, options = {:include=>false}) ⇒ Object
- #label ⇒ Object
- #parent ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/customize/inherited.rb', line 3 def self.included base base.extend ClassMethods base.has_one :inherit_node, :class_name=>Customize::InheritNode.name, :as=>:node, :dependent=>:destroy base.scope :ascents_for, lambda { |object, ={:include=>false}| ntn = Customize::InheritNode.table_name node = object.inherit_node condition_string = if [:include] "#{ntn}.left <= ? and #{ntn}.right >= ?" else "#{ntn}.left < ? and #{ntn}.right > ?" end base.joins(:inherit_node).where(condition_string, node.left, node.right) } base.scope :descents_for, lambda { |object, ={:include=>false}| ntn = Customize::InheritNode.table_name node = object.inherit_node condition_string = if [:include] "#{ntn}.left >= ? and #{ntn}.right <= ?" else "#{ntn}.left > ? and #{ntn}.right < ?" end base.joins(:inherit_node).where(condition_string, node.left, node.right) } base.after_create { |object| object.create_inherit_node } base.delegate :leaf?, :to=>:inherit_node base.before_destroy { |object| raise 'object should not have children when destroy' if object.inherit_node.children.size > 0 } end |
Instance Method Details
#ascent_ids ⇒ Object
106 107 108 |
# File 'lib/customize/inherited.rb', line 106 def ascent_ids ascents.map(&:id) end |
#ascents(options = {:include=>false}) ⇒ Object
89 90 91 92 |
# File 'lib/customize/inherited.rb', line 89 def ascents ={:include=>false} return [] if new_record? self.class.ascents_for self, end |
#children ⇒ Object
69 70 71 |
# File 'lib/customize/inherited.rb', line 69 def children inherit_node.children.collect(&:node) end |
#descent_ids ⇒ Object
110 111 112 |
# File 'lib/customize/inherited.rb', line 110 def descent_ids descents.map(&:id) end |
#descents ⇒ Object
101 102 103 104 |
# File 'lib/customize/inherited.rb', line 101 def descents return [] if new_record? self.class.descents_for self end |
#inherit(parent) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/customize/inherited.rb', line 77 def inherit parent raise 'should be save first' if self.new_record? raise 'should be same class' if self.class != parent.class raise 'should not be self' if self.id == parent.id raise 'should not inherit descents' if self.descent_ids.include? parent.id raise 'should not inherit parent' if self.parent.try(:id) == parent.id self.class.transaction do inherit_node.destroy create_inherit_node(:parent_id=>parent.inherit_node.id) end end |
#inherit_association(name, options = {:include=>false}) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/customize/inherited.rb', line 94 def inherit_association name, ={:include=>false} a = association(name) sql = ascents().joins(name).select("#{a.aliased_table_name}.#{a.klass.primary_key}").to_sql a.klass.from("(#{sql}) as subquery, #{a.klass.table_name}"). where("#{a.klass.table_name}.#{a.klass.primary_key} = subquery.#{a.klass.primary_key}") end |
#label ⇒ Object
73 74 75 |
# File 'lib/customize/inherited.rb', line 73 def label self.to_s end |
#parent ⇒ Object
65 66 67 |
# File 'lib/customize/inherited.rb', line 65 def parent inherit_node.parent_node.try(:node) end |