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
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/composite_primary_keys/associations.rb', line 248 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 |key_pair| "#{key_pair.first}=#{key_pair.last}" end.join(" AND ") "(#{where_clause})" end |
#composite_where_clause(full_keys, ids) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/composite_primary_keys/associations.rb', line 229 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
275 276 277 278 279 280 281 282 283 |
# File 'lib/composite_primary_keys/associations.rb', line 275 def full_columns_equals(table_name, keys, quoted_ids) if keys.is_a?(Symbol) or (keys.is_a?(String) and keys == keys.to_s.split(CompositePrimaryKeys::ID_SEP)) return "#{table_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| "(#{table_name}.#{key} = #{id})"}.join(' AND ') end |
#full_composite_join_clause(table1, full_keys1, table2, full_keys2) ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/composite_primary_keys/associations.rb', line 259 def full_composite_join_clause(table1, full_keys1, table2, 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 |key_pair| "#{table1}.#{key_pair.first}=#{table2}.#{key_pair.last}" end.join(" AND ") "(#{where_clause})" end |
#full_keys(table_name, keys) ⇒ Object
270 271 272 273 |
# File 'lib/composite_primary_keys/associations.rb', line 270 def full_keys(table_name, keys) keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String) keys.is_a?(Array) ? keys.collect {|key| "#{table_name}.#{key}"}.join(CompositePrimaryKeys::ID_SEP) : "#{table_name}.#{keys}" end |
#set_belongs_to_association_for(record) ⇒ Object
285 286 287 288 289 290 291 292 293 |
# File 'lib/composite_primary_keys/associations.rb', line 285 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{|kv| record[kv.first] = kv.last} unless @owner.new_record? end end |