diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index afcee8571..e2a1e5cbf 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -107,10 +107,10 @@ class PhotosController < ApplicationController if photo.status_message_id respond_with :location => photo.status_message else - respond_with :location => photos_path + respond_with :location => person_photos_path(current_user.person) end else - respond_with :location => photos_path + respond_with :location => person_photos_path(current_user.person) end end diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 9dc63c54a..5cca0b903 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -22,7 +22,7 @@ class StatusMessagesController < ApplicationController @status_message = current_user.build_post(:status_message, params[:status_message]) - if @status_message.save(:safe => true) + if photos || @status_message.save!(:safe => true) raise 'MongoMapper failed to catch a failed save' unless @status_message.id @status_message.photos += photos unless photos.nil? @@ -65,7 +65,6 @@ class StatusMessagesController < ApplicationController @status_message = current_user.my_posts.where(:_id => params[:id]).first if @status_message @status_message.destroy - else Rails.logger.info "event=post_destroy status=failure user=#{current_user.diaspora_handle} reason='User does not own post'" end diff --git a/app/models/photo.rb b/app/models/photo.rb index e9b804544..2cfda4bca 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -27,6 +27,7 @@ class Photo < Post before_destroy :ensure_user_picture + before_destroy :delete_parent_if_no_photos_or_message def ownership_of_status_message message = StatusMessage.find_by_id(self.status_message_id) if status_message_id && message @@ -117,5 +118,15 @@ class Photo < Post scope :on_statuses, lambda { |post_ids| where(:status_message_id.in => post_ids) } + + private + def delete_parent_if_no_photos_or_message + parent = self.status_message + photos = parent.photos || [] + if parent.message.blank? && photos.count <= 1 + parent.delete + end + end end + diff --git a/app/models/status_message.rb b/app/models/status_message.rb index fd51c3397..92b5dd02f 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -37,7 +37,7 @@ class StatusMessage < Post protected def message_or_photos_present? - unless !self.message.blank? || self.photos.count > 0 + if self.message.blank? && self.photos.count == 0 errors[:base] << 'Status message requires a message or at least one photo' end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index d4444142d..c35c2d110 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -158,14 +158,41 @@ describe Photo do end context "deletion" do + before do + @status_message = @user.build_post(:status_message, :message => "", :to => @aspect.id) + @status_message.photos << @photo2 + @status_message.save + @status_message.reload + end + it 'is deleted with parent status message' do - status_message = @user.build_post(:status_message, :message => "whattup", :to => @aspect.id) - status_message.photos << @photo2 - status_message.save + proc { + @status_message.destroy + }.should change(Photo, :count).by(-1) + end + + it 'deletes the parent object if there are no other photos or message' do + proc { + @photo2.destroy + }.should change(StatusMessage, :count).by(-1) + end + + it 'does not delete the parent if the parent has other photos' do + @status_message.photos << @photo + @status_message.save proc { - status_message.destroy - }.should change(Photo, :count).by(-1) + @photo2.destroy + }.should_not change(StatusMessage, :count) + end + + it 'does not delete the parent if the parent has a message' do + @status_message.message = "hello there kids" + @status_message.save + + proc { + @photo2.destroy + }.should_not change(StatusMessage, :count) end end end