move closet-hanger-destroy form to JS

This commit is contained in:
Emi Matchu 2013-06-22 15:45:59 -07:00
parent 2876de2db0
commit d132567931
6 changed files with 28 additions and 17 deletions

View file

@ -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);
});
});
});

View file

@ -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

View file

@ -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

View file

@ -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')

View file

@ -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'

View file

@ -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')