0

Accessing an index page takes about 6 seconds which is way too long considering there are only 4 users registered on my app right now in development environment. I'm using rails 4, devise, cancancan, public_activity, acts_as_commentable_through_threading, simple_form, haml.

Here's some output from the server

Rendered dogs/_activity.html.haml (5726.5ms)
Rendered dogs/index.html.haml within layouts/application (6033.2ms)

index.html.haml

 = provide(:title, @dog.name)
%h1 Dog Park
.row
  .col-xs-10.col-xs-offset-1.center
    .grass(style="margin-bottom:20px") 
.container
  .row
    .col-sm-3.col-sm-offset-1
      = render 'dogs/home_info' 
    .col-sm-6 
      = render 'dogs/activity' 

_activity.html.haml (using public activity)

%section
    = render 'post_form' 
#activities
    - @activities.each do |activity| 
        .activity= render_activity activity 
%ul.pager
    %li= link_to_previous_page @activities, "Newer"
    %li= link_to_next_page @activities, "Older"

I then have 3 activities public_activity/post/_create.html.haml

- if activity.trackable 
-# unless activity.trackable.foreign? and current_master == @master and Dog.find(activity.owner_id).master == current_master
.panel.panel-info{id: "activity#{activity.id}"}
    .panel-heading
        = link_to "×", master_dog_post_path(@master, activity.owner, Post.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this Post?", disable_with: "×" }, :class => 'close'
        - if activity.owner
            = link_to master_dog_path(activity.owner.master, activity.owner) do
                - if activity.owner.profile_pics.empty? 
                    = image_tag("dog_silhouette.jpg", alt: "Dog Profile pic", class: "img-circle", size: "50")
                - else 
                    = image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle")
                = activity.owner.name 
        updated their doggy status
        = time_ago_in_words(activity.created_at) 
        ago.
    %div(style="padding: 20px 10px;")
        = activity.trackable.content 
    .panel-footer(style="margin-top: 0px")
        .small
            Comments
        %div{ id: "comments#{activity.id}" } 
            = render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Dog"}
        %div{ id: "form#{activity.id}" }
            = render :partial => 'comments/form', locals: { activity: activity }

public_activity/post_pic/_create.html.haml

- if activity.trackable 
-# unless activity.trackable.foreign? and current_master == @master and Dog.find(activity.owner_id).master == current_master
.panel.panel-info{id: "activity#{activity.id}"}
    .panel-heading
        = link_to "×", master_dog_post_pic_path(activity.owner.master, activity.owner, PostPic.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this Picture?", disable_with: "×" }, :class => 'close'
        - if activity.owner
            = link_to master_dog_path(activity.owner.master, activity.owner) do
                - if activity.owner.profile_pics.empty? 
                    = image_tag("dog_silhouette.jpg", alt: "Dog Profile pic", class: "img-circle", size: "50")
                - else 
                    = image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle")
                = activity.owner.name 
        added a picture
        = time_ago_in_words(activity.created_at) 
        ago.
    .list-group
        - if activity.trackable.title?
            %li.list-group-item
                = activity.trackable.title 
        %li.list-group-item.center(style= "padding: 20px 0px;")
            = image_tag(activity.trackable.image.url, :width => 5000, :height => 400, :crop => :fill, class: "img-thumbnail center") 
        - if activity.trackable.description? 
            %li.list-group-item
                = activity.trackable.description 
    .panel-footer
        %div{ id: "comments#{activity.id}" }
            .small Comments 
            = render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Dog"} 
        %div{ id: "form#{activity.id}" }
            = render :partial => 'comments/form', locals: { activity: activity }

public_activity/profile_pic/_create.html.haml

- if activity.trackable 
.panel.panel-info{id: "activity#{activity.id}"}
    .panel-heading
        - if activity.owner
            - if activity.owner_type == "Dog" 
                = link_to "×", master_dog_profile_pic_path(activity.owner.master, activity.owner, ProfilePic.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this profile picture?", disable_with: "×" }, :class => 'close'
                = link_to master_dog_path @master, activity.owner do
                    = image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle") 
                    = activity.owner.name 
            - else 
                = link_to "×", master_profile_pic_path(activity.owner, ProfilePic.find(activity.trackable_id), :activity_id => activity.id), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to delete this profile picture?", disable_with: "×" }, :class => 'close'
                = link_to master_path activity.owner do
                    = image_tag(activity.owner.profile_pics.first.image.thumbnail.url, :width => 50, :height => 50, :crop => :fill, class: "img-circle") 
                    = activity.owner.name 
        updated their profile picture
        = time_ago_in_words(activity.created_at) 
        ago.

    .center(style="padding: 20px 0")
        - if activity.owner_type === "Dog" 
            = image_tag(activity.trackable.image.thumbnail.url, :width => 300, :height => 200, :crop => :fill, class: "img-thumbnail center") 
        - else
            = image_tag activity.trackable.image.thumbnail.url, :width => 300, :height => 200, :crop => :fill,  class: "img-thumbnail center" 
    .panel-footer
        %div{ id: "comments#{activity.id}" }
            .small Comments 
            - if activity.owner_type === "Dog" 
                = render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Dog"}  
            - else
                = render :partial => 'comments/comment', collection: activity.comment_threads , as: :comment, locals: { object: "Master"} 
        %div{ id: "form#{activity.id}" }
            = render :partial => 'comments/form', locals: { activity: activity }

my comments are through acts_as_commentable_through_threading

comments/comment.html.haml

%div{ id: "comment#{comment.id}" }
%hr(style="margin: 5px")
= link_to "×", comment_path(comment), :method => :delete, :remote => true, data: { confirm: "Are you sure you want to remove this comment?", disable_with: "×" }, :class => 'close'
- if Dog.exists?(comment.user_id)
    = link_to master_dog_path Dog.find(comment.user_id).master, Dog.find(comment.user_id) do
        - if Dog.find(comment.user_id).profile_pics.empty? 
            = image_tag("dog_silhouette.jpg", alt: "Dog Profile pic", class: "img-circle", size: "30")
        - else 
            = image_tag(Dog.find(comment.user_id).profile_pics.first.image.thumbnail.url, :width => 30, :height => 30, :crop => :fill, class: "img-circle") 
        = Dog.find(comment.user_id).name 
%inline.small
    = time_ago_in_words(comment.created_at) 
    ago.
%p.small= comment.body

comments/_form.html.haml

= simple_form_for Comment.new, :remote => true do |f|


= f.input :body, :input_html => { :rows => "2",placeholder: "Add a comment..." }, :label => false
  = f.input :commentable_id, :as => :hidden, :input_html => { :value => activity.id }
  = f.input :commentable_type, :as => :hidden, :input_html => { :value => activity }
  = f.button :submit, "Comment", :class => "btn btn-primary", :disable_with => "Adding Comment…"

Here is the action. I get the ids through a keep_if block which I then use in a where method to grab @activities, which I'm sure is probably inefficient but I'm a bit new and not sure how to request such a complex query in a where method. If anyone has any advice on a more efficient way I would really appreciate it.

def index
@dog = current_dog

@master = current_master
@buddy_ids = @master.buddy_ids 
@master_id = @master.id
@buddy_dog_ids = []
@master.buddies.each do |b|
  @buddy_dog_ids << b.dog_ids
end
@master_dog_ids = @master.dog_ids

@activity_ids = PublicActivity::Activity.ids.keep_if do |id|
  a = PublicActivity::Activity.find(id) 
  a.trackable_type.constantize.exists?(a.trackable_id) and
  #take out current_master.dogs posts and pics
  (@master_dog_ids.include?(a.owner_id) or @master_dog_ids.include?(a.recipient_id) and (a.owner_type == "Dog")) or
  #take out buddies.dogs posts and pics (not foreign)
  (@buddy_dog_ids.flatten.include?(a.owner_id) and a.trackable_type.constantize.exists?(a.trackable_id) and !(a.trackable_type.constantize.find(a.trackable_id).foreign? unless a.trackable_type == "ProfilePic") and (a.owner_type == "Dog")) or
  #take out profile pics
  (@master_id == a.owner_id or @buddy_ids.include?(a.owner_id) and a.owner_type == "Master")
end
@activities = PublicActivity::Activity.order("created_at desc").where(id: @activity_ids).page(params[:page]).per(50)

@post = @dog.posts.build
@photo = @dog.post_pics.build
@new_comment = Comment.new

end

monty_lennie
  • 2,871
  • 2
  • 29
  • 49

2 Answers2

0

Caching will be done as a temporar solution. Though, you should try to make your code more efficient in the long run.

Peeyush
  • 6,144
  • 5
  • 23
  • 37
0

In your _activity.html.haml, do not use the @activities instance variable, you are trying to display all the activities again and again.

You'd better write :

.activity= render_activity activity

Just that, nothing else ! Because the _activity partial is here to render only one activity, not a lot of them. Can you show me your index.html.haml please ?

Try that :

#index.html.haml
 = provide(:title, @dog.name)
%h1 Dog Park
.row
  .col-xs-10.col-xs-offset-1.center
    .grass(style="margin-bottom:20px") 
.container
  .row
    .col-sm-3.col-sm-offset-1
      = render 'dogs/home_info' 
    .col-sm-6 
      %section
        = render 'post_form' 
    #activities
        = render @activities
    %ul.pager
        %li= link_to_previous_page @activities, "Newer"
        %li= link_to_next_page @activities, "Older"

And :

#activities/_activity.html.haml
.activity= render_activity activity 
Uelb
  • 3,947
  • 2
  • 21
  • 32
  • Ok I will try taking @activities out and I put the index.html.haml up. Thanks – monty_lennie Mar 21 '14 at 08:56
  • Yeah taking activities out just crashes it. I'm pretty sure it needs activities for both scope and to give a collection. – monty_lennie Mar 21 '14 at 08:59
  • Wait, maybe I don't understand what you are trying to do, you want a form for each activity ? – Uelb Mar 21 '14 at 09:18
  • Ok I got rid of the extra partial and its still showing Completed 200 OK in 4663ms (Views: 4178.3ms | ActiveRecord: 41.7ms | Solr: 0.0ms). Whats funny is that its going up about 100.ms every time I refresh the index page. And to your last question every activity, I want to show the activity and add a comments form with a list of comments underneath it. – monty_lennie Mar 21 '14 at 09:26
  • public_activity/activities/_activity.html.haml (4340.2ms) – monty_lennie Mar 21 '14 at 09:30
  • 1
    You mean the single line file ? Try commenting each line one by one to find the line that is that long... – Uelb Mar 21 '14 at 09:34
  • Yeah so I guess it must be in one of the public_activity files (post, post_pic or profile_pic/_create.html.haml). I'll try commenting the lines out. Ahh I have to head out be back in few hours. Thanks for your help so far @Oxynum – monty_lennie Mar 21 '14 at 09:41