Skip to content

Instantly share code, notes, and snippets.

@j-manu
Created November 17, 2011 22:20
Show Gist options
  • Save j-manu/1374738 to your computer and use it in GitHub Desktop.
Save j-manu/1374738 to your computer and use it in GitHub Desktop.
def update
@list = List.find(params[:id])
if(@list.position > params[:list][:position].to_i)
List.find(:all, :conditions => ["position >= ? AND position < ?", params[:list][:position],@list.position]).each {|c| c.update_attribute(:position, c.position + 1)}
else
List.find(:all, :conditions => ["position <= ? AND position > ?", params[:list][:position],@list.position]).each {|c| c.update_attribute(:position, c.position - 1)}
endif
@list.update_attributes(params[:list])
redirect_to :action => 'list'
else
redirect_to :action => 'edit', :id => params[:id]
end
end
def create
@list = List.new(params[:list])
if @list.save and @list.insert_at(params[:list][:position])
flash[:notice] = 'List was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end
def update_positions
params[:sortable_list].each_with_index do |id, position|
List.update(id, :position => position+1)
end
@lists = List.find(:all, :order => :position )
render :layout => false , :action => :reorder
end
def reorder
@lists = List.find(:all, :order => :position )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment