Skip to content

Commit

Permalink
Take images out of broadcast - not serializable
Browse files Browse the repository at this point in the history
Give images a default of `object.images`
  • Loading branch information
nimmolo committed May 22, 2024
1 parent 84ac477 commit de21850
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
17 changes: 5 additions & 12 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ class Observation < AbstractModel # rubocop:disable Metrics/ClassLength
has_many :namings

has_many :observation_images, dependent: :destroy
# These callbacks are to update the carousel when images are added or removed
has_many :images, through: :observation_images #,
# after_add: :flag_images, after_remove: :flag_images
has_many :images, through: :observation_images

has_many :project_observations, dependent: :destroy
has_many :projects, through: :project_observations
Expand Down Expand Up @@ -1060,6 +1058,9 @@ def dump_votes

# Add Image to this Observation, making it the thumbnail if none set already.
# Saves changes. Returns Image.
# NOTE: AR now does change tracking for belongs_to, but not has_many.
# @images_changed is used to trigger a broadcast, adapted from this:
# https://stackoverflow.com/a/60601696/3357635
def add_image(img)
unless images.include?(img)
images << img
Expand Down Expand Up @@ -1101,13 +1102,6 @@ def has_backup_data?

private

# ActiveRecord does change tracking for belongs_to, but not has_many.
# This is a workaround to flag changes to images.
# https://stackoverflow.com/a/60601696/3357635
def flag_images
@images_changed = true
end

# potentially broadcast other changes to obs here:
def do_broadcasts
# rubocop:disable Style/GuardClause
Expand All @@ -1117,8 +1111,7 @@ def do_broadcasts
[self, :images],
target: "observation_images",
partial: "shared/carousel",
locals: { images: images,
object: self,
locals: { object: self,
top_img: thumb_image,
html_id: "observation_images" }
)
Expand Down
3 changes: 1 addition & 2 deletions app/views/controllers/shared/_carousel.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# Note: uses concat(x) instead of [x,y].safe_join because of conditionals

images ||= nil #
images ||= object&.images || nil #
object ||= nil #
size ||= :large
top_img ||= images.first #
Expand All @@ -27,7 +27,6 @@ if !images.nil? && images.any?
data: { ride: "false", interval: "false" }) do
concat(tag.div(class: "carousel-inner bg-light", role: "listbox") do
images.each do |image|
concat(turbo_stream_from(image))
concat(render(partial: "shared/carousel_item",
locals: { image:, size:, top_img:, object: }))
end
Expand Down

0 comments on commit de21850

Please sign in to comment.