Commit b4435ba8e9e80ca372676e7ba3264d3cde59af31
1 parent
7e1172c4
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Added includes in models to prevent n+1 queries
N+1 queries found with the help of bullet gem
Showing
9 changed files
with
15 additions
and
15 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
| @@ -59,7 +59,7 @@ class CmsController < MyProfileController | @@ -59,7 +59,7 @@ class CmsController < MyProfileController | ||
| 59 | conditions = ['type != ?', 'RssFeed'] | 59 | conditions = ['type != ?', 'RssFeed'] |
| 60 | end | 60 | end |
| 61 | 61 | ||
| 62 | - @articles = @article.children.reorder("case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC, name").paginate( | 62 | + @articles = @article.children.includes(:parent).reorder("case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC, name").paginate( |
| 63 | :conditions => conditions, | 63 | :conditions => conditions, |
| 64 | :per_page => per_page, | 64 | :per_page => per_page, |
| 65 | :page => params[:npage] | 65 | :page => params[:npage] |
app/controllers/public/content_viewer_controller.rb
| @@ -221,7 +221,7 @@ class ContentViewerController < ApplicationController | @@ -221,7 +221,7 @@ class ContentViewerController < ApplicationController | ||
| 221 | # relation. | 221 | # relation. |
| 222 | posts = posts.native_translations if blog_with_translation?(@page) | 222 | posts = posts.native_translations if blog_with_translation?(@page) |
| 223 | 223 | ||
| 224 | - @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a | 224 | + @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).includes(:author).to_a |
| 225 | 225 | ||
| 226 | if blog_with_translation?(@page) | 226 | if blog_with_translation?(@page) |
| 227 | @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact | 227 | @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact |
| @@ -260,7 +260,7 @@ class ContentViewerController < ApplicationController | @@ -260,7 +260,7 @@ class ContentViewerController < ApplicationController | ||
| 260 | end | 260 | end |
| 261 | 261 | ||
| 262 | def process_comments(params) | 262 | def process_comments(params) |
| 263 | - @comments = @page.comments.without_spam | 263 | + @comments = @page.comments.without_spam.includes(:source) |
| 264 | @comments = @plugins.filter(:unavailable_comments, @comments) | 264 | @comments = @plugins.filter(:unavailable_comments, @comments) |
| 265 | @comments_count = @comments.count | 265 | @comments_count = @comments.count |
| 266 | @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) | 266 | @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) |
app/models/article.rb
| @@ -58,10 +58,10 @@ class Article < ActiveRecord::Base | @@ -58,10 +58,10 @@ class Article < ActiveRecord::Base | ||
| 58 | belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' | 58 | belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' |
| 59 | belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' | 59 | belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' |
| 60 | 60 | ||
| 61 | - has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' | 61 | + has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'comments.created_at asc', :include => [:author, :children] |
| 62 | 62 | ||
| 63 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] | 63 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] |
| 64 | - has_many :categories, :through => :article_categorizations | 64 | + has_many :categories, :through => :article_categorizations, :include => [:environment] |
| 65 | 65 | ||
| 66 | has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization' | 66 | has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization' |
| 67 | has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category | 67 | has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category |
| @@ -236,7 +236,7 @@ class Article < ActiveRecord::Base | @@ -236,7 +236,7 @@ class Article < ActiveRecord::Base | ||
| 236 | # retrieves all articles belonging to the given +profile+ that are not | 236 | # retrieves all articles belonging to the given +profile+ that are not |
| 237 | # sub-articles of any other article. | 237 | # sub-articles of any other article. |
| 238 | scope :top_level_for, lambda { |profile| | 238 | scope :top_level_for, lambda { |profile| |
| 239 | - {:conditions => [ 'parent_id is null and profile_id = ?', profile.id ]} | 239 | + {:conditions => [ 'parent_id is null and profile_id = ?', profile.id ], :include => [:profile]} |
| 240 | } | 240 | } |
| 241 | 241 | ||
| 242 | scope :public, | 242 | scope :public, |
| @@ -460,7 +460,7 @@ class Article < ActiveRecord::Base | @@ -460,7 +460,7 @@ class Article < ActiveRecord::Base | ||
| 460 | end | 460 | end |
| 461 | 461 | ||
| 462 | scope :published, :conditions => ['articles.published = ?', true] | 462 | scope :published, :conditions => ['articles.published = ?', true] |
| 463 | - scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types] }} | 463 | + scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types], :include => [:parent] }} |
| 464 | scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}} | 464 | scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}} |
| 465 | scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ] | 465 | scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ] |
| 466 | scope :images, :conditions => { :is_image => true } | 466 | scope :images, :conditions => { :is_image => true } |
| @@ -469,7 +469,7 @@ class Article < ActiveRecord::Base | @@ -469,7 +469,7 @@ class Article < ActiveRecord::Base | ||
| 469 | 469 | ||
| 470 | scope :more_popular, :order => 'hits DESC' | 470 | scope :more_popular, :order => 'hits DESC' |
| 471 | scope :more_comments, :order => "comments_count DESC" | 471 | scope :more_comments, :order => "comments_count DESC" |
| 472 | - scope :more_recent, :order => "created_at DESC" | 472 | + scope :more_recent, :order => "articles.created_at DESC" |
| 473 | 473 | ||
| 474 | def self.display_filter(user, profile) | 474 | def self.display_filter(user, profile) |
| 475 | return {:conditions => ['articles.published = ?', true]} if !user | 475 | return {:conditions => ['articles.published = ?', true]} if !user |
app/models/category.rb
| @@ -16,7 +16,7 @@ class Category < ActiveRecord::Base | @@ -16,7 +16,7 @@ class Category < ActiveRecord::Base | ||
| 16 | 16 | ||
| 17 | # Finds all top level categories for a given environment. | 17 | # Finds all top level categories for a given environment. |
| 18 | scope :top_level_for, lambda { |environment| | 18 | scope :top_level_for, lambda { |environment| |
| 19 | - {:conditions => ['parent_id is null and environment_id = ?', environment.id ]} | 19 | + {:conditions => ['parent_id is null and environment_id = ?', environment.id ], :include => [:children]} |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | scope :on_level, lambda { |parent| {:conditions => {:parent_id => parent}} } | 22 | scope :on_level, lambda { |parent| {:conditions => {:parent_id => parent}} } |
app/models/comment.rb
| @@ -18,7 +18,7 @@ class Comment < ActiveRecord::Base | @@ -18,7 +18,7 @@ class Comment < ActiveRecord::Base | ||
| 18 | has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy | 18 | has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy |
| 19 | belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' | 19 | belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' |
| 20 | 20 | ||
| 21 | - scope :without_reply, :conditions => ['reply_of_id IS NULL'] | 21 | + scope :without_reply, :conditions => ['comments.reply_of_id IS NULL'] |
| 22 | 22 | ||
| 23 | # unauthenticated authors: | 23 | # unauthenticated authors: |
| 24 | validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) | 24 | validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) |
app/models/person.rb
| @@ -434,7 +434,7 @@ class Person < Profile | @@ -434,7 +434,7 @@ class Person < Profile | ||
| 434 | end | 434 | end |
| 435 | 435 | ||
| 436 | def already_reported?(profile) | 436 | def already_reported?(profile) |
| 437 | - abuse_reports.any? { |report| report.abuse_complaint.reported == profile && report.abuse_complaint.opened? } | 437 | + abuse_reports.includes({:abuse_complaint => :reported}).any? { |report| report.abuse_complaint.reported == profile && report.abuse_complaint.opened? } |
| 438 | end | 438 | end |
| 439 | 439 | ||
| 440 | def register_report(abuse_report, profile) | 440 | def register_report(abuse_report, profile) |
app/models/profile.rb
| @@ -190,7 +190,7 @@ class Profile < ActiveRecord::Base | @@ -190,7 +190,7 @@ class Profile < ActiveRecord::Base | ||
| 190 | belongs_to :preferred_domain, :class_name => 'Domain', :foreign_key => 'preferred_domain_id' | 190 | belongs_to :preferred_domain, :class_name => 'Domain', :foreign_key => 'preferred_domain_id' |
| 191 | belongs_to :environment | 191 | belongs_to :environment |
| 192 | 192 | ||
| 193 | - has_many :articles, :dependent => :destroy | 193 | + has_many :articles, :dependent => :destroy, :include => [:profile] |
| 194 | belongs_to :home_page, :class_name => Article.name, :foreign_key => 'home_page_id' | 194 | belongs_to :home_page, :class_name => Article.name, :foreign_key => 'home_page_id' |
| 195 | 195 | ||
| 196 | has_many :files, :class_name => 'UploadedFile' | 196 | has_many :files, :class_name => 'UploadedFile' |
lib/acts_as_having_posts.rb
| @@ -2,7 +2,7 @@ module ActsAsHavingPosts | @@ -2,7 +2,7 @@ module ActsAsHavingPosts | ||
| 2 | 2 | ||
| 3 | module ClassMethods | 3 | module ClassMethods |
| 4 | def acts_as_having_posts(options = {}) | 4 | def acts_as_having_posts(options = {}) |
| 5 | - has_many :posts, { :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'articles.type != ?', 'RssFeed' ], :order => 'published_at DESC, id DESC' }.merge(options) | 5 | + has_many :posts, { :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'articles.type != ?', 'RssFeed' ], :order => 'published_at DESC, id DESC', :include => [:parent, :profile] }.merge(options) |
| 6 | 6 | ||
| 7 | attr_accessor :feed_attrs | 7 | attr_accessor :feed_attrs |
| 8 | 8 |
lib/spammable.rb
| @@ -12,8 +12,8 @@ module Spammable | @@ -12,8 +12,8 @@ module Spammable | ||
| 12 | def self.extended (base) | 12 | def self.extended (base) |
| 13 | if base.respond_to?(:scope) | 13 | if base.respond_to?(:scope) |
| 14 | base.class_eval do | 14 | base.class_eval do |
| 15 | - scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false] | ||
| 16 | - scope :spam, :conditions => ['spam = ?', true] | 15 | + scope :without_spam, :conditions => ["#{base.table_name}.spam IS NULL OR #{base.table_name}.spam = ?", false] |
| 16 | + scope :spam, :conditions => ["#{base.table_name}.spam = ?", true] | ||
| 17 | end | 17 | end |
| 18 | end | 18 | end |
| 19 | end | 19 | end |