@@ -22,15 +22,12 @@ class Relationship < ApplicationRecord
2222 before_create :point_targets_to_new_source
2323 before_create :destroy_same_suggested_item , if : proc { |r | r . is_confirmed? }
2424 after_create :move_to_same_project_as_main , prepend : true
25- after_create :update_counters , prepend : true
26- after_update :reset_counters , prepend : true
2725 after_update :propagate_inversion
2826 after_save :turn_off_unmatched_field , if : proc { |r | r . is_confirmed? || r . is_suggested? }
2927 after_save :move_explainers_to_source , :apply_status_to_target , if : proc { |r | r . is_confirmed? }
3028 before_destroy :archive_detach_to_list
31- after_destroy :update_counters , prepend : true
3229 after_destroy :turn_on_unmatched_field , if : proc { |r | r . is_confirmed? || r . is_suggested? }
33- after_commit :update_counter_and_elasticsearch , on : [ :create , :update ]
30+ after_commit :update_elasticsearch_data , on : [ :create , :update ]
3431 after_commit :destroy_elasticsearch_relation , on : :destroy
3532
3633 has_paper_trail on : [ :create , :update , :destroy ] , if : proc { |x | User . current . present? && !x . is_being_copied? } , versions : { class_name : 'Version' }
@@ -99,13 +96,6 @@ def self.default_type
9996 { source : 'parent' , target : 'child' }
10097 end
10198
102- def self . propagate_inversion ( ids , source_id )
103- Relationship . where ( id : ids . split ( ',' ) ) . each do |r |
104- r . source_id = source_id
105- r . send ( :reset_counters )
106- end
107- end
108-
10999 def is_being_copied?
110100 ( self . source && self . source . is_being_copied ) || self . is_being_copied
111101 end
@@ -138,22 +128,6 @@ def is_being_confirmed?
138128 self . send ( method ) . to_json == Relationship . suggested_type . to_json && self . relationship_type . to_json == Relationship . confirmed_type . to_json
139129 end
140130
141- def update_counters
142- return if self . is_default?
143- unless self . target . nil?
144- target = self . target
145- target . skip_check_ability = true
146- target . sources_count = Relationship . where ( target_id : target . id ) . where ( 'relationship_type = ?' , Relationship . confirmed_type . to_yaml ) . count
147- target . save!
148- end
149- unless self . source . nil?
150- source = self . source
151- source . skip_check_ability = true
152- source . targets_count = Relationship . where ( source_id : source . id ) . where ( 'relationship_type = ? OR relationship_type = ?' , Relationship . confirmed_type . to_yaml , Relationship . suggested_type . to_yaml ) . count
153- source . save!
154- end
155- end
156-
157131 def create_or_update_parent_id
158132 self . source_id
159133 end
@@ -293,15 +267,6 @@ def relationship_type_is_valid
293267 end
294268 end
295269
296- def reset_counters
297- if ( self . source_id_before_last_save && self . source_id_before_last_save != self . source_id ) || ( self . target_id_before_last_save && self . target_id_before_last_save != self . target_id )
298- previous = Relationship . new ( source_id : self . source_id_before_last_save , target_id : self . target_id_before_last_save )
299- previous . update_counters
300- current = Relationship . new ( source_id : self . source_id , target_id : self . target_id )
301- current . update_counters
302- end
303- end
304-
305270 def propagate_inversion
306271 if self . source_id_before_last_save == self . target_id && self . target_id_before_last_save == self . source_id
307272 ids = Relationship . where ( source_id : self . target_id ) . map ( &:id ) . join ( ',' )
@@ -322,7 +287,6 @@ def propagate_inversion
322287 end
323288 self . source &.clear_cached_fields
324289 self . target &.clear_cached_fields
325- Relationship . delay_for ( 1 . second ) . propagate_inversion ( ids , self . source_id )
326290 end
327291 end
328292
@@ -382,8 +346,7 @@ def turn_on_unmatched_field
382346 set_unmatched_field ( 1 )
383347 end
384348
385- def update_counter_and_elasticsearch
386- self . update_counters
349+ def update_elasticsearch_data
387350 self . update_elasticsearch_parent
388351 end
389352
0 commit comments