From d13256793130588caa5ec564dba989173843dca4 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 22 Jun 2013 15:45:59 -0700 Subject: [PATCH] move closet-hanger-destroy form to JS --- app/assets/javascripts/closet_hangers/index.js | 13 +++++++++++-- app/helpers/closet_lists_helper.rb | 8 ++++---- app/models/closet_hanger.rb | 2 ++ app/views/closet_hangers/_closet_hanger.html.haml | 6 ------ app/views/closet_hangers/index.html.haml | 14 ++++++++++---- app/views/closet_lists/_closet_list.html.haml | 2 +- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/closet_hangers/index.js b/app/assets/javascripts/closet_hangers/index.js index 8fd9b3b1..b7087c82 100644 --- a/app/assets/javascripts/closet_hangers/index.js +++ b/app/assets/javascripts/closet_hangers/index.js @@ -74,27 +74,36 @@ // is through the autocompleter, which reinitializes anyway. Geez, this thing // is begging for a rewrite, but today we're here for performance. $("#closet-hanger-update-tmpl").template("updateFormTmpl"); + $("#closet-hanger-destroy-tmpl").template("destroyFormTmpl"); onHangersInit(function () { // Super-lame hack to get the user ID from where it already is :/ var currentUserId = itemsSearchForm.data("current-user-id"); $("#closet-hangers div.closet-hangers-group").each(function () { var groupEl = $(this); var owned = groupEl.data("owned"); + groupEl.find("div.closet-list").each(function () { var listEl = $(this); var listId = listEl.data("id"); + listEl.find("div.object").each(function () { var hangerEl = $(this); var hangerId = hangerEl.data("id"); var quantityEl = hangerEl.find("div.quantity"); var quantity = hangerEl.data("quantity"); + $.tmpl("updateFormTmpl", { + user_id: currentUserId, + closet_hanger_id: hangerId, quantity: quantity, list_id: listId, - owned: owned, + owned: owned + }).appendTo(quantityEl); + + $.tmpl("destroyFormTmpl", { user_id: currentUserId, closet_hanger_id: hangerId - }).appendTo(quantityEl); + }).appendTo(hangerEl); }); }); }); diff --git a/app/helpers/closet_lists_helper.rb b/app/helpers/closet_lists_helper.rb index 498fba82..899b9ae2 100644 --- a/app/helpers/closet_lists_helper.rb +++ b/app/helpers/closet_lists_helper.rb @@ -17,10 +17,10 @@ module ClosetListsHelper ] end - def render_sorted_hangers(list, show_controls) - render :partial => 'closet_hanger', - :collection => list.hangers.sort { |x,y| x.item.name <=> y.item.name }, - :locals => {:show_controls => show_controls} + def render_sorted_hangers(list) + # TODO: do we still *need* the sort, now that 99% of items are translated? + render partial: 'closet_hanger', + collection: list.hangers.sort_by(&:item_name) end end diff --git a/app/models/closet_hanger.rb b/app/models/closet_hanger.rb index 036e3626..1f63739f 100644 --- a/app/models/closet_hanger.rb +++ b/app/models/closet_hanger.rb @@ -7,6 +7,8 @@ class ClosetHanger < ActiveRecord::Base attr_accessible :list_id, :owned, :quantity + delegate :name, to: :item, prefix: true + validates :item_id, :uniqueness => {:scope => [:user_id, :owned, :list_id]} validates :quantity, :numericality => {:greater_than => 0} validates_presence_of :item, :user diff --git a/app/views/closet_hangers/_closet_hanger.html.haml b/app/views/closet_hangers/_closet_hanger.html.haml index a7a66de8..3db56960 100644 --- a/app/views/closet_hangers/_closet_hanger.html.haml +++ b/app/views/closet_hangers/_closet_hanger.html.haml @@ -1,10 +1,4 @@ -- show_controls ||= false # we could do user check here, but may as well do it once %div{'class' => closet_hanger_partial_class(closet_hanger), 'data-item-id' => closet_hanger.item_id, 'data-quantity' => closet_hanger.quantity, 'data-id' => closet_hanger.id} = render :partial => 'items/item_link', :locals => {:item => closet_hanger.item} .quantity{:class => "quantity-#{closet_hanger.quantity}"} %span= closet_hanger.quantity - - if show_controls - = form_tag [current_user, closet_hanger], :method => :delete, :class => 'closet-hanger-destroy' do - = return_to_field_tag - = hidden_field_tag 'closet_hanger[owned]', closet_hanger.owned - = submit_tag t('.delete') diff --git a/app/views/closet_hangers/index.html.haml b/app/views/closet_hangers/index.html.haml index 337a5fd9..655e0c84 100644 --- a/app/views/closet_hangers/index.html.haml +++ b/app/views/closet_hangers/index.html.haml @@ -92,16 +92,22 @@ = t '.autocomplete.already_in_collection_html', collection_name: '${collection_name}' + + -# Gotta do this weird subbing in the path, since the braces will be + -# escaped if they themselves are inserted. Probably deserves a more legit + -# method, especially if we ever need it again. + - templated_hanger_path = user_closet_hanger_path(user_id: '$0', id: '$1').sub('$0', '${user_id}').sub('$1', '${closet_hanger_id}') %script#closet-hanger-update-tmpl{type: 'text/x-jquery-tmpl'} - -# Gotta do this weird subbing in the path, since the braces will be - -# escaped if they themselves are inserted. Probably deserves a more - -# legit method, especially if we ever need it again. - = form_tag user_closet_hanger_path(user_id: '$0', id: '$1').sub('$0', '${user_id}').sub('$1', '${closet_hanger_id}'), method: :put, authenticity_token: false, class: 'closet-hanger-update' do + = form_tag templated_hanger_path, method: :put, authenticity_token: false, class: 'closet-hanger-update' do = hidden_field_tag 'closet_hanger[list_id]', '${list_id}' = hidden_field_tag 'closet_hanger[owned]', '${owned}' = number_field_tag 'closet_hanger[quantity]', '${quantity}', min: 0, required: true + %script#closet-hanger-destroy-tmpl{type: 'text/x-jquery-tmpl'} + = form_tag templated_hanger_path, method: :delete, authenticity_token: false, class: 'closet-hanger-destroy' do + = submit_tag t('closet_hangers.closet_hanger.delete') + - content_for :stylesheets do = stylesheet_link_tag 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/south-street/jquery-ui.css' diff --git a/app/views/closet_lists/_closet_list.html.haml b/app/views/closet_lists/_closet_list.html.haml index 488982fb..9addd4a4 100644 --- a/app/views/closet_lists/_closet_list.html.haml +++ b/app/views/closet_lists/_closet_list.html.haml @@ -17,7 +17,7 @@ .closet-list-hangers - unless closet_list.hangers.empty? - = render_sorted_hangers(closet_list, show_controls) + = render_sorted_hangers(closet_list) %span.empty-list= t('.empty')