Commit 91e3f5f6 authored by danielgrippi's avatar danielgrippi

remove cache counter for photos, as it was causing too many edge-case errors &...

remove cache counter for photos, as it was causing too many edge-case errors & added complexity across models
parent 84af585f
......@@ -31,7 +31,7 @@ class Photo < ActiveRecord::Base
xml_attr :text
xml_attr :status_message_guid
belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid, :counter_cache => :photos_count
belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid
attr_accessible :text, :pending
validate :ownership_of_status_message
......@@ -43,14 +43,6 @@ class Photo < ActiveRecord::Base
queue_processing_job if self.author.local?
end
after_save do
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
......
......@@ -28,7 +28,6 @@ class Post < ActiveRecord::Base
t.add :provider_display_name
t.add :author
t.add :post_type
t.add :photos_count
t.add :image_url
t.add :object_url
t.add :root
......@@ -36,7 +35,7 @@ class Post < ActiveRecord::Base
t.add :user_like
t.add :mentioned_people
t.add lambda { |post|
if post.respond_to?(:photos) && post.photos_count > 0
if post.respond_to?(:photos)
post.photos
else
[]
......
......@@ -172,11 +172,6 @@ class StatusMessage < Post
self.oembed_url = urls.find{|url| ENDPOINT_HOSTS_STRING.match(URI.parse(url).host)}
end
def update_photos_counter
self.class.where(:id => self.id).
update_all(:photos_count => self.photos.count)
end
protected
def presence_of_content
unless text_and_photos_blank?
......
......@@ -27,7 +27,7 @@
</div>
<!-- duplicate from statusmessage partial -->
<% if(root.photos_count > 0 && root.photos && root.photos.length > 0) { %>
<% if(root.photos && root.photos.length > 0) { %>
<div class="photo_attachments">
<img src="<%= root.photos[0].sizes.large %>" class="stream-photo big_stream_photo">
<% for(photo in root.photos) {
......
<% if(photos_count > 0 && photos.length > 0) { %>
<% if(photos.length > 0) { %>
<div class="photo_attachments">
<a href="#" class="stream-photo-link">
<img src="<%= photos[0].sizes.large %>" class="stream-photo big_stream_photo" data-small-photo="<%= photos[0].sizes.small %>" data-full-photo="<%= photos[0].sizes.large %>" rel="lightbox">
......
class RemovePhotosCountFromPost < ActiveRecord::Migration
def self.up
remove_column :posts, :photos_count
end
def self.down
add_column :posts, :photos_count, :integer, :default => 0
execute <<SQL if Post.count > 0
UPDATE posts
SET photos_count = (SELECT COUNT(*) FROM photos WHERE photos.status_message_guid = posts.guid)
SQL
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111217042006) do
ActiveRecord::Schema.define(:version => 20120114191018) do
create_table "account_deletions", :force => true do |t|
t.string "diaspora_handle"
......@@ -315,7 +315,6 @@ ActiveRecord::Schema.define(:version => 20111217042006) do
t.integer "comments_count", :default => 0
t.integer "o_embed_cache_id"
t.integer "reshares_count", :default => 0
t.integer "photos_count", :default => 0
end
add_index "posts", ["author_id", "root_guid"], :name => "index_posts_on_author_id_and_root_guid", :unique => true
......
......@@ -53,8 +53,7 @@ factory = {
"root" : null,
"post_type" : "StatusMessage",
"likes_count" : 0,
"comments_count" : 0,
"photos_count" : 0
"comments_count" : 0
}
return new app.models.Post(_.extend(defaultAttrs, overrides))
......
......@@ -244,22 +244,4 @@ 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment