oops - don't cache as_json's owned/wanted, but instead have the proxy override

This commit is contained in:
Emi Matchu 2013-06-27 00:10:55 -07:00
parent bf697cef7b
commit 5b9394ce82
2 changed files with 20 additions and 7 deletions

View file

@ -209,17 +209,27 @@ class Item < ActiveRecord::Base
end
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

View file

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