Try to take photos query out of views

This commit is contained in:
Raphael 2010-11-29 23:41:03 -08:00
parent fcb0e07dd7
commit fce428826c
7 changed files with 29 additions and 6 deletions

View file

@ -136,11 +136,14 @@ class AspectsController < ApplicationController
private private
def hashes_for_posts posts def hashes_for_posts posts
comment_hash = Comment.hash_from_post_ids posts.map{|p| p.id} post_ids = posts.map{|p| p.id}
comment_hash = Comment.hash_from_post_ids post_ids
person_hash = Person.from_post_comment_hash comment_hash person_hash = Person.from_post_comment_hash comment_hash
photo_hash = Photo.hash_from_post_ids post_ids
posts.map do |post| posts.map do |post|
{:post => post, {:post => post,
:photos => photo_hash[post.id],
:person => post.person, :person => post.person,
:comments => comment_hash[post.id].map do |comment| :comments => comment_hash[post.id].map do |comment|
{:comment => comment, {:comment => comment,

View file

@ -102,12 +102,15 @@ class PeopleController < ApplicationController
private private
def hashes_for_posts posts def hashes_for_posts posts
comment_hash = Comment.hash_from_post_ids posts.map{|p| p.id} post_ids = posts.map{|p| p.id}
comment_hash = Comment.hash_from_post_ids post_ids
person_hash = Person.from_post_comment_hash comment_hash person_hash = Person.from_post_comment_hash comment_hash
photo_hash = Photo.hash_from_post_ids post_ids
posts.map do |post| posts.map do |post|
{:post => post, {:post => post,
:person => @person, :person => @person,
:photos => photo_hash[post.id],
:comments => comment_hash[post.id].map do |comment| :comments => comment_hash[post.id].map do |comment|
{:comment => comment, {:comment => comment,
:person => person_hash[comment.person_id], :person => person_hash[comment.person_id],

View file

@ -39,6 +39,7 @@ class StatusMessagesController < ApplicationController
:locals => { :locals => {
:post => @status_message, :post => @status_message,
:person => @status_message.person, :person => @status_message.person,
:photos => @status_message.photos,
:comments => [], :comments => [],
:aspects => current_user.aspects, :aspects => current_user.aspects,
:current_user => current_user :current_user => current_user

View file

@ -12,9 +12,10 @@ module SocketsHelper
def action_hash(uid, object, opts={}) def action_hash(uid, object, opts={})
begin begin
user = User.find_by_id uid user = User.find_by_id uid
if object.is_a? Post if object.is_a? StatusMessage
post_hash = {:post => object, post_hash = {:post => object,
:person => object.person, :person => object.person,
:photos => object.photos,
:comments => object.comments.map{|c| :comments => object.comments.map{|c|
{:comment => c, {:comment => c,
:person => c.person :person => c.person

View file

@ -102,5 +102,20 @@ class Photo < Post
} }
end end
def self.hash_from_post_ids post_ids
hash = {}
photos = self.on_statuses(post_ids)
post_ids.each do |id|
hash[id] = []
end
photos.each do |photo|
hash[photo.status_message_id] << photo
end
hash.each_value {|photos| photos.sort!{|p1, p2| p1.created_at <=> p2.created_at }}
hash
end
scope :on_statuses, lambda { |post_ids|
where(:status_message_id.in => post_ids)
}
end end

View file

@ -25,7 +25,7 @@
= render 'shared/reshare', :current_user => current_user, :post => post if post.is_a? StatusMessage = render 'shared/reshare', :current_user => current_user, :post => post if post.is_a? StatusMessage
= link_to t('delete'), status_message_path(post), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete" = link_to t('delete'), status_message_path(post), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete"
= render 'status_messages/status_message', :post => post = render 'status_messages/status_message', :post => post, :photos => post.photos
.info .info
%span.time= link_to(how_long_ago(post), status_message_path(post)) %span.time= link_to(how_long_ago(post), status_message_path(post))

View file

@ -5,8 +5,8 @@
%p %p
= markdownify(post.message, :youtube_maps => post[:youtube_titles]) = markdownify(post.message, :youtube_maps => post[:youtube_titles])
- if post.photos.count > 0 - if photos.count > 0
.photo_attachments .photo_attachments
- for photo in post.photos - for photo in photos
= link_to (image_tag photo.url(:thumb_medium)), photo_path(photo) = link_to (image_tag photo.url(:thumb_medium)), photo_path(photo)