added counter cache for photos on StatusMessage
This commit is contained in:
parent
0929f153a1
commit
97b8045f20
6 changed files with 35 additions and 3 deletions
|
|
@ -128,6 +128,7 @@ GEM
|
||||||
uuidtools
|
uuidtools
|
||||||
childprocess (0.2.3)
|
childprocess (0.2.3)
|
||||||
ffi (~> 1.0.6)
|
ffi (~> 1.0.6)
|
||||||
|
chronic (0.6.6)
|
||||||
client_side_validations (3.1.3)
|
client_side_validations (3.1.3)
|
||||||
cloudfiles (1.4.10)
|
cloudfiles (1.4.10)
|
||||||
mime-types (>= 1.16)
|
mime-types (>= 1.16)
|
||||||
|
|
@ -424,6 +425,9 @@ GEM
|
||||||
webmock (1.6.2)
|
webmock (1.6.2)
|
||||||
addressable (>= 2.2.2)
|
addressable (>= 2.2.2)
|
||||||
crack (>= 0.1.7)
|
crack (>= 0.1.7)
|
||||||
|
whenever (0.7.0)
|
||||||
|
activesupport (>= 2.3.4)
|
||||||
|
chronic (~> 0.6.3)
|
||||||
will_paginate (3.0.2)
|
will_paginate (3.0.2)
|
||||||
xpath (0.1.4)
|
xpath (0.1.4)
|
||||||
nokogiri (~> 1.3)
|
nokogiri (~> 1.3)
|
||||||
|
|
@ -515,5 +519,6 @@ DEPENDENCIES
|
||||||
typhoeus
|
typhoeus
|
||||||
vanna!
|
vanna!
|
||||||
webmock
|
webmock
|
||||||
|
whenever
|
||||||
will_paginate (= 3.0.2)
|
will_paginate (= 3.0.2)
|
||||||
yard
|
yard
|
||||||
|
|
|
||||||
|
|
@ -17,15 +17,20 @@ 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
|
belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid, :counter_cache => :photos_count
|
||||||
|
|
||||||
attr_accessible :text, :pending
|
attr_accessible :text, :pending
|
||||||
validate :ownership_of_status_message
|
validate :ownership_of_status_message
|
||||||
|
|
||||||
before_destroy :ensure_user_picture
|
before_destroy :ensure_user_picture
|
||||||
after_destroy :clear_empty_status_message
|
after_destroy :clear_empty_status_message
|
||||||
|
|
||||||
after_create :queue_processing_job
|
after_create :queue_processing_job
|
||||||
|
|
||||||
|
after_save 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
|
||||||
|
|
|
||||||
|
|
@ -178,13 +178,18 @@ class StatusMessage < Post
|
||||||
def queue_gather_oembed_data
|
def queue_gather_oembed_data
|
||||||
Resque.enqueue(Jobs::GatherOEmbedData, self.id, self.oembed_url)
|
Resque.enqueue(Jobs::GatherOEmbedData, self.id, self.oembed_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
def contains_oembed_url_in_text?
|
def contains_oembed_url_in_text?
|
||||||
require 'uri'
|
require 'uri'
|
||||||
urls = URI.extract(self.raw_message, ['http', 'https'])
|
urls = URI.extract(self.raw_message, ['http', 'https'])
|
||||||
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
|
||||||
|
StatusMessage.where(:id => self.id).
|
||||||
|
update_all(:photos_count => self.photos.count)
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def presence_of_content
|
def presence_of_content
|
||||||
if text_and_photos_blank?
|
if text_and_photos_blank?
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
class CounterCacheOnPostComments < ActiveRecord::Migration
|
class CounterCacheOnPostComments < ActiveRecord::Migration
|
||||||
class Post < ActiveRecord::Base; end
|
class Post < ActiveRecord::Base; end
|
||||||
|
|
||||||
def self.up
|
def self.up
|
||||||
add_column :posts, :comments_count, :integer, :default => 0
|
add_column :posts, :comments_count, :integer, :default => 0
|
||||||
execute <<SQL if Post.count > 0
|
execute <<SQL if Post.count > 0
|
||||||
15
db/migrate/20111217042006_add_photo_counter_cache_to_post.rb
Normal file
15
db/migrate/20111217042006_add_photo_counter_cache_to_post.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
class AddPhotoCounterCacheToPost < ActiveRecord::Migration
|
||||||
|
class Post < ActiveRecord::Base; end
|
||||||
|
|
||||||
|
def self.up
|
||||||
|
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
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
remove_column :posts, :photos_count
|
||||||
|
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 => 20111207233503) do
|
ActiveRecord::Schema.define(:version => 20111217042006) 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"
|
||||||
|
|
@ -314,6 +314,7 @@ ActiveRecord::Schema.define(:version => 20111207233503) do
|
||||||
t.integer "likes_count", :default => 0
|
t.integer "likes_count", :default => 0
|
||||||
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 "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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue