Commit a4611fd225a81fa6be1b0cc45212325dee295006
1 parent
d4026821
Exists in
master
and in
22 other branches
rails3: refactoring interface for category selection
Showing
8 changed files
with
34 additions
and
36 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
| @@ -231,7 +231,7 @@ class CmsController < MyProfileController | @@ -231,7 +231,7 @@ class CmsController < MyProfileController | ||
| 231 | @current_category = Category.find(params[:category_id]) | 231 | @current_category = Category.find(params[:category_id]) |
| 232 | @categories = @current_category.children | 232 | @categories = @current_category.children |
| 233 | end | 233 | end |
| 234 | - render :partial => 'shared/select_categories', :locals => {:object_name => 'article', :multiple => true}, :layout => false | 234 | + render :template => 'shared/update_categories', :locals => { :category => @current_category } |
| 235 | end | 235 | end |
| 236 | 236 | ||
| 237 | def publish | 237 | def publish |
app/controllers/my_profile/profile_editor_controller.rb
| @@ -60,7 +60,7 @@ class ProfileEditorController < MyProfileController | @@ -60,7 +60,7 @@ class ProfileEditorController < MyProfileController | ||
| 60 | @current_category = Category.find(params[:category_id]) | 60 | @current_category = Category.find(params[:category_id]) |
| 61 | @categories = @current_category.children | 61 | @categories = @current_category.children |
| 62 | end | 62 | end |
| 63 | - render :partial => 'shared/select_categories', :locals => {:object_name => 'profile_data', :multiple => true}, :layout => false | 63 | + render :template => 'shared/update_categories', :locals => { :category => @current_category } |
| 64 | end | 64 | end |
| 65 | 65 | ||
| 66 | def header_footer | 66 | def header_footer |
app/helpers/categories_helper.rb
| @@ -50,11 +50,17 @@ module CategoriesHelper | @@ -50,11 +50,17 @@ module CategoriesHelper | ||
| 50 | 50 | ||
| 51 | #FIXME make this test | 51 | #FIXME make this test |
| 52 | def selected_category_link(cat) | 52 | def selected_category_link(cat) |
| 53 | - js_remove = j("jQuery('#selected-category-#{cat.id}').remove();") | 53 | + js_remove = "jQuery('#selected-category-#{cat.id}').remove();" |
| 54 | content_tag('div', button_to_function_without_text(:remove, _('Remove'), js_remove) + | 54 | content_tag('div', button_to_function_without_text(:remove, _('Remove'), js_remove) + |
| 55 | link_to_function(cat.full_name(' → '), js_remove, :id => "remove-selected-category-#{cat.id}-button", :class => 'select-subcategory-link'), | 55 | link_to_function(cat.full_name(' → '), js_remove, :id => "remove-selected-category-#{cat.id}-button", :class => 'select-subcategory-link'), |
| 56 | :class => 'selected-category' | 56 | :class => 'selected-category' |
| 57 | ) | 57 | ) |
| 58 | end | 58 | end |
| 59 | 59 | ||
| 60 | + def update_categories_link(body, category_id=nil, html_options={}) | ||
| 61 | + link_to body, | ||
| 62 | + { :action => "update_categories", :category_id => category_id, :id => @object }, | ||
| 63 | + {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => 'select-subcategory-link'}.merge(html_options) | ||
| 64 | + end | ||
| 65 | + | ||
| 60 | end | 66 | end |
app/views/shared/_select_categories.html.erb
| @@ -3,30 +3,25 @@ | @@ -3,30 +3,25 @@ | ||
| 3 | <% if !@current_category.nil? %> | 3 | <% if !@current_category.nil? %> |
| 4 | <%= hidden_field_tag "#{object_name}[#{object_name}_category_id]", @current_category.id unless multiple %> | 4 | <%= hidden_field_tag "#{object_name}[#{object_name}_category_id]", @current_category.id unless multiple %> |
| 5 | <%= hidden_field_tag "#{object_name}[category_ids][]", @current_category.id if multiple %> | 5 | <%= hidden_field_tag "#{object_name}[category_ids][]", @current_category.id if multiple %> |
| 6 | - <%= button_to_remote_without_text(:back, _('Back'), | ||
| 7 | - { :update => "select-categories", | ||
| 8 | - :url => { :action => 'update_categories', :id => @object }, | ||
| 9 | - :loaded => visual_effect(:highlight, "select-categories") | ||
| 10 | - }, | ||
| 11 | - :id => 'cancel-category-button') %> | 6 | + |
| 7 | + <%= update_categories_link("", nil, :id => "cancel-category-button", :class => "button icon-back") %> | ||
| 12 | <% | 8 | <% |
| 13 | categories = [@current_category] | 9 | categories = [@current_category] |
| 14 | categories.push(@current_category) while @current_category = @current_category.parent | 10 | categories.push(@current_category) while @current_category = @current_category.parent |
| 15 | %> | 11 | %> |
| 16 | - <%= categories.compact.reverse.map{|i| | ||
| 17 | - link_to_remote(i.name, | ||
| 18 | - :update => "select-categories", | ||
| 19 | - :url => { :action => 'update_categories', :category_id => i.id, :id => @object }, | ||
| 20 | - :loaded => visual_effect(:highlight, "select-categories"), | ||
| 21 | - :class => 'select-current-category-link')}.join(' → ') | ||
| 22 | - %> | ||
| 23 | - <%= button_to_function_without_text(:add, _('Add'), nil, :id => 'save-category-button') do |page| | ||
| 24 | - page.insert_html :bottom, 'selected-categories', content_tag('div', | ||
| 25 | - hidden_field_tag("#{object_name}[category_ids][]", categories.first.id) + | ||
| 26 | - selected_category_link(categories.first), :id => "selected-category-#{categories.first.id}") | ||
| 27 | - page.replace_html 'select-categories', :partial => 'shared/select_subcategories', | ||
| 28 | - :locals => {:object_name => object_name, :categories => @toplevel_categories} | ||
| 29 | - end if multiple %> | 12 | + <%= categories.compact.reverse.map{|i| update_categories_link(i.name, i.id)}.join %> |
| 13 | + | ||
| 14 | + <script> | ||
| 15 | + function add_category() { | ||
| 16 | + var hidden_field = '<%= j hidden_field_tag("#{object_name}[category_ids][]", categories.first.id) %>'; | ||
| 17 | + var category_link = '<%= j selected_category_link(categories.first) %>'; | ||
| 18 | + jQuery('#selected-categories').append('<div id="selected-category-<%= categories.first.id %>">' + hidden_field + category_link + '</div>'); | ||
| 19 | + var subcategories = '<%= j(render :partial => 'shared/select_subcategories', :locals => {:object_name => object_name, :categories => @toplevel_categories}) %>'; | ||
| 20 | + jQuery('#select-categories').html(subcategories); | ||
| 21 | + } | ||
| 22 | + </script> | ||
| 23 | + | ||
| 24 | + <%= button_to_function_without_text(:add, _('Add'), 'add_category()', :id => 'save-category-button') %> | ||
| 30 | <% end %> | 25 | <% end %> |
| 31 | 26 | ||
| 32 | <div class="toplevel-categories"> | 27 | <div class="toplevel-categories"> |
app/views/shared/_select_subcategories.html.erb
| @@ -3,14 +3,6 @@ | @@ -3,14 +3,6 @@ | ||
| 3 | <div class="category-helper-label"><%= _('Click to select a category') %></div> | 3 | <div class="category-helper-label"><%= _('Click to select a category') %></div> |
| 4 | 4 | ||
| 5 | <% categories.select{|i| @object.accept_category?(i)}.each do |category| %> | 5 | <% categories.select{|i| @object.accept_category?(i)}.each do |category| %> |
| 6 | - | ||
| 7 | - <%= link_to_remote category.name, | ||
| 8 | - { :update => "select-categories", | ||
| 9 | - :url => { :action => "update_categories", :category_id => category.id, :id => @object}, | ||
| 10 | - :loaded => j(visual_effect(:highlight, "select-categories")) | ||
| 11 | - }, | ||
| 12 | - :class => 'select-subcategory-link', | ||
| 13 | - :id => "select-category-#{category.id}-link" | ||
| 14 | - %> | 6 | + <%= update_categories_link(category.name, category.id) %> |
| 15 | <% end %> | 7 | <% end %> |
| 16 | <% end %> | 8 | <% end %> |
test/functional/cms_controller_test.rb
| @@ -682,8 +682,8 @@ class CmsControllerTest < ActionController::TestCase | @@ -682,8 +682,8 @@ class CmsControllerTest < ActionController::TestCase | ||
| 682 | top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category') | 682 | top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category') |
| 683 | c1 = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id) | 683 | c1 = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id) |
| 684 | c2 = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id) | 684 | c2 = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id) |
| 685 | - get :update_categories, :profile => profile.identifier, :category_id => top.id | ||
| 686 | - assert_template 'shared/_select_categories' | 685 | + xhr :get, :update_categories, :profile => profile.identifier, :category_id => top.id |
| 686 | + assert_template 'shared/update_categories' | ||
| 687 | assert_equal top, assigns(:current_category) | 687 | assert_equal top, assigns(:current_category) |
| 688 | assert_equal [c1, c2], assigns(:categories) | 688 | assert_equal [c1, c2], assigns(:categories) |
| 689 | end | 689 | end |
test/functional/profile_editor_controller_test.rb
| @@ -474,8 +474,8 @@ class ProfileEditorControllerTest < ActionController::TestCase | @@ -474,8 +474,8 @@ class ProfileEditorControllerTest < ActionController::TestCase | ||
| 474 | top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category') | 474 | top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category') |
| 475 | c1 = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id) | 475 | c1 = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id) |
| 476 | c2 = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id) | 476 | c2 = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id) |
| 477 | - get :update_categories, :profile => profile.identifier, :category_id => top.id | ||
| 478 | - assert_template 'shared/_select_categories' | 477 | + xhr :get, :update_categories, :profile => profile.identifier, :category_id => top.id |
| 478 | + assert_template 'shared/update_categories' | ||
| 479 | assert_equal top, assigns(:current_category) | 479 | assert_equal top, assigns(:current_category) |
| 480 | assert_equal [c1, c2], assigns(:categories) | 480 | assert_equal [c1, c2], assigns(:categories) |
| 481 | end | 481 | end |