Skip to content

Instantly share code, notes, and snippets.

@patsanch
Forked from dmitry/sort_photos.rb
Last active August 29, 2015 14:07
Show Gist options
  • Save patsanch/385a2ce9ecf0257e6393 to your computer and use it in GitHub Desktop.
Save patsanch/385a2ce9ecf0257e6393 to your computer and use it in GitHub Desktop.
# TODO can be optimized to use custom class and `model_ids` method
module SortPhotos
def sort_photos!(ids)
raise ArgumentError, "ids isn't an array" if !ids.is_a?(Array)
ids.map!(&:to_i)
old_ids = self.photo_ids
ids = ids - (ids - old_ids)
raise ArgumentError, "Not all the ids were passed to sort! method" if old_ids.size != ids.size
reflections[:photos].klass.update_all(['position = FIND_IN_SET(id, ?)', ids.join(',')], {:id => ids})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment