remove cache counter for photos, as it was causing too many edge-case errors & added complexity across models
This commit is contained in:
parent
84af585f87
commit
91e3f5f6ff
9 changed files with 19 additions and 40 deletions
|
|
@ -31,7 +31,7 @@ class Photo < ActiveRecord::Base
|
||||||
xml_attr :text
|
xml_attr :text
|
||||||
xml_attr :status_message_guid
|
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
|
attr_accessible :text, :pending
|
||||||
validate :ownership_of_status_message
|
validate :ownership_of_status_message
|
||||||
|
|
@ -43,14 +43,6 @@ class Photo < ActiveRecord::Base
|
||||||
queue_processing_job if self.author.local?
|
queue_processing_job if self.author.local?
|
||||||
end
|
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
|
def clear_empty_status_message
|
||||||
if self.status_message_guid && self.status_message.text_and_photos_blank?
|
if self.status_message_guid && self.status_message.text_and_photos_blank?
|
||||||
self.status_message.destroy
|
self.status_message.destroy
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ class Post < ActiveRecord::Base
|
||||||
t.add :provider_display_name
|
t.add :provider_display_name
|
||||||
t.add :author
|
t.add :author
|
||||||
t.add :post_type
|
t.add :post_type
|
||||||
t.add :photos_count
|
|
||||||
t.add :image_url
|
t.add :image_url
|
||||||
t.add :object_url
|
t.add :object_url
|
||||||
t.add :root
|
t.add :root
|
||||||
|
|
@ -36,7 +35,7 @@ class Post < ActiveRecord::Base
|
||||||
t.add :user_like
|
t.add :user_like
|
||||||
t.add :mentioned_people
|
t.add :mentioned_people
|
||||||
t.add lambda { |post|
|
t.add lambda { |post|
|
||||||
if post.respond_to?(:photos) && post.photos_count > 0
|
if post.respond_to?(:photos)
|
||||||
post.photos
|
post.photos
|
||||||
else
|
else
|
||||||
[]
|
[]
|
||||||
|
|
|
||||||
|
|
@ -172,11 +172,6 @@ class StatusMessage < Post
|
||||||
self.oembed_url = urls.find{|url| ENDPOINT_HOSTS_STRING.match(URI.parse(url).host)}
|
self.oembed_url = urls.find{|url| ENDPOINT_HOSTS_STRING.match(URI.parse(url).host)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_photos_counter
|
|
||||||
self.class.where(:id => self.id).
|
|
||||||
update_all(:photos_count => self.photos.count)
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def presence_of_content
|
def presence_of_content
|
||||||
unless text_and_photos_blank?
|
unless text_and_photos_blank?
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- duplicate from statusmessage partial -->
|
<!-- 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">
|
<div class="photo_attachments">
|
||||||
<img src="<%= root.photos[0].sizes.large %>" class="stream-photo big_stream_photo">
|
<img src="<%= root.photos[0].sizes.large %>" class="stream-photo big_stream_photo">
|
||||||
<% for(photo in root.photos) {
|
<% for(photo in root.photos) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<% if(photos_count > 0 && photos.length > 0) { %>
|
<% if(photos.length > 0) { %>
|
||||||
<div class="photo_attachments">
|
<div class="photo_attachments">
|
||||||
<a href="#" class="stream-photo-link">
|
<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">
|
<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">
|
||||||
|
|
|
||||||
13
db/migrate/20120114191018_remove_photos_count_from_post.rb
Normal file
13
db/migrate/20120114191018_remove_photos_count_from_post.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
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.
|
# 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|
|
create_table "account_deletions", :force => true do |t|
|
||||||
t.string "diaspora_handle"
|
t.string "diaspora_handle"
|
||||||
|
|
@ -315,7 +315,6 @@ ActiveRecord::Schema.define(:version => 20111217042006) do
|
||||||
t.integer "comments_count", :default => 0
|
t.integer "comments_count", :default => 0
|
||||||
t.integer "o_embed_cache_id"
|
t.integer "o_embed_cache_id"
|
||||||
t.integer "reshares_count", :default => 0
|
t.integer "reshares_count", :default => 0
|
||||||
t.integer "photos_count", :default => 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "posts", ["author_id", "root_guid"], :name => "index_posts_on_author_id_and_root_guid", :unique => true
|
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,
|
"root" : null,
|
||||||
"post_type" : "StatusMessage",
|
"post_type" : "StatusMessage",
|
||||||
"likes_count" : 0,
|
"likes_count" : 0,
|
||||||
"comments_count" : 0,
|
"comments_count" : 0
|
||||||
"photos_count" : 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new app.models.Post(_.extend(defaultAttrs, overrides))
|
return new app.models.Post(_.extend(defaultAttrs, overrides))
|
||||||
|
|
|
||||||
|
|
@ -244,22 +244,4 @@ describe Photo do
|
||||||
}.should_not change(StatusMessage, :count)
|
}.should_not change(StatusMessage, :count)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue