From 37372544b027faf52059c07294cd5dad4c83cc86 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 8 Jun 2010 18:26:42 -0400 Subject: [PATCH] alter item swf asset routes to allow for page caching --- app/controllers/swf_assets_controller.rb | 6 ++--- app/models/color.rb | 1 + app/models/pet_type.rb | 5 ++++ app/models/swf_asset.rb | 12 ++++++--- config/routes.rb | 1 + public/javascripts/items/show.js | 32 ++++++++++++------------ 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/app/controllers/swf_assets_controller.rb b/app/controllers/swf_assets_controller.rb index a54ed4ab..3e63cfe3 100644 --- a/app/controllers/swf_assets_controller.rb +++ b/app/controllers/swf_assets_controller.rb @@ -4,9 +4,9 @@ class SwfAssetsController < ApplicationController @swf_assets = Item.find(params[:item_id]).swf_assets if params[:body_id] @swf_assets = @swf_assets.fitting_body_id(params[:body_id]) - if params[:body_id].is_a? Array - json = @swf_assets.for_json.all.group_by(&:body_id) - end + else + @swf_assets = @swf_assets.fitting_standard_body_ids + json = @swf_assets.for_json.all.group_by(&:body_id) end elsif params[:pet_type_id] @swf_assets = PetType.find(params[:pet_type_id]).pet_states.first.swf_assets diff --git a/app/models/color.rb b/app/models/color.rb index 39ad1755..4f3933f7 100644 --- a/app/models/color.rb +++ b/app/models/color.rb @@ -2,4 +2,5 @@ class Color < PetAttribute fetch_objects! Basic = %w(blue green red yellow).map { |name| find_by_name(name) } + BasicIds = Basic.map(&:id) end diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index 7819469f..6be2bf3c 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -3,6 +3,11 @@ class PetType < ActiveRecord::Base BasicHashes = YAML::load_file(Rails.root.join('config', 'basic_type_hashes.yml')) + + StandardBodyIds = PetType.select(arel_table[:body_id]). + where(arel_table[:color_id].in(Color::BasicIds)). + group(arel_table[:species_id]).map(&:body_id) + scope :random_basic_per_species, lambda { |species_ids| conditions = nil species_ids.each do |species_id| diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index d767ca9d..cd0d3dc1 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -6,10 +6,14 @@ class SwfAsset < ActiveRecord::Base delegate :depth, :to => :zone scope :for_json, includes(:zone) - scope :fitting_body_id, lambda { |body_ids| - body_ids = [body_ids] unless body_ids.is_a?(Array) - body_ids << 0 - where(arel_table[:body_id].in(body_ids)) + + scope :fitting_body_id, lambda { |body_id| + where(arel_table[:body_id].in([body_id, 0])) + } + + BodyIdsFittingStandard = PetType::StandardBodyIds + [0] + scope :fitting_standard_body_ids, lambda { + where(arel_table[:body_id].in(BodyIdsFittingStandard)) } def local_url diff --git a/config/routes.rb b/config/routes.rb index e8bec354..b482893f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ OpenneoImpressItems::Application.routes.draw do |map| match '/:id' => 'items#show', :as => :item match '/:item_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets + match '/:item_id/bodies/:body_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets_for_body_id match '/pet_types/:pet_type_id/swf_assets.json' => 'swf_assets#index', :as => :pet_type_swf_assets match '/species/:species_id/color/:color_id/pet_type.json' => 'pet_types#show' diff --git a/public/javascripts/items/show.js b/public/javascripts/items/show.js index 29b2301e..318c1086 100644 --- a/public/javascripts/items/show.js +++ b/public/javascripts/items/show.js @@ -103,19 +103,6 @@ function PetType() { } PetType.all = []; -PetType.all.load = function () { - var body_ids = $.map(PetType.all, function (pt) { return pt.body_id }); - $.getJSON(Item.current.assets_url_base, {body_id: body_ids}, function (assets_by_body_id) { - $.each(assets_by_body_id, function (i) { - Item.current.assets_by_body_id[parseInt(i)] = this; - }); - $.each(PetType.all, function () { - if(Item.current.getAssetsForPetType(this).length == 0) { - this.deactivateWithItem(Item.current); - } - }); - }); -} PetType.LOAD_ERROR = new LoadError("$color_article $color $species"); PetType.DASH_REGEX = /-/g; @@ -134,10 +121,9 @@ PetType.createFromLink = function (link) { function Item(id) { this.assets_by_body_id = {}; - this.assets_url_base = '/' + id + '/swf_assets.json'; this.load = function (pet_type) { - var url = this.assets_url_base + '?body_id=' + pet_type.body_id, + var url = '/' + id + '/bodies/' + pet_type.body_id + '/swf_assets.json', item = this; if(this.getAssetsForPetType(pet_type).length) { pet_type.onUpdate(); @@ -148,6 +134,20 @@ function Item(id) { } } + this.loadAllStandard = function () { + var item = this; + $.getJSON('/' + id + '/swf_assets.json', function (assets_by_body_id) { + $.each(assets_by_body_id, function (i) { + item.assets_by_body_id[parseInt(i)] = this; + }); + $.each(PetType.all, function () { + if(item.getAssetsForPetType(this).length == 0) { + this.deactivateWithItem(item); + } + }); + }); + } + this.getAssetsForPetType = function (pet_type) { return this.assets_by_body_id[pet_type.body_id] || this.assets_by_body_id[0] || []; } @@ -248,6 +248,6 @@ speciesList.each(function () { }); }); -setTimeout(PetType.all.load, 5000); +setTimeout($.proxy(Item.current, 'loadAllStandard'), 5000); MainWardrobe = { View: { Outfit: Preview } };