From 240d595714f50c72636a868cb6fcbc3a8dd5f403 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Fri, 13 Jan 2012 23:33:51 -0800 Subject: [PATCH] removing photos_count method from StatusMessage in hopes of fixing the counter (still a bit confused, though...) --- app/models/comment.rb | 1 - app/models/status_message.rb | 7 +------ spec/models/photo_spec.rb | 18 ++++++++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index f8de11dad..ef1e7c3eb 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -84,5 +84,4 @@ class Comment < ActiveRecord::Base def parent= parent self.post = parent end - end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index f8a172425..7fe01de82 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -40,10 +40,6 @@ class StatusMessage < Post joins(:likes).where(:likes => {:author_id => person.id}) } - def photos_count - self.photos.size - end - def self.guids_for_author(person) Post.connection.select_values(Post.where(:author_id => person.id).select('posts.guid').to_sql) end @@ -177,7 +173,7 @@ class StatusMessage < Post end def update_photos_counter - StatusMessage.where(:id => self.id). + self.class.where(:id => self.id). update_all(:photos_count => self.photos.count) end @@ -192,6 +188,5 @@ class StatusMessage < Post def self.tag_stream(tag_ids) joins(:tags).where(:tags => {:id => tag_ids}) end - end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index b1ae2fe27..b15e3a133 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -244,4 +244,22 @@ describe Photo do }.should_not change(StatusMessage, :count) end end + + describe 'photo cache counter' do + it "works" do + @s = @user.build_post(:status_message, :text => "sup?", :to => @aspect.id) + @s.save! + @s.reload.photos_count.should == 0 + @s.photos << @photo + @s.save! + @s.reload.photos_count.should == 1 + @s.photos << @photo2 + @s.save! + @s.reload.photos_count.should == 2 + @s.photos.last.destroy + @s.reload.photos_count.should == 1 + @s.photos.first.destroy + @s.reload.photos_count.should == 0 + end + end end