From a4611fd225a81fa6be1b0cc45212325dee295006 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 13 May 2014 17:15:30 -0300 Subject: [PATCH] rails3: refactoring interface for category selection --- app/controllers/my_profile/cms_controller.rb | 2 +- app/controllers/my_profile/profile_editor_controller.rb | 2 +- app/helpers/categories_helper.rb | 8 +++++++- app/views/shared/_select_categories.html.erb | 35 +++++++++++++++-------------------- app/views/shared/_select_subcategories.html.erb | 10 +--------- app/views/shared/update_categories.js.erb | 5 +++++ test/functional/cms_controller_test.rb | 4 ++-- test/functional/profile_editor_controller_test.rb | 4 ++-- 8 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 app/views/shared/update_categories.js.erb diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 107151f..557bd6b 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -231,7 +231,7 @@ class CmsController < MyProfileController @current_category = Category.find(params[:category_id]) @categories = @current_category.children end - render :partial => 'shared/select_categories', :locals => {:object_name => 'article', :multiple => true}, :layout => false + render :template => 'shared/update_categories', :locals => { :category => @current_category } end def publish diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb index 41b3f76..1150b14 100644 --- a/app/controllers/my_profile/profile_editor_controller.rb +++ b/app/controllers/my_profile/profile_editor_controller.rb @@ -60,7 +60,7 @@ class ProfileEditorController < MyProfileController @current_category = Category.find(params[:category_id]) @categories = @current_category.children end - render :partial => 'shared/select_categories', :locals => {:object_name => 'profile_data', :multiple => true}, :layout => false + render :template => 'shared/update_categories', :locals => { :category => @current_category } end def header_footer diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb index f473a34..c54fc49 100644 --- a/app/helpers/categories_helper.rb +++ b/app/helpers/categories_helper.rb @@ -50,11 +50,17 @@ module CategoriesHelper #FIXME make this test def selected_category_link(cat) - js_remove = j("jQuery('#selected-category-#{cat.id}').remove();") + js_remove = "jQuery('#selected-category-#{cat.id}').remove();" content_tag('div', button_to_function_without_text(:remove, _('Remove'), js_remove) + link_to_function(cat.full_name(' → '), js_remove, :id => "remove-selected-category-#{cat.id}-button", :class => 'select-subcategory-link'), :class => 'selected-category' ) end + def update_categories_link(body, category_id=nil, html_options={}) + link_to body, + { :action => "update_categories", :category_id => category_id, :id => @object }, + {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => 'select-subcategory-link'}.merge(html_options) + end + end diff --git a/app/views/shared/_select_categories.html.erb b/app/views/shared/_select_categories.html.erb index eb4f485..8aaf995 100644 --- a/app/views/shared/_select_categories.html.erb +++ b/app/views/shared/_select_categories.html.erb @@ -3,30 +3,25 @@ <% if !@current_category.nil? %> <%= hidden_field_tag "#{object_name}[#{object_name}_category_id]", @current_category.id unless multiple %> <%= hidden_field_tag "#{object_name}[category_ids][]", @current_category.id if multiple %> - <%= button_to_remote_without_text(:back, _('Back'), - { :update => "select-categories", - :url => { :action => 'update_categories', :id => @object }, - :loaded => visual_effect(:highlight, "select-categories") - }, - :id => 'cancel-category-button') %> + + <%= update_categories_link("", nil, :id => "cancel-category-button", :class => "button icon-back") %> <% categories = [@current_category] categories.push(@current_category) while @current_category = @current_category.parent %> - <%= categories.compact.reverse.map{|i| - link_to_remote(i.name, - :update => "select-categories", - :url => { :action => 'update_categories', :category_id => i.id, :id => @object }, - :loaded => visual_effect(:highlight, "select-categories"), - :class => 'select-current-category-link')}.join(' → ') - %> - <%= button_to_function_without_text(:add, _('Add'), nil, :id => 'save-category-button') do |page| - page.insert_html :bottom, 'selected-categories', content_tag('div', - hidden_field_tag("#{object_name}[category_ids][]", categories.first.id) + - selected_category_link(categories.first), :id => "selected-category-#{categories.first.id}") - page.replace_html 'select-categories', :partial => 'shared/select_subcategories', - :locals => {:object_name => object_name, :categories => @toplevel_categories} - end if multiple %> + <%= categories.compact.reverse.map{|i| update_categories_link(i.name, i.id)}.join %> + + + + <%= button_to_function_without_text(:add, _('Add'), 'add_category()', :id => 'save-category-button') %> <% end %>
diff --git a/app/views/shared/_select_subcategories.html.erb b/app/views/shared/_select_subcategories.html.erb index b02d5f7..c3acb27 100644 --- a/app/views/shared/_select_subcategories.html.erb +++ b/app/views/shared/_select_subcategories.html.erb @@ -3,14 +3,6 @@
<%= _('Click to select a category') %>
<% categories.select{|i| @object.accept_category?(i)}.each do |category| %> - - <%= link_to_remote category.name, - { :update => "select-categories", - :url => { :action => "update_categories", :category_id => category.id, :id => @object}, - :loaded => j(visual_effect(:highlight, "select-categories")) - }, - :class => 'select-subcategory-link', - :id => "select-category-#{category.id}-link" - %> + <%= update_categories_link(category.name, category.id) %> <% end %> <% end %> diff --git a/app/views/shared/update_categories.js.erb b/app/views/shared/update_categories.js.erb new file mode 100644 index 0000000..65602ed --- /dev/null +++ b/app/views/shared/update_categories.js.erb @@ -0,0 +1,5 @@ +<% + content = render :partial => 'shared/select_categories', + :locals => {:object_name => 'article', :multiple => true}, :layout => false +%> +jQuery('#select-categories').html('<%= j(content) %>'); diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 07e31e5..5f9be5b 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -682,8 +682,8 @@ class CmsControllerTest < ActionController::TestCase top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category') c1 = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id) c2 = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id) - get :update_categories, :profile => profile.identifier, :category_id => top.id - assert_template 'shared/_select_categories' + xhr :get, :update_categories, :profile => profile.identifier, :category_id => top.id + assert_template 'shared/update_categories' assert_equal top, assigns(:current_category) assert_equal [c1, c2], assigns(:categories) end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index efd9479..35c1259 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -474,8 +474,8 @@ class ProfileEditorControllerTest < ActionController::TestCase top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category') c1 = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id) c2 = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id) - get :update_categories, :profile => profile.identifier, :category_id => top.id - assert_template 'shared/_select_categories' + xhr :get, :update_categories, :profile => profile.identifier, :category_id => top.id + assert_template 'shared/update_categories' assert_equal top, assigns(:current_category) assert_equal [c1, c2], assigns(:categories) end -- libgit2 0.21.2