Skip to content

Commit

Permalink
Try broadcasting from obs controller instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed May 22, 2024
1 parent de21850 commit 1aa7cad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
16 changes: 15 additions & 1 deletion app/controllers/observations_controller/form_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def update_good_images(arg)
end

# Now that the observation has been successfully created, we can attach
# any images that were uploaded earlier
# any images that were uploaded earlier. This fires on :create and :update.
def attach_good_images(observation, images)
return unless images

Expand All @@ -233,6 +233,20 @@ def attach_good_images(observation, images)
end
logger.warn("attaching_image: #{image.id} to #{observation.id}")
end
broadcast_images_to_carousel(observation, images)
end

def broadcast_images_to_carousel(observation, images)
logger.warn("Broadcasting carousel update to observation #{observation.id}")
Turbo::StreamsChannel.broadcast_replace_later_to(
[observation, :images],
target: "observation_images",
partial: "shared/carousel",
locals: { images:,
object: observation,
top_img: observation.thumb_image,
html_id: "observation_images" }
)
end

def strip_images!
Expand Down
24 changes: 0 additions & 24 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ class Observation < AbstractModel # rubocop:disable Metrics/ClassLength
before_destroy :destroy_orphaned_collection_numbers
before_destroy :notify_species_lists
after_destroy :destroy_dependents
after_save :do_broadcasts

# Automatically (but silently) log destruction.
self.autolog_events = [:destroyed]
Expand Down Expand Up @@ -1069,7 +1068,6 @@ def add_image(img)
save
notify_users(:added_image)
reload
@images_changed = true
end
img
end
Expand All @@ -1084,7 +1082,6 @@ def remove_image(img)
update(thumb_image: images.empty? ? nil : images.first)
end
notify_users(:removed_image)
@images_changed = true
end
img
end
Expand All @@ -1100,27 +1097,6 @@ def has_backup_data?
notes.length >= 100
end

private

# potentially broadcast other changes to obs here:
def do_broadcasts
# rubocop:disable Style/GuardClause
if @images_changed || images.any?(&:saved_changes?)
logger.warn("Broadcasting carousel update to observation #{id}")
broadcast_replace_later_to(
[self, :images],
target: "observation_images",
partial: "shared/carousel",
locals: { object: self,
top_img: thumb_image,
html_id: "observation_images" }
)
end
# rubocop:enable Style/GuardClause
end

public

##############################################################################
#
# :section: Specimens
Expand Down

0 comments on commit 1aa7cad

Please sign in to comment.