From 99fb6b984f55eadcd4651f09768e31f5119c3a73 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 25 Nov 2010 21:33:34 -0500 Subject: [PATCH] ensure that we're only returning item assets for items and biology assets for pet states --- app/controllers/swf_assets_controller.rb | 7 +++---- app/models/item.rb | 3 ++- app/models/pet_state.rb | 3 ++- app/models/swf_asset.rb | 4 ++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/swf_assets_controller.rb b/app/controllers/swf_assets_controller.rb index 89158303..e86a331a 100644 --- a/app/controllers/swf_assets_controller.rb +++ b/app/controllers/swf_assets_controller.rb @@ -10,11 +10,10 @@ class SwfAssetsController < ApplicationController end elsif params[:body_id] && params[:item_ids] swf_assets = SwfAsset.arel_table - rels = ParentSwfAssetRelationship.arel_table - @swf_assets = SwfAsset.select('swf_assets.*, parents_swf_assets.parent_id'). + @swf_assets = SwfAsset.object_assets. + select('swf_assets.*, parents_swf_assets.parent_id'). fitting_body_id(params[:body_id]). - joins(:object_asset_relationships). - where(rels[:parent_id].in(params[:item_ids])) + for_item_ids(params[:item_ids]) json = @swf_assets.map { |a| a.as_json(:parent_id => a.parent_id.to_i, :for => 'wardrobe') } elsif params[:pet_state_id] @swf_assets = PetState.find(params[:pet_state_id]).swf_assets.all diff --git a/app/models/item.rb b/app/models/item.rb index ca6b4f8d..5955a543 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -4,7 +4,8 @@ class Item < ActiveRecord::Base has_one :contribution, :as => :contributed has_many :parent_swf_asset_relationships, :foreign_key => 'parent_id', :conditions => {:swf_asset_type => SwfAssetType} - has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :object_asset + has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :object_asset, + :conditions => {:type => SwfAssetType} attr_writer :current_body_id diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index a2750bad..de85b6cc 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -5,7 +5,8 @@ class PetState < ActiveRecord::Base has_many :outfits has_many :parent_swf_asset_relationships, :foreign_key => 'parent_id', :conditions => {:swf_asset_type => SwfAssetType} - has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :biology_asset + has_many :swf_assets, :through => :parent_swf_asset_relationships, :source => :biology_asset, + :conditions => {:type => SwfAssetType} belongs_to :pet_type diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 27c84d9d..cce106ba 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -22,6 +22,10 @@ class SwfAsset < ActiveRecord::Base scope :biology_assets, where(arel_table[:type].eq(PetState::SwfAssetType)) scope :object_assets, where(arel_table[:type].eq(Item::SwfAssetType)) + scope :for_item_ids, lambda { |item_ids| + joins(:object_asset_relationships). + where(ParentSwfAssetRelationship.arel_table[:parent_id].in(item_ids)) + } def local_url '/' + File.join(PUBLIC_ASSET_DIR, local_path_within_outfit_swfs)