diff --git a/app/models/photo.rb b/app/models/photo.rb index c9300f379..904d884f7 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -31,6 +31,10 @@ class Photo < ActiveRecord::Base self.status_message.update_photos_counter if status_message end + after_destroy do + self.status_message.update_photos_counter if status_message + end + def clear_empty_status_message if self.status_message_guid && self.status_message.text_and_photos_blank? self.status_message.destroy diff --git a/app/models/status_message.rb b/app/models/status_message.rb index ddd4746ea..579dc06c4 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -18,22 +18,21 @@ class StatusMessage < Post has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid - # TODO: disabling presence_of_content() (and its specs in status_message_controller_spec.rb:125) is a quick and dirty fix for federation # a StatusMessage is federated before its photos are so presence_of_content() fails erroneously if no text is present - #validate :presence_of_content + # therefore, we put the validation in a before_destory callback instead of a validation + before_destroy :presence_of_content attr_accessible :text, :provider_display_name attr_accessor :oembed_url after_create :create_mentions - after_create :queue_gather_oembed_data, :if => :contains_oembed_url_in_text? #scopes scope :where_person_is_mentioned, lambda { |person| joins(:mentions).where(:mentions => {:person_id => person.id}) } - + scope :commented_by, lambda { |person| select('DISTINCT posts.*').joins(:comments).where(:comments => {:author_id => person.id}) } @@ -67,7 +66,7 @@ class StatusMessage < Post def raw_message=(text) write_attribute(:text, text) end - + def nsfw? self.raw_message.include?('#nsfw') end @@ -177,7 +176,7 @@ class StatusMessage < Post def queue_gather_oembed_data Resque.enqueue(Jobs::GatherOEmbedData, self.id, self.oembed_url) - end + end def contains_oembed_url_in_text? require 'uri' @@ -192,8 +191,8 @@ class StatusMessage < Post protected def presence_of_content - if text_and_photos_blank? - errors[:base] << 'Status message requires a message or at least one photo' + unless text_and_photos_blank? + errors[:base] << "Cannot destory a StatusMessage with text and/or photos present" end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index d3c133fe0..b1ae2fe27 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -59,7 +59,7 @@ describe Photo do @photo = Photo.diaspora_initialize( :author => @user.person, :user_file => @image) end - + it 'sets the persons diaspora handle' do @photo.diaspora_handle.should == @user.person.diaspora_handle end @@ -83,7 +83,7 @@ describe Photo do context "with a url" do it 'saves the photo' do url = "https://service.com/user/profile_image" - + photo_stub = stub.as_null_object photo_stub.should_receive(:remote_unprocessed_image_url=).with(url) Photo.stub(:new).and_return(photo_stub) @@ -227,17 +227,19 @@ describe Photo do }.should change(Photo, :count).by(-1) end - it 'will delete parent status message iff message is otherwise empty' do + it 'will delete parent status message if message is otherwise empty' do expect { @photo2.destroy }.should change(StatusMessage, :count).by(-1) end - it 'will not delete parent status message iff message had other content' do + it 'will not delete parent status message if message had other content' do + @status_message.text = "Some text" + @status_message.save + @status_message.reload + expect { - @status_message.text = "Some text" - @status_message.save - @status_message.reload + @photo2.status_message.reload @photo2.destroy }.should_not change(StatusMessage, :count) end