Created
June 18, 2010 03:54
-
-
Save patrick99e99/443212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%- content_tag :li, :id => "dropdown_#{dropdown.id}" do %> | |
<%= dropdown.name %> | |
<%- unless dropdown.filtered_webpages.length.zero? -%> | |
<ul class="sortable"> | |
<%= hidden_field_tag "webpages[dropdown_#{dropdown.id}]", :id => nil %> | |
<%= render :partial => 'admin/dropdowns/webpage', :collection => dropdown.filtered_webpages %> | |
</ul> | |
<%- end -%> | |
<%- end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= content_tag :li, webpage.name, :id => "webpage_#{webpage.id}" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dropdown < ActiveRecord::Base | |
has_many :webpages, :order => "position", :dependent => :destroy | |
def self.update_positions(params) | |
update_dropdown_positions(params[:dropdowns]) | |
update_webpage_positions(params[:webpages]) | |
end | |
def self.update_dropdown_positions(dropdown_id_sequence) | |
dropdown_id_sequence.split(",").each_with_index do |dropdown_id, i| | |
Dropdown.find(dropdown_id).update_attribute(:position, i) | |
end | |
end | |
def self.update_webpage_positions(webpage_params) | |
webpage_params.values.each do |webpage_ids| | |
webpage_ids.split(",").each_with_index do |webpage_id, i| | |
Webpage.find(webpage_id).update_attribute(:position, i) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Dropdown do | |
context 'updating positions' do | |
def create_dropdowns | |
@dropdowns = [] | |
3.times do | |
@dropdowns << Dropdown.create(:name => 'some dropdown') | |
end | |
@dropdown_ids = @dropdowns.map(&:id) | |
end | |
def create_webpages | |
3.times do | |
@dropdowns.first.webpages << Webpage.create | |
end | |
@webpage_ids = @dropdowns.first.webpages.map(&:id) | |
end | |
before(:each) do | |
create_dropdowns | |
create_webpages | |
end | |
it 'should update the correct order of dropdowns' do | |
dropdown_order = [@dropdown_ids[2], @dropdown_ids[0], @dropdown_ids[1]] | |
Dropdown.update_dropdown_positions(dropdown_order.join(",")) | |
Dropdown.all(:order => :position).map(&:id).should == dropdown_order | |
end | |
it 'should update the correct order of webpages for a dropdown' do | |
webpage_order = [@webpage_ids[2], @webpage_ids[0], @webpage_ids[1]] | |
Dropdown.update_webpage_positions({"dropdown_#{@dropdowns.first.id}".to_sym => webpage_order.join(",")}) | |
@dropdowns.first.webpages.map(&:id).should == webpage_order | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::DropdownsController < Admin::TablesController | |
def reorder | |
@dropdowns = Dropdown.all(:order => 'position') | |
end | |
def update_positions | |
Dropdown.update_positions(params) | |
flash[:notice] = "Successfully updated positions." | |
redirect_to admin_dropdowns_url | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Event.addBehavior({ | |
'.sortable': function() { | |
Sortable.create(this, {'tag': 'li'}); | |
}, | |
'#update_dropdown_ordering:click': function() { | |
$$('.sortable').each(function(e) { | |
e.down('input').value = Sortable.sequence(e).join(","); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%- form_tag update_positions_admin_dropdowns_path, :method => 'put', :id => 'dropdown_position_form' do -%> | |
<ul id="main_menu_list" class="sortable"> | |
<%= hidden_field_tag :dropdowns, :id => nil %> | |
<%= render :partial => 'admin/dropdowns/dropdown', :collection => @dropdowns %> | |
</ul> | |
<div id="button_container"> | |
<%= link_to 'cancel', :back, :class => 'button_object' %> | |
<%= submit_tag 'update', :class => 'button_object', :id => 'update_dropdown_ordering' %> | |
</div> | |
<%- end -%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment