Class: ActiveRecord::Associations::CollectionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_bulkoperation/active_record/associations/associations.rb

Instance Method Summary collapse

Instance Method Details

#child_association_pkObject



85
86
87
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 85

def child_association_pk
  parent_reflection.options[:association_foreign_key] || "#{proxy_association.klass.to_s.underscore.downcase}_id"
end

#child_pkObject



81
82
83
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 81

def child_pk
  parent_reflection.options[:foreign_key] || proxy_association.klass.primary_key
end

#countObject



91
92
93
94
95
96
97
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 91

def count
  if defined?(@internal_new_count)
    count_without_merges + ( @internal_new_count.to_i)
  else
    count_without_merges
  end
end

#count_without_mergesObject

TODO remove



90
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 90

alias_method :count_without_merges, :count

#get_m_t_m_table_nameObject



69
70
71
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 69

def get_m_t_m_table_name
  parent_reflection.options[:join_table] || [proxy_association.owner.class.table_name,proxy_association.klass.table_name].sort.join('_')
end

#handle_has_many_schedule_merge(record) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 47

def handle_has_many_schedule_merge(record)
  pk = proxy_association.reflection.options[:primary_key] || proxy_association.owner.class.primary_key
  fk = proxy_association.reflection.options[:foreign_key] || "#{proxy_association.owner.class.to_s.underscore.downcase}_id"
  if(pk.is_a?(Array))
    #puts "is a array"
    pk.each do |pk_item|
      pk_item_val = proxy_association.owner[pk_item.to_s]
      record.send("#{pk_item.to_s}=",pk_item_val)
    end
  else
    pk_val = proxy_association.owner[pk.to_s]        
    record.send("#{fk}=",pk_val) 
  end
  
  record.schedule_merge
  self << record
end

#handle_has_many_through_schedule_merge(record) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 99

def handle_has_many_through_schedule_merge(record)        
     # TODO AK doesn't work well 
  self << record 
  
=begin
  join_model = Class.new(ActiveRecord::Base) do
    class << self;           
      attr_accessor :table_info
    end
    def self.table_name
      table_info.table_name
    end
  end
  join_model.table_info = OpenStruct.new(:table_name => get_m_t_m_table_name)        
  unless(ManyToManyTables.const_defined?(get_m_t_m_table_name.camelize))
    ManyToManyTables.const_set get_m_t_m_table_name.camelize,join_model
  end
  #puts "RP DEBUG child_pk: #{child_pk} parent_pk: #{parent_pk}"
  #puts "RP DEBUG child_association_pk: #{child_association_pk} parent_association_pk: #{parent_association_pk}"
  #puts "RP DEBUG table_name: #{get_m_t_m_table_name}"
  c = ManyToManyTables.const_get get_m_t_m_table_name.camelize
  record.schedule_merge
  obj = c.new
  puts "AK: #{obj.class.name}"
  puts "AK: #{obj.class.name}.#{child_association_pk} = #{proxy_association.owner.class.name}.#{parent_pk} #{proxy_association.owner.send(parent_pk)}" 
  puts "AK: #{obj.class.name}.#{child_association_pk} = #{record.class.name}.#{parent_association_pk} #{record.send(parent_association_pk)}" 
  obj.send("#{parent_association_pk}=", proxy_association.owner.send(parent_pk))
  obj.send("#{child_association_pk}=" , record.send(parent_association_pk))
  obj.schedule_merge
  pp obj
  @internal_new_count ||= 0 
  @internal_new_count += 1
=end

end

#parent_association_pkObject



77
78
79
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 77

def parent_association_pk
  parent_reflection.options[:association_primary_key] || "#{proxy_association.owner.class.to_s.underscore.downcase}_id"
end

#parent_pkObject



73
74
75
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 73

def parent_pk
  parent_reflection.options[:primary_key] || proxy_association.owner.class.primary_key
end

#parent_reflectionObject



65
66
67
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 65

def parent_reflection
  proxy_association.reflection.parent_reflection[1]
end

#schedule_merge(record) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/activerecord_bulkoperation/active_record/associations/associations.rb', line 34

def schedule_merge(record)        
  proxy_association.reflection.options
  macro = proxy_association.reflection.macro
  if(proxy_association.is_a?(ActiveRecord::Associations::HasManyThroughAssociation))
    handle_has_many_through_schedule_merge(record)
    return
  end  
  if(macro == :has_many)
    handle_has_many_schedule_merge(record)
    return
  end      
end