|
| 1 | +require 'active_record' |
| 2 | + |
| 3 | +class ActiveRecord::Base |
| 4 | + |
| 5 | + attr_reader :reflection |
| 6 | + def self.has_children |
| 7 | + class << self |
| 8 | + self.class.instance_variable_set(:@cti,true) |
| 9 | + alias_method :child_find_by_sql, :find_by_sql |
| 10 | + def find_by_sql(sql) |
| 11 | + objects = child_find_by_sql(sql) |
| 12 | + if self.class.instance_variable_get(:@cti) |
| 13 | + puts "cti #{self.class.instance_variable_get(:@cti)}" |
| 14 | + objects.collect! do |obj| |
| 15 | + if obj.child_type |
| 16 | + child_class = Object.const_get(obj.child_type) |
| 17 | + obj = child_class.find(obj.child_id) |
| 18 | + end |
| 19 | + obj |
| 20 | + end |
| 21 | + end |
| 22 | + objects |
| 23 | + end |
| 24 | + def cti |
| 25 | + self.class.instance_variable_get(:@cti) |
| 26 | + end |
| 27 | + def cti=(value) |
| 28 | + self.class.instance_variable_set(:@cti,value) |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def self.has_parent(parent_class_name) |
| 34 | + |
| 35 | + has_one parent_class_name, :as => :child |
| 36 | + |
| 37 | + set_primary_key "#{parent_class_name}_id" |
| 38 | + |
| 39 | + # Fetch or build a parent instance |
| 40 | + define_method(parent_class_name) do |
| 41 | + parent_id = "#{parent_class_name}_id" |
| 42 | + @parent ||= send("build_#{parent_class_name}") unless eval(parent_id) |
| 43 | + if !@parent |
| 44 | + eval("#{parent_class_name.capitalize}.cti=false") |
| 45 | + @parent ||= eval("#{parent_class_name.capitalize}.find(#{parent_id})") |
| 46 | + eval("#{parent_class_name.capitalize}.cti=true") |
| 47 | + end |
| 48 | + @parent |
| 49 | + end |
| 50 | + |
| 51 | + define_method('parent') do |
| 52 | + send(parent_class_name) |
| 53 | + end |
| 54 | + |
| 55 | + before_save :save_parent |
| 56 | + |
| 57 | + define_method("save_parent") do |
| 58 | + parent.save |
| 59 | + self["#{parent_class_name}_id"] = parent.id |
| 60 | + true |
| 61 | + end |
| 62 | + |
| 63 | + validate :parent_valid |
| 64 | + |
| 65 | + # Assure parent is a valid class |
| 66 | + define_method("parent_valid") do |
| 67 | + unless valid = parent.valid? |
| 68 | + parent.errors.each do |attr, message| |
| 69 | + errors.add(attr, message) |
| 70 | + end |
| 71 | + end |
| 72 | + valid |
| 73 | + end |
| 74 | + |
| 75 | + # Determine and save parent class |
| 76 | + reflection = create_reflection(:has_one, parent_class_name, {}, self) |
| 77 | + parent_class = Object.const_get(reflection.class_name) |
| 78 | + self.instance_variable_set(:@parent_class,parent_class) |
| 79 | + |
| 80 | + # Determine inherits from parent |
| 81 | + columns = parent_class.column_names.reject { |c| self.column_names.grep(c).length > 0 || c == "child_type"} |
| 82 | + methods = parent_class.reflections.map { |key,value| key.to_s } |
| 83 | + methods = methods.reject { |c| self.reflections.map {|key, value| key.to_s }.include?(c) } |
| 84 | + inherits = columns + methods |
| 85 | + inherits.delete('id') |
| 86 | + inherits.delete('parent') |
| 87 | + |
| 88 | + inherits.each do |name| |
| 89 | + define_method name do |
| 90 | + parent.send(name) |
| 91 | + end |
| 92 | + define_method "#{name}=" do |new_value| |
| 93 | + parent.send("#{name}=", new_value) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + # Inherit constants |
| 98 | + constants = parent_class.constants.reject { |c| self.constants.grep(c).length > 0} |
| 99 | + constants.each do |name| |
| 100 | + const_set(name,parent_class.const_get(name)) |
| 101 | + end |
| 102 | + |
| 103 | + class << self |
| 104 | + alias_method :child_find, :find |
| 105 | + def find(*args) |
| 106 | + begin |
| 107 | + child_find(*args) |
| 108 | + rescue |
| 109 | + @parent_class.find(*args) if @parent_class |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | +end |
| 116 | + |
| 117 | + |
| 118 | +class ActiveRecord::Relation |
| 119 | + |
| 120 | + alias_method :child_first, :first |
| 121 | + def first(*args) |
| 122 | + begin |
| 123 | + child_first(*args) |
| 124 | + rescue |
| 125 | + klass = @klass.instance_variable_get(:@parent_class) |
| 126 | + if klass |
| 127 | + @klass = klass |
| 128 | + @table = klass.arel_table |
| 129 | + end |
| 130 | + child_first(*args) |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + alias_method :child_last, :last |
| 135 | + def last(*args) |
| 136 | + begin |
| 137 | + child_last(*args) |
| 138 | + rescue |
| 139 | + klass = @klass.instance_variable_get(:@parent_class) |
| 140 | + if klass |
| 141 | + @klass = klass |
| 142 | + @table = klass.arel_table |
| 143 | + end |
| 144 | + child_last(*args) |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + # alias_method :child_to_a, :to_a |
| 149 | + # def to_a |
| 150 | + # begin |
| 151 | + # child_to_a |
| 152 | + # rescue |
| 153 | + # klass = @klass.instance_variable_get(:@parent_class) |
| 154 | + # if klass |
| 155 | + # @klass = klass |
| 156 | + # @table = klass.arel_table |
| 157 | + # end |
| 158 | + # child_to_a |
| 159 | + # end |
| 160 | + # end |
| 161 | + # |
| 162 | + |
| 163 | + alias_method :child_find, :find |
| 164 | + def find(*args) |
| 165 | + begin |
| 166 | + child_find(*args) |
| 167 | + rescue |
| 168 | + klass = @klass.instance_variable_get(:@parent_class) |
| 169 | + if klass |
| 170 | + @klass = klass |
| 171 | + @table = klass.arel_table |
| 172 | + end |
| 173 | + child_find(*args) |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | +end |
| 178 | + |
0 commit comments