Skip to content

Instantly share code, notes, and snippets.

@tute
Created June 17, 2011 03:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tute/1030815 to your computer and use it in GitHub Desktop.
Save tute/1030815 to your computer and use it in GitHub Desktop.
Rails SortableMethods
# lib/sortable_controller_methods.rb (must then load lib files manually)
# Add in "sortable" controllers:
# include SortableControllerMethods
#
# As in http://railscasts.com/episodes/147-sortable-lists
module SortableControllerMethods
def sort
klass.all.each do |object|
object.position = params[object.class.to_s.downcase].index(object.id.to_s) + 1
object.save
end
render :nothing => true
end
# May be common index action (or where sort options are displayed)
def order
instance_variable_set("@" + controller_name, klass.all)
end
def klass
controller_name.singularize.camelize.constantize
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment