Module: MoveAssociations::InstanceMethods

Defined in:
lib/move_associations/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#associated_records_sizeObject



27
28
29
30
31
32
33
# File 'lib/move_associations/instance_methods.rb', line 27

def associated_records_size
  associated_records = 0
  self.class.associations_to_move.collect do |association|
    associated_records += association_records(association.name).size 
  end
  associated_records
end

#association_records(association_name) ⇒ Object



23
24
25
# File 'lib/move_associations/instance_methods.rb', line 23

def association_records(association_name)
  [send(association_name.to_sym)].flatten.compact
end

#destroy_if_associations_are_emptyObject



3
4
5
# File 'lib/move_associations/instance_methods.rb', line 3

def destroy_if_associations_are_empty 
  destroy unless has_associated_records?
end

#has_associated_records?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/move_associations/instance_methods.rb', line 19

def has_associated_records?
  associated_records_size > 0
end

#move_association(association_name, new_id) ⇒ Object



7
8
9
10
11
# File 'lib/move_associations/instance_methods.rb', line 7

def move_association(association_name, new_id)
  if self.respond_to? association_name
    association_records(association_name).each { |record| record.update_attributes(self.class.name.foreign_key => new_id) }
  end
end

#move_associations(new_id) ⇒ Object



13
14
15
16
17
# File 'lib/move_associations/instance_methods.rb', line 13

def move_associations(new_id)
  self.class.associations_to_move.collect do |association|
    move_association(association.name, new_id)
  end
end