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