From a5866a19e33cfb1e981326209bc199e55cfa33dc Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 20 May 2010 19:04:56 -0400 Subject: [PATCH] zone model in order to be able to include depth for swf_asset JSON --- app/models/swf_asset.rb | 20 ++++++++++++++++++++ app/models/zone.rb | 3 +++ config/environments/test.rb | 2 ++ spec/models/swf_asset_spec.rb | 35 +++++++++++++++++++++++++++++++++++ test/factories/swf_asset.rb | 2 ++ test/factories/zone.rb | 6 ++++++ 6 files changed, 68 insertions(+) create mode 100644 app/models/zone.rb create mode 100644 spec/models/swf_asset_spec.rb create mode 100644 test/factories/zone.rb diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index ae157ba8..9c212c27 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -1,3 +1,23 @@ class SwfAsset < ActiveRecord::Base set_inheritance_column 'inheritance_type' + + belongs_to :zone + + delegate :depth, :to => :zone + + def local_url + uri = URI.parse(url) + uri.host = RemoteImpressHost + pieces = uri.path.split('/') + uri.path = "/assets/swf/outfit/#{pieces[2]}/#{pieces[4..7].join('/')}" + uri.to_s + end + + def as_json + { + :id => id, + :depth => depth, + :local_url => local_url + } + end end diff --git a/app/models/zone.rb b/app/models/zone.rb new file mode 100644 index 00000000..f75affde --- /dev/null +++ b/app/models/zone.rb @@ -0,0 +1,3 @@ +class Zone < ActiveRecord::Base + set_inheritance_column 'inheritance_type' +end diff --git a/config/environments/test.rb b/config/environments/test.rb index f5ba670d..e56e3b4b 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -27,3 +27,5 @@ OpenneoImpressItems::Application.configure do # like if you have constraints or database-specific column types # config.active_record.schema_format = :sql end + +RemoteImpressHost = 'impress.openneo.net' diff --git a/spec/models/swf_asset_spec.rb b/spec/models/swf_asset_spec.rb new file mode 100644 index 00000000..52bf5e4a --- /dev/null +++ b/spec/models/swf_asset_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe SwfAsset do + it "belongs to a zone" do + zone = Factory.create :zone, :label => 'foo' + asset = Factory.create :swf_asset, :zone_id => 1 + asset.zone_id.should == 1 + asset.zone.id.should == 1 + asset.zone.label.should == 'foo' + end + + it "delegates depth to zone" do + zone = Factory.create :zone, :depth => 12 + asset = Factory.create :swf_asset, :zone_id => 1 + asset.depth.should == 12 + end + + it "converts neopets URL to impress URL" do + asset = Factory.create :swf_asset, :url => 'http://images.neopets.com/cp/items/swf/000/000/012/12211_9969430b3a.swf' + asset.local_url.should == 'http://impress.openneo.net/assets/swf/outfit/items/000/000/012/12211_9969430b3a.swf' + end + + it "should contain id, depth, and local_url as JSON" do + zone = Factory.create :zone, :depth => 12 + asset = Factory.create :swf_asset, + :id => 123, + :zone_id => 1, + :url => 'http://images.neopets.com/cp/items/swf/000/000/012/12211_9969430b3a.swf' + asset.as_json.should == { + :id => 123, + :depth => 12, + :local_url => 'http://impress.openneo.net/assets/swf/outfit/items/000/000/012/12211_9969430b3a.swf' + } + end +end diff --git a/test/factories/swf_asset.rb b/test/factories/swf_asset.rb index 1152a86c..ea48f253 100644 --- a/test/factories/swf_asset.rb +++ b/test/factories/swf_asset.rb @@ -3,4 +3,6 @@ Factory.define :swf_asset do |s| s.zone_id 0 s.zones_restrict '' s.body_id 0 + s.add_attribute :type, 'object' + s.sequence(:id) { |n| n } end diff --git a/test/factories/zone.rb b/test/factories/zone.rb new file mode 100644 index 00000000..10817e62 --- /dev/null +++ b/test/factories/zone.rb @@ -0,0 +1,6 @@ +Factory.define :zone do |z| + z.label 'foo' + z.depth 1 + z.add_attribute :type, 'FOO' + z.type_id 1 +end