diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index afcee8571343af436a04fb6ba99267db9cbdcabc..e2a1e5cbfc122a283f2b3e3f4ced2b064cf14310 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 9dc63c54a27b2d765e137ffe237bfa293449fbe7..5cca0b903ed204952e4e118164bc161a9e9002be 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 e9b804544fa69afee8a538abf91cd2067db6f89b..2cfda4bca8e9c82c14215753bc7570fad2b576a1 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 fd51c339771a2ab91401eb8a8760fc76b9bd5576..92b5dd02fe70dc7a08a878a37f0c1d03ffc56340 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 d4444142de3994ff371427a8e400de10556e8517..c35c2d1107b4ae3c6447a7a3db64ce29c77ce824 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -158,14 +158,41 @@ describe Photo do end context "deletion" do - 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 + 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 proc { - status_message.destroy + @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 { + @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