Module: SortableAttachments::InstanceMethods

Defined in:
lib/sortable_attachments.rb

Instance Method Summary collapse

Instance Method Details

#attachment_ids_with_position=(attachment_ids) ⇒ Object



25
26
27
28
# File 'lib/sortable_attachments.rb', line 25

def attachment_ids_with_position=(attachment_ids)
  self.attachment_ids_without_position=(attachment_ids)
  reset_attachment_positions_by_ids(attachment_ids)
end

#reset_attachment_positions_by_ids(ids) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/sortable_attachments.rb', line 30

def reset_attachment_positions_by_ids(ids)
  return if self.new_record?
  ids.each_with_index do |id, i|
    connection.update(
      "UPDATE `attachment_links` SET `position` = #{i} " +
      "WHERE `element_id` = #{self.id} AND `element_type` = '#{self.class.base_class}' AND `attachment_id` = #{id}"
    ) if id.to_i != 0
  end
end