Class: ActiveRecord::Associations::AssociationProxy
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::AssociationProxy
- Defined in:
- lib/composite_primary_keys/associations.rb
Overview
:nodoc:
Instance Method Summary collapse
- #composite_join_clause(full_keys1, full_keys2) ⇒ Object
- #composite_where_clause(full_keys, ids) ⇒ Object
- #full_columns_equals(table_name, keys, quoted_ids) ⇒ Object
- #full_composite_join_clause(table1, full_keys1, table2, full_keys2) ⇒ Object
- #full_keys(table_name, keys) ⇒ Object
- #set_belongs_to_association_for(record) ⇒ Object
Instance Method Details
#composite_join_clause(full_keys1, full_keys2) ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/composite_primary_keys/associations.rb', line 253 def composite_join_clause(full_keys1, full_keys2) full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String) full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String) where_clause = [full_keys1, full_keys2].transpose.map do |key1, key2| "#{key1}=#{key2}" end.join(" AND ") "(#{where_clause})" end |
#composite_where_clause(full_keys, ids) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/composite_primary_keys/associations.rb', line 234 def composite_where_clause(full_keys, ids) full_keys = full_keys.split(CompositePrimaryKeys::ID_SEP) if full_keys.is_a?(String) if ids.is_a?(String) ids = [[ids]] elsif not ids.first.is_a?(Array) # if single comp key passed, turn into an array of 1 ids = [ids.to_composite_ids] end where_clause = ids.map do |id_set| transposed = id_set.size == 1 ? [[full_keys, id_set.first]] : [full_keys, id_set].transpose transposed.map do |full_key, id| "#{full_key.to_s}=#{@reflection.klass.sanitize(id)}" end.join(" AND ") end.join(") OR (") "(#{where_clause})" end |
#full_columns_equals(table_name, keys, quoted_ids) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/composite_primary_keys/associations.rb', line 290 def full_columns_equals(table_name, keys, quoted_ids) connection = @reflection.active_record.connection quoted_table_name = connection.quote_table_name(table_name) if keys.is_a?(Symbol) or (keys.is_a?(String) and keys == keys.to_s.split(CompositePrimaryKeys::ID_SEP)) return "#{quoted_table_name}.#{connection.quote_column_name(keys)} = #{quoted_ids}" end keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String) quoted_ids = quoted_ids.split(CompositePrimaryKeys::ID_SEP) if quoted_ids.is_a?(String) keys_ids = [keys, quoted_ids].transpose keys_ids.collect {|key, id| "(#{quoted_table_name}.#{connection.quote_column_name(key)} = #{id})"}.join(' AND ') end |
#full_composite_join_clause(table1, full_keys1, table2, full_keys2) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/composite_primary_keys/associations.rb', line 264 def full_composite_join_clause(table1, full_keys1, table2, full_keys2) connection = @reflection.active_record.connection full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String) full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String) quoted1 = connection.quote_table_name(table1) quoted2 = connection.quote_table_name(table2) where_clause = [full_keys1, full_keys2].transpose.map do |key_pair| "#{quoted1}.#{connection.quote_column_name(key_pair.first)}=#{quoted2}.#{connection.quote_column_name(key_pair.last)}" end.join(" AND ") "(#{where_clause})" end |
#full_keys(table_name, keys) ⇒ Object
279 280 281 282 283 284 285 286 287 288 |
# File 'lib/composite_primary_keys/associations.rb', line 279 def full_keys(table_name, keys) connection = @reflection.active_record.connection quoted_table_name = connection.quote_table_name(table_name) keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String) if keys.is_a?(Array) keys.collect {|key| "#{quoted_table_name}.#{connection.quote_column_name(key)}"}.join(CompositePrimaryKeys::ID_SEP) else "#{quoted_table_name}.#{connection.quote_column_name(keys)}" end end |
#set_belongs_to_association_for(record) ⇒ Object
302 303 304 305 306 307 308 309 310 |
# File 'lib/composite_primary_keys/associations.rb', line 302 def set_belongs_to_association_for(record) if @reflection.[:as] record["#{@reflection.[:as]}_id"] = @owner.id unless @owner.new_record? record["#{@reflection.[:as]}_type"] = @owner.class.base_class.name.to_s else key_values = @reflection.primary_key_name.to_s.split(CompositePrimaryKeys::ID_SEP).zip([@owner.id].flatten) key_values.each{|key, value| record[key] = value} unless @owner.new_record? end end |