From 5b9394ce8264ebe90ca277c616bf302db1ab4a42 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 27 Jun 2013 00:10:55 -0700 Subject: [PATCH] oops - don't cache as_json's owned/wanted, but instead have the proxy override --- app/models/item.rb | 18 ++++++++++++++---- app/models/item/proxy.rb | 9 ++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index 1698a8bb..c3c2ddbf 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -208,18 +208,28 @@ class Item < ActiveRecord::Base species_support_ids.blank? || species_support_ids.include?(species.id) end - def as_json(options = {}) - { + def as_json(options={}) + json = { :description => description, :id => id, :name => name, :thumbnail_url => thumbnail_url, :zones_restrict => zones_restrict, :rarity_index => rarity_index, - :owned => owned?, - :wanted => wanted?, :nc => nc? } + + # Set owned and wanted keys, unless explicitly told not to. (For example, + # item proxies don't want us to bother, since they'll override.) + unless options.has_key?(:include_hanger_status) + options[:include_hanger_status] = true + end + if options[:include_hanger_status] + json[:owned] = owned? + json[:wanted] = wanted? + end + + json end before_create do diff --git a/app/models/item/proxy.rb b/app/models/item/proxy.rb index 0ac4e30f..e627842a 100644 --- a/app/models/item/proxy.rb +++ b/app/models/item/proxy.rb @@ -13,7 +13,10 @@ class Item end def as_json(options={}) - cache_method(:as_json) + cache_method(:as_json, include_hanger_status: false).tap do |json| + json[:owned] = owned? + json[:wanted] = wanted? + end end def cached?(type, name) @@ -39,12 +42,12 @@ class Item private - def cache_method(method_name, &block) + def cache_method(method_name, *args, &block) # Two layers of cache: a local copy, in case the method is called again, # and then the Rails cache, before we hit the actual method call. @known_outputs[method_name] ||= begin key = fragment_key(:method, method_name) - Rails.cache.fetch(key) { item.send(method_name) } + Rails.cache.fetch(key) { item.send(method_name, *args) } end end