From 8411d4c64a0d95a57bc371e4d438c4cf488ac371 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 20 May 2010 19:56:08 -0400 Subject: [PATCH] swf asset json for pet types --- app/controllers/swf_assets_controller.rb | 7 ++++++- app/models/swf_asset.rb | 4 +++- app/models/swf_asset_parent.rb | 6 +++--- config/routes.rb | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/controllers/swf_assets_controller.rb b/app/controllers/swf_assets_controller.rb index 6403047a..5bd8ccba 100644 --- a/app/controllers/swf_assets_controller.rb +++ b/app/controllers/swf_assets_controller.rb @@ -1,5 +1,10 @@ class SwfAssetsController < ApplicationController def index - render :json => Item.find(params[:item_id]).swf_assets + if params[:item_id] + @swf_assets = Item.find(params[:item_id]).swf_assets.for_json.all + elsif params[:pet_type_id] + @swf_assets = PetType.find(params[:pet_type_id]).swf_assets.for_json.all + end + render :json => @swf_assets end end diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 9c212c27..5f20b978 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -5,6 +5,8 @@ class SwfAsset < ActiveRecord::Base delegate :depth, :to => :zone + scope :for_json, includes(:zone) + def local_url uri = URI.parse(url) uri.host = RemoteImpressHost @@ -13,7 +15,7 @@ class SwfAsset < ActiveRecord::Base uri.to_s end - def as_json + def as_json(options={}) { :id => id, :depth => depth, diff --git a/app/models/swf_asset_parent.rb b/app/models/swf_asset_parent.rb index 489b4604..3d69f337 100644 --- a/app/models/swf_asset_parent.rb +++ b/app/models/swf_asset_parent.rb @@ -1,12 +1,12 @@ module SwfAssetParent def swf_assets - rels = Table(ParentSwfAssetRelationship.table_name) + rels = ParentSwfAssetRelationship.arel_table type = self.class::SwfAssetType ids = ParentSwfAssetRelationship. where(rels[:parent_id].eq(id).and(rels[:swf_asset_type].eq(type))). select(rels[:swf_asset_id]). all.map(&:swf_asset_id) - assets = Table(SwfAsset.table_name) - SwfAsset.where(assets[:id].in(ids)).all + assets = SwfAsset.arel_table + SwfAsset.where(assets[:id].in(ids)) end end diff --git a/config/routes.rb b/config/routes.rb index c5cd53e4..e8bec354 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 '/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' end