diff --git a/app/models/communication/website/with_connected_objects.rb b/app/models/communication/website/with_connected_objects.rb index 21fe01143349673f6344ea3865281d27fca57ad2..98dede27c352c46eaa90f3e290fa7d5190ff708a 100644 --- a/app/models/communication/website/with_connected_objects.rb +++ b/app/models/communication/website/with_connected_objects.rb @@ -3,6 +3,8 @@ module Communication::Website::WithConnectedObjects included do has_many :connections + + after_save :connect_about, if: :saved_change_to_about_id? end # Appelé par un objet avec des connexions lorsqu'il est destroyed @@ -59,6 +61,11 @@ module Communication::Website::WithConnectedObjects protected + def connect_about + self.connect(about, self) if about.present? && about.try(:is_indirect_object?) + destroy_obsolete_connections + end + def connect_object(indirect_object, direct_source, direct_source_type: nil) return unless should_connect?(indirect_object, direct_source) # puts "connect #{object} (#{object.class})" @@ -81,7 +88,7 @@ module Communication::Website::WithConnectedObjects # On ne connecte pas le site à lui-même return false if indirect_object.is_a?(Communication::Website) # On ne connecte pas les objets directs (en principe ça n'arrive pas) - return false if indirect_object.respond_to?(:is_direct_object?) && indirect_object.is_direct_object? + return false if indirect_object.try(:is_direct_object?) true end end \ No newline at end of file diff --git a/app/models/communication/website/with_old_dependencies.rb b/app/models/communication/website/with_old_dependencies.rb index 527d1aa980f3cd3718a99d0ef08aa0a9fabf840b..39e51edead9c61358aaca5b360d192c03db2c50c 100644 --- a/app/models/communication/website/with_old_dependencies.rb +++ b/app/models/communication/website/with_old_dependencies.rb @@ -41,15 +41,15 @@ module Communication::Website::WithOldDependencies end def administrators - about&.administrators + has_administrators? ? about.administrators : University::Person.none end def researchers - about&.researchers + has_researchers? ? about.researchers : University::Person.none end def teachers - about&.teachers + has_teachers? ? about.teachers : University::Person.none end def people_in_blocks diff --git a/app/models/concerns/with_abouts.rb b/app/models/concerns/with_abouts.rb index c2cbb413bccc9d353fd07b1a34b56549a97cf7ec..4be1b380992c26208b575eab97037a611fd5fb15 100644 --- a/app/models/concerns/with_abouts.rb +++ b/app/models/concerns/with_abouts.rb @@ -6,6 +6,8 @@ module WithAbouts polymorphic: true, optional: true + before_validation :nullify_about_id_if_about_type_changed_to_blank + scope :for_about_type, -> (type) { where(about_type: type) } def self.about_types @@ -17,6 +19,11 @@ module WithAbouts Research::Journal.name, ] end + end + + protected + def nullify_about_id_if_about_type_changed_to_blank + self.about_id = nil if about_type_changed? && about_type.blank? end end