Permalink
Browse files

Photo and Flyer additions.

  • Loading branch information...
1 parent 9ee13cc commit 3a45ca55607ff90ce7b1235a5b91e42af4887bf0 @waratuman waratuman committed Aug 10, 2012
View
9 lib/mls/models/account.rb
@@ -37,7 +37,8 @@ class MLS::Account < MLS::Resource
exclude_from_comparison :password, :password_confirmation
- attr_accessor :favorite_ids
+ attr_accessor :favorite_ids, :password_required
+
attr_writer :favorites
def update
@@ -115,6 +116,12 @@ def unfavorite(listing_id)
end
end
+ def to_hash
+ hash = super
+ hash[:password_required] = password_required unless password_required.nil?
+ hash
+ end
+
class << self
def current
View
21 lib/mls/models/flyer.rb
@@ -3,24 +3,37 @@
class MLS::Flyer < MLS::Resource
property :id, Fixnum
+ property :subject_id, Fixnum
+ property :subject_type, String
property :created_at, DateTime
property :updated_at, DateTime
property :file_content_type, String
property :file_name, String
property :file_size, Fixnum
property :url, String
- def self.create(file)
- file.rewind
+ def self.create(attrs)
+ attrs[:file].rewind
url = MLS.url.dup
url.user = nil
url.path = "/api/flyers"
- response = RestClient.post(url.to_s, {:file => file}, MLS.headers)
- file.close unless file.closed?
+
+ if attrs[:subject]
+ attrs[:subject_id] = attrs[:subject].id
+ attrs[:subject_type] = attrs[:subject].class.name.split("::").last
+ attrs.delete(:subject)
+ end
+ response = RestClient.post(url.to_s, {:flyer => attrs}, MLS.headers)
+ attrs[:file].close unless attrs[:file].closed?
MLS::Flyer::Parser.parse(response.body)
end
+ def self.find(id)
+ response = MLS.get("/flyers/#{id}")
+ MLS::Flyer::Parser.parse(response.body)
+ end
+
end
class MLS::Flyer::Parser < MLS::Parser
View
2 lib/mls/models/photo.rb
@@ -21,7 +21,7 @@ def self.create(image_file)
url.user = nil
url.path = "/api/photos"
response = RestClient.post(url.to_s, {:file => image_file}, MLS.headers)
- image_file.close
+ image_file.close unless image_file.closed?
MLS::Photo::Parser.parse(response.body)
end
View
4 lib/mls/properties/decimal.rb
@@ -1,7 +1,9 @@
class MLS::Property::Decimal < MLS::Property
def load(value) # from_json
- if value.is_a?(BigDecimal)
+ if value.nil?
+ nil
+ elsif value.is_a?(BigDecimal)
value
else
BigDecimal.new(value.to_s)
View
1 test/test_helper.rb
@@ -13,4 +13,5 @@
FactoryGirl.find_definitions
MLS.url = ENV["MLS_TEST_URL"]
+MLS.auth_key = MLS::Account.authenticate(ENV["MLS_USER"], ENV["MLS_PASSWORD"]).auth_key
View
13 test/units/models/test_flyer.rb
@@ -4,8 +4,19 @@ class TestFlyer < ::Test::Unit::TestCase
def test_create_flyer
- flyer = MLS::Flyer.create(File.new('test/fixtures/flyer.pdf'))
+ flyer = MLS::Flyer.create(:file => File.new('test/fixtures/flyer.pdf'))
assert flyer.id
end
+
+ def test_attach_flyer_to_listing
+ listing = MLS::Listing.create
+ flyer = MLS::Flyer.create(
+ :file => File.new('test/fixtures/flyer.pdf'),
+ :subject => listing)
+
+ flyer = MLS::Flyer.find(flyer.id)
+ assert_equal listing.id, flyer.subject_id
+ assert_equal "Listing", flyer.subject_type
+ end
end

0 comments on commit 3a45ca5

Please sign in to comment.