Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix bug that would cause all entries to be deleted upon saving with a…

…n empty image set.
  • Loading branch information...
commit a9c08ed741113bb2a974e0bf475e0e2e02b0d8ad 1 parent 34947e5
Rob Yurkowski robyurkowski authored
Showing with 40 additions and 26 deletions.
  1. +40 −26 app/models/refinery/portfolio/gallery.rb
66 app/models/refinery/portfolio/gallery.rb
View
@@ -28,39 +28,53 @@ def cover_image
items.sort_by(&:position).first if items.present?
end
- def images
- end
-
+ # We reject any empty ones because we have a template sitting around
+ # (Probably not the best way to do this, but it's what the project
+ # we cribbed the multi-image tab from did, and it's what we're doing
+ # in the interest of time.
def images=(ids = [])
@image_ids = ids.reject(&:empty?).map(&:to_i).uniq
end
-
+ # Don't upload duplicate images
def bulk_update_associated_items
- @image_ids ||= []
- existing_images = ::Refinery::Portfolio::Item.select('id').where(:gallery_id => id)
- existing_ids = existing_images.collect(&:id)
- @image_ids.each_with_index do |image_id, position|
- # If image_id is in existing_ids, don't add a new one, but update its position.
- if existing_ids.include? image_id
- existing_images.find{|i| i.id == image_id }.update_attribute(:position, position)
- # If image_id is not in existing_ids, create a new one
- else
- ::Refinery::Portfolio::Item.create({
- :title => "#{title} #{position}",
- :position => position,
- :gallery_id => id,
- :image_id => image_id
- })
- end
- end
- # Once done, compare ids to existing_ids. For any missing entries in ids, delete it.
- # [1 2 3 4 5] = [ 1 2 4 5 6] = [3]
- removed_ids = existing_ids - @image_ids
- removed_ids.each do |trashed_id|
- ::Refinery::Portfolio::Item.delete(trashed_id)
+ return unless @image_ids.present?
+ @image_ids.each_with_index { |image_id, position| update_position_or_create_item(image_id, position) }
+ delete_removed_items
+ end
+
+ private
+
+ def existing_image_ids
+ @existing_image_ids ||= self.items.pluck(:image_id)
+ end
+
+ def update_position_or_create_item(image_id, position)
+ # If that image ID already exists, update its item's position
+ if existing_image_ids.include? image_id
+ items.find_by_image_id(image_id).update_attribute(:position, position)
+ # If image_id is not in existing_ids, create a new one
+
+ else
+ ::Refinery::Portfolio::Item.create({
+ :title => "#{title} #{position}",
+ :position => position,
+ :gallery_id => id,
+ :image_id => image_id
+ })
end
+ end
+ def delete_removed_items
+ # Array#- will find all entries existing in the first array that do not exist in
+ # the second. In this case, we want to find which item id existed before but
+ # does not exist in the new array of item ids, because it's these items we
+ # want to delete.
+ #
+ # That is:
+ # [1 2 3] - [1 2 4] = [3]
+ removed_items = items.find_all_by_image_id(existing_image_ids - @image_ids)
+ removed_items.map(&:destroy)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.