Permalink
Browse files

Use UJS and js.erb in place of RJS

  • Loading branch information...
1 parent cc26f82 commit c0c322cefba02df7954c6e8dc389899f260bec68 @scottwillson committed Mar 22, 2012
View
13 app/assets/javascripts/categories.js
@@ -6,13 +6,12 @@ function bindCategoryEvents() {
$('.category').unbind();
jQuery('.category').droppable({
drop: function(ev, ui) {
- jQuery.post(
- '/admin/categories/add_child',
- {
- id: encodeURIComponent(jQuery(ui.draggable).attr('data-id')),
- parent_id: $(this).attr('data-id')
- }
- );
+ var droppedOn = jQuery(this);
+ jQuery.ajax({
+ type: 'PUT',
+ url: '/admin/categories/' + encodeURIComponent(jQuery(ui.draggable).attr('data-id')) + '.js',
+ data: { category: { parent_id: $(this).attr('data-id') } }
+ });
},
hoverClass: 'hovering'
});
View
30 app/controllers/admin/categories_controller.rb
@@ -7,29 +7,23 @@ class Admin::CategoriesController < Admin::AdminController
# === Assigns
# * categories
def index
- respond_to do |format|
- format.html {
- @category = Category.find_or_create_by_name(RacingAssociation.current.short_name)
- @unknowns = Category.find_all_unknowns
- }
- format.js {
- page.replace_html "category_#{@category.id}_children", :partial => "category", :collection => @category.children.sort
- }
+ if params[:parent_id].present?
+ @category = Category.find(params[:parent_id], :include => :children)
+ else
+ @category = Category.find_or_create_by_name(RacingAssociation.current.short_name)
+ @unknowns = Category.find_all_unknowns
end
end
- # Add category as child
- def add_child
- category_id = params[:id]
- @category = Category.find(category_id)
- parent_id = params[:parent_id]
- if parent_id.present?
- @parent = Category.find(parent_id)
- @category.parent = @parent
+ def update
+ @category = Category.find(params[:id])
+ @category.update_attributes(params[:category])
+ # parent_id could be nil, so can't use @category.children
+ if @category.parent_id
+ @children = @category.parent.children
else
- @category.parent = nil
+ @children = Category.find_all_unknowns
end
- @category.save!
end
# Calculate MbraBar only
View
12 app/views/admin/categories/add_child.js.erb
@@ -1,12 +0,0 @@
-page.remove "category_#{@category.id}_row"
-if @parent
- if @parent.name == RacingAssociation.current.short_name
- page.replace_html "association_category_root", :partial => "category", :collection => @parent.children.sort
- page.call :bindCategoryEvents
- else
- page.call :expandDisclosure, parent_id
- end
-else
- page.replace_html "unknown_category_root", :partial => "category", :collection => Category.find_all_unknowns.sort
- page.call :bindCategoryEvents
-end
View
4 app/views/admin/categories/index.html.erb
@@ -15,14 +15,14 @@
<div class="column_grid_container">
<div class="table_container box-3">
<div class="caption"><%= RacingAssociation.current.short_name %></div>
- <div class="category category_root" id="association_category_root" data-id="<%= @category.id %>">
+ <div class="category category_root association_category_root" id="category_<%= @category.id %>_row" data-id="<%= @category.id %>">
<%= render :partial => "category", :collection => @category.children.sort %>
</div>
</div>
<div class="table_container box-3 last">
<div class="caption">Unknown</div>
- <div class="category category_root" id="unknown_category_root" data-id="">
+ <div class="category category_root unknown_category_root" id="category__row" data-id="">
<%= render :partial => "category", :collection => @unknowns.sort %>
</div>
</div>
View
2 app/views/admin/categories/index.js.erb
@@ -1 +1 @@
-page.replace_html "category_#{@category.id}_children", :partial => "category", :collection => @category.children.sort
+jQuery('#category_<%= @category.id %>_children').html("<%= escape_javascript(render(@category.children.sort)) %>");
View
11 app/views/admin/categories/update.js.erb
@@ -0,0 +1,11 @@
+jQuery('#category_<%= @category.id %>_row').fadeOut();
+jQuery('#category_<%= @category.id %>_row').remove();
+
+<% if @category.parent && @category.parent.name != RacingAssociation.current.short_name %>
+ expandDisclosure('<%= @category.parent_id %>');
+<% else %>
+ jQuery('#category_<%= @category.parent_id %>_row').html("<%= escape_javascript(render(@children.sort)) %>");
+<% end %>
+
+jQuery('#category_<%= @category.id %>_row').effect("highlight", {}, 3000);
+bindCategoryEvents();
View
20 test/acceptance/admin/categories_test.rb
@@ -10,18 +10,18 @@ def test_edit
visit "/admin/categories"
- assert page.has_selector? "#unknown_category_root #category_#{masters_35_plus.id}"
- assert page.has_no_selector? "#association_category_root #category_#{masters_35_plus.id}"
+ assert page.has_selector? ".unknown_category_root #category_#{masters_35_plus.id}"
+ assert page.has_no_selector? ".association_category_root #category_#{masters_35_plus.id}"
- find("#category_#{masters_35_plus.id}").drag_to(find("#association_category_root"))
- assert page.has_selector? "#association_category_root #category_#{masters_35_plus.id}"
- assert page.has_no_selector? "#unknown_category_root #category_#{masters_35_plus.id}"
+ find("#category_#{masters_35_plus.id}").drag_to(find(".association_category_root"))
+ assert page.has_selector? ".association_category_root #category_#{masters_35_plus.id}"
+ assert page.has_no_selector? ".unknown_category_root #category_#{masters_35_plus.id}"
- assert page.has_selector? "#association_category_root #category_#{women_4.id}"
- assert page.has_no_selector? "#unknown_category_root #category_#{women_4.id}"
+ assert page.has_selector? ".association_category_root #category_#{women_4.id}"
+ assert page.has_no_selector? ".unknown_category_root #category_#{women_4.id}"
- find("#category_#{women_4.id}").drag_to(find("#unknown_category_root"))
- assert page.has_selector? "#unknown_category_root #category_#{women_4.id}"
- assert page.has_no_selector? "#association_category_root #category_#{women_4.id}"
+ find("#category_#{women_4.id}").drag_to(find(".unknown_category_root"))
+ assert page.has_selector? ".unknown_category_root #category_#{women_4.id}"
+ assert page.has_no_selector? ".association_category_root #category_#{women_4.id}"
end
end

0 comments on commit c0c322c

Please sign in to comment.