| 1 |
module ActiveRecord |
|---|
| 2 |
module Associations |
|---|
| 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 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
class AssociationProxy |
|---|
| 49 |
alias_method :proxy_respond_to?, :respond_to? |
|---|
| 50 |
alias_method :proxy_extend, :extend |
|---|
| 51 |
delegate :to_param, :to => :proxy_target |
|---|
| 52 |
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_)/ } |
|---|
| 53 |
|
|---|
| 54 |
def initialize(owner, reflection) |
|---|
| 55 |
@owner, @reflection = owner, reflection |
|---|
| 56 |
Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) } |
|---|
| 57 |
reset |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
def proxy_owner |
|---|
| 61 |
@owner |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
def proxy_reflection |
|---|
| 65 |
@reflection |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
def proxy_target |
|---|
| 69 |
@target |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
def respond_to?(symbol, include_priv = false) |
|---|
| 73 |
proxy_respond_to?(symbol, include_priv) || (load_target && @target.respond_to?(symbol, include_priv)) |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
def ===(other) |
|---|
| 79 |
load_target |
|---|
| 80 |
other === @target |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
def aliased_table_name |
|---|
| 84 |
@reflection.klass.table_name |
|---|
| 85 |
end |
|---|
| 86 |
|
|---|
| 87 |
def conditions |
|---|
| 88 |
@conditions ||= interpolate_sql(sanitize_sql(@reflection.options[:conditions])) if @reflection.options[:conditions] |
|---|
| 89 |
end |
|---|
| 90 |
alias :sql_conditions :conditions |
|---|
| 91 |
|
|---|
| 92 |
def reset |
|---|
| 93 |
@loaded = false |
|---|
| 94 |
@target = nil |
|---|
| 95 |
end |
|---|
| 96 |
|
|---|
| 97 |
def reload |
|---|
| 98 |
reset |
|---|
| 99 |
load_target |
|---|
| 100 |
self unless @target.nil? |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
def loaded? |
|---|
| 104 |
@loaded |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
def loaded |
|---|
| 108 |
@loaded = true |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
def target |
|---|
| 112 |
@target |
|---|
| 113 |
end |
|---|
| 114 |
|
|---|
| 115 |
def target=(target) |
|---|
| 116 |
@target = target |
|---|
| 117 |
loaded |
|---|
| 118 |
end |
|---|
| 119 |
|
|---|
| 120 |
def inspect |
|---|
| 121 |
reload unless loaded? |
|---|
| 122 |
@target.inspect |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
protected |
|---|
| 126 |
def dependent? |
|---|
| 127 |
@reflection.options[:dependent] |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
def quoted_record_ids(records) |
|---|
| 131 |
records.map { |record| record.quoted_id }.join(',') |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
def interpolate_sql_options!(options, *keys) |
|---|
| 135 |
keys.each { |key| options[key] &&= interpolate_sql(options[key]) } |
|---|
| 136 |
end |
|---|
| 137 |
|
|---|
| 138 |
def interpolate_sql(sql, record = nil) |
|---|
| 139 |
@owner.send(:interpolate_sql, sql, record) |
|---|
| 140 |
end |
|---|
| 141 |
|
|---|
| 142 |
def sanitize_sql(sql) |
|---|
| 143 |
@reflection.klass.send(:sanitize_sql, sql) |
|---|
| 144 |
end |
|---|
| 145 |
|
|---|
| 146 |
def set_belongs_to_association_for(record) |
|---|
| 147 |
if @reflection.options[:as] |
|---|
| 148 |
record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record? |
|---|
| 149 |
record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s |
|---|
| 150 |
else |
|---|
| 151 |
record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? |
|---|
| 152 |
end |
|---|
| 153 |
end |
|---|
| 154 |
|
|---|
| 155 |
def merge_options_from_reflection!(options) |
|---|
| 156 |
options.reverse_merge!( |
|---|
| 157 |
:group => @reflection.options[:group], |
|---|
| 158 |
:limit => @reflection.options[:limit], |
|---|
| 159 |
:offset => @reflection.options[:offset], |
|---|
| 160 |
:joins => @reflection.options[:joins], |
|---|
| 161 |
:include => @reflection.options[:include], |
|---|
| 162 |
:select => @reflection.options[:select], |
|---|
| 163 |
:readonly => @reflection.options[:readonly] |
|---|
| 164 |
) |
|---|
| 165 |
end |
|---|
| 166 |
|
|---|
| 167 |
def with_scope(*args, &block) |
|---|
| 168 |
@reflection.klass.send :with_scope, *args, &block |
|---|
| 169 |
end |
|---|
| 170 |
|
|---|
| 171 |
private |
|---|
| 172 |
def method_missing(method, *args) |
|---|
| 173 |
if load_target |
|---|
| 174 |
if block_given? |
|---|
| 175 |
@target.send(method, *args) { |*block_args| yield(*block_args) } |
|---|
| 176 |
else |
|---|
| 177 |
@target.send(method, *args) |
|---|
| 178 |
end |
|---|
| 179 |
end |
|---|
| 180 |
end |
|---|
| 181 |
|
|---|
| 182 |
def load_target |
|---|
| 183 |
return nil unless defined?(@loaded) |
|---|
| 184 |
|
|---|
| 185 |
if !loaded? and (!@owner.new_record? || foreign_key_present) |
|---|
| 186 |
@target = find_target |
|---|
| 187 |
end |
|---|
| 188 |
|
|---|
| 189 |
@loaded = true |
|---|
| 190 |
@target |
|---|
| 191 |
rescue ActiveRecord::RecordNotFound |
|---|
| 192 |
reset |
|---|
| 193 |
end |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
def foreign_key_present |
|---|
| 198 |
false |
|---|
| 199 |
end |
|---|
| 200 |
|
|---|
| 201 |
def raise_on_type_mismatch(record) |
|---|
| 202 |
unless record.is_a?(@reflection.klass) |
|---|
| 203 |
raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.klass} expected, got #{record.class}" |
|---|
| 204 |
end |
|---|
| 205 |
end |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
def flatten_deeper(array) |
|---|
| 209 |
array.collect { |element| element.respond_to?(:flatten) ? element.flatten : element }.flatten |
|---|
| 210 |
end |
|---|
| 211 |
end |
|---|
| 212 |
end |
|---|
| 213 |
end |
|---|