prepare partials for closet_hangers#index, too

This commit is contained in:
Emi Matchu 2013-12-27 21:48:28 -05:00
parent 1ce32e5867
commit b6247fa22f
5 changed files with 34 additions and 14 deletions

View file

@ -26,18 +26,27 @@ class ClosetHangersController < ApplicationController
visible_groups = @user.closet_hangers_groups_visible_to(@perspective_user) visible_groups = @user.closet_hangers_groups_visible_to(@perspective_user)
@unlisted_closet_hangers_by_owned = find_unlisted_closet_hangers_by_owned(visible_groups) @unlisted_closet_hangers_by_owned = find_unlisted_closet_hangers_by_owned(visible_groups)
if @public_perspective && user_signed_in? items = Item::ProxyArray.new
items = []
@closet_lists_by_owned.each do |owned, lists| @closet_lists_by_owned.each do |owned, lists|
lists.each do |list| lists.each do |list|
list.hangers.each { |hanger| items << hanger.item } list.hangers.each do |hanger|
hanger.item_proxy = Item::Proxy.new(hanger.item)
items << hanger.item_proxy
end end
end end
end
@unlisted_closet_hangers_by_owned.each do |owned, hangers| @unlisted_closet_hangers_by_owned.each do |owned, hangers|
hangers.each { |hanger| items << hanger.item } hangers.each do |hanger|
hanger.item_proxy = Item::Proxy.new(hanger.item)
items << hanger.item_proxy
end end
end
items.prepare_partial(:item_link_partial)
if @public_perspective && user_signed_in?
current_user.assign_closeted_to_items!(items) current_user.assign_closeted_to_items!(items)
end end
end end

View file

@ -129,7 +129,7 @@ module ItemsHelper
# Try to read from the prepared proxy's known partial output, if it's # Try to read from the prepared proxy's known partial output, if it's
# even a proxy at all. # even a proxy at all.
if item.respond_to?(:known_partial_output) if item.respond_to?(:known_partial_output)
prepared_output = item.known_partial_output(:item_link_partial).html_safe prepared_output = item.known_partial_output(:item_link_partial).try(:html_safe)
else else
prepared_output = nil prepared_output = nil
end end

View file

@ -7,6 +7,8 @@ class ClosetHanger < ActiveRecord::Base
attr_accessible :list_id, :owned, :quantity attr_accessible :list_id, :owned, :quantity
attr_accessor :item_proxy
delegate :name, to: :item, prefix: true delegate :name, to: :item, prefix: true
validates :item_id, :uniqueness => {:scope => [:user_id, :owned, :list_id]} validates :item_id, :uniqueness => {:scope => [:user_id, :owned, :list_id]}

View file

@ -12,8 +12,14 @@ class Item
} }
}) })
def initialize(items_or_ids) def initialize(proxies_or_items_or_ids=[])
self.replace(items_or_ids.map { |item_or_id| Proxy.new(item_or_id) }) self.replace(proxies_or_items_or_ids.map { |proxy_or_item_or_id|
if proxy_or_item_or_id.is_a?(Proxy)
proxy_or_item_or_id
else
Proxy.new(proxy_or_item_or_id)
end
})
end end
def prepare_method(name) def prepare_method(name)
@ -34,12 +40,15 @@ class Item
# values as we go along. Delete successfully set proxies, so that # values as we go along. Delete successfully set proxies, so that
# everything left in proxies_by_key in the end is known to be a miss. # everything left in proxies_by_key in the end is known to be a miss.
proxies_by_key = {} proxies_by_key = {}
self.each { |p| proxies_by_key[p.fragment_key(type, name)] = p } self.each do |p|
proxies_by_key[p.fragment_key(type, name)] ||= []
proxies_by_key[p.fragment_key(type, name)] << p
end
Rails.cache.read_multi(*proxies_by_key.keys).each { |k, v| Rails.cache.read_multi(*proxies_by_key.keys).each { |k, v|
proxies_by_key.delete(k).set_known_output(type, name, v) proxies_by_key.delete(k).each { |p| p.set_known_output(type, name, v) }
} }
missed_proxies = proxies_by_key.values missed_proxies = proxies_by_key.values.flatten
missed_proxies_by_id = missed_proxies.index_by(&:id) missed_proxies_by_id = missed_proxies.index_by(&:id)
item_scope.find(missed_proxies_by_id.keys).each do |item| item_scope.find(missed_proxies_by_id.keys).each do |item|

View file

@ -1,4 +1,4 @@
%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} %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_item_link(closet_hanger.item) = render_item_link(closet_hanger.item_proxy || closet_hanger.item)
.quantity{:class => "quantity-#{closet_hanger.quantity}"} .quantity{:class => "quantity-#{closet_hanger.quantity}"}
%span= closet_hanger.quantity %span= closet_hanger.quantity