MS DG photos now delete sm if they are empty status messages, and status messages delete their photos

This commit is contained in:
maxwell 2010-12-05 00:23:02 -08:00
parent a415ff5c5f
commit 2c09c35425
5 changed files with 47 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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