Module: Tenacity::Associations::HasMany::ClassMethods
- Defined in:
- lib/tenacity/associations/has_many.rb
Overview
:nodoc:
Instance Method Summary collapse
- #_t_clear_old_associations(record, association, old_associates) ⇒ Object
- #_t_save_associates(record, association) ⇒ Object
- #destroy_orphaned_associates(association, old_associates, associates) ⇒ Object
- #establish_relationship_in_target_objects(record, association, associates) ⇒ Object
- #get_current_associates(record, association) ⇒ Object
- #initialize_has_many_association(association) ⇒ Object
- #save_associate(associate) ⇒ Object
Instance Method Details
#_t_clear_old_associations(record, association, old_associates) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/tenacity/associations/has_many.rb', line 119 def _t_clear_old_associations(record, association, old_associates) property_name = association.foreign_key(record.class) old_associates.each do |old_associate| old_associate.send("#{property_name}=", nil) save_associate(old_associate) end end |
#_t_save_associates(record, association) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/tenacity/associations/has_many.rb', line 95 def _t_save_associates(record, association) return if record.perform_save_associates_callback == false loaded_associations = record.instance_variable_get('@_t_loaded_associations') if record.instance_variable_get(record._t_ivar_name(association)).nil? && (loaded_associations.nil? || loaded_associations[association].nil?) return end old_associates = get_current_associates(record, association) # Some ORM libraries (CouchRest, ActiveRecord, etc) return a proxy in # place of the associated objects. The actual associated objects # will be fetched the first time they are needed. So, force them to # be fetched here, before we clear them out in the database. old_associates.first _t_clear_old_associations(record, association, old_associates) associates = (record.instance_variable_get(record._t_ivar_name(association))) || [] establish_relationship_in_target_objects(record, association, associates) destroy_orphaned_associates(association, old_associates, associates) end |
#destroy_orphaned_associates(association, old_associates, associates) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/tenacity/associations/has_many.rb', line 137 def destroy_orphaned_associates(association, old_associates, associates) if association.dependent == :destroy || association.dependent == :delete_all issue_callbacks = (association.dependent == :destroy) (old_associates.map{|a| a.id} - associates.map{|a| a.id}).each do |associate_id| association.associate_class._t_delete([_t_serialize(associate_id)], issue_callbacks) end end end |
#establish_relationship_in_target_objects(record, association, associates) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/tenacity/associations/has_many.rb', line 146 def establish_relationship_in_target_objects(record, association, associates) associates.each do |a| associate = a._t_reload associate.send("#{association.foreign_key(record.class)}=", _t_serialize(record.id, association)) associate.send "#{association.polymorphic_type}=", self.to_s if association.polymorphic? save_associate(associate) end end |
#get_current_associates(record, association) ⇒ Object
131 132 133 134 135 |
# File 'lib/tenacity/associations/has_many.rb', line 131 def get_current_associates(record, association) clazz = association.associate_class property_name = association.foreign_key(record.class) clazz._t_find_all_by_associate(property_name, _t_serialize(record.id, association)) end |
#initialize_has_many_association(association) ⇒ Object
89 90 91 92 93 |
# File 'lib/tenacity/associations/has_many.rb', line 89 def initialize_has_many_association(association) _t_initialize_has_many_association(association) if self.respond_to?(:_t_initialize_has_many_association) attr_accessor :perform_save_associates_callback end |
#save_associate(associate) ⇒ Object
127 128 129 |
# File 'lib/tenacity/associations/has_many.rb', line 127 def save_associate(associate) associate.respond_to?(:_t_save_without_callback) ? associate._t_save_without_callback : associate._t_save_if_dirty end |