From d2665e4ef0dfc490c04c63390fd0ae83251777af Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 26 Aug 2015 16:51:58 +0200 Subject: [PATCH 1/5] adds bullet to gemfile --- Gemfile | 1 + Gemfile.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/Gemfile b/Gemfile index 14029a3c9..e381bc516 100644 --- a/Gemfile +++ b/Gemfile @@ -63,6 +63,7 @@ group :development, :test do gem "capistrano-bundler", '1.1.4', require: false gem "capistrano-rails", '1.1.3', require: false gem "capistrano-rvm", require: false + gem "bullet" end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index adeb5d99f..94e26694c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,9 @@ GEM debug_inspector (>= 0.0.1) browser (0.9.1) builder (3.2.2) + bullet (4.14.7) + activesupport (>= 3.0.0) + uniform_notifier (~> 1.9.0) byebug (6.0.2) cancancan (1.12.0) capistrano (3.4.0) @@ -345,6 +348,7 @@ GEM kgio (~> 2.6) rack raindrops (~> 0.7) + uniform_notifier (1.9.0) user_agent_parser (2.2.0) uuidtools (2.1.5) warden (1.2.3) @@ -368,6 +372,7 @@ DEPENDENCIES acts_as_commentable_with_threading acts_as_votable ahoy_matey (~> 1.2.1) + bullet byebug cancancan capistrano (= 3.4.0) From 048f7c9b73aa3e7e6b94397dfad177e787a4c896 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 26 Aug 2015 17:24:08 +0200 Subject: [PATCH 2/5] Adds bullet to dev & test, but makes it visible only on-demmand The logs are always stored in log/bullet.log If you run the specs with `BULLET=true bin/rspec`: * Any feature test which makes bullet angry will fail If you run rails with `BULLET=true bin/rails s`: * It will print the bullet logs in both the rails log and the bullet log * It will show a footer on each page with the N+1 queries etc. --- config/environments/development.rb | 9 +++++++++ config/environments/test.rb | 9 +++++++++ spec/spec_helper.rb | 11 +++++++++++ 3 files changed, 29 insertions(+) diff --git a/config/environments/development.rb b/config/environments/development.rb index acf67286d..d2f4d1c4b 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -44,4 +44,13 @@ Rails.application.configure do # config.action_view.raise_on_missing_translations = true config.cache_store = :null_store + + config.after_initialize do + Bullet.enable = true + Bullet.bullet_logger = true + if ENV['BULLET'] + Bullet.rails_logger = true + Bullet.add_footer = true + end + end end diff --git a/config/environments/test.rb b/config/environments/test.rb index a76376fe6..cef568af6 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -45,4 +45,13 @@ Rails.application.configure do # config.action_view.raise_on_missing_translations = true config.cache_store = :null_store + + config.after_initialize do + Bullet.enable = true + Bullet.bullet_logger = true + if ENV['BULLET'] + Bullet.raise = true # raise an error if n+1 query occurs + end + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0e0b14960..bf1a65776 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,6 +27,15 @@ RSpec.configure do |config| DatabaseCleaner.clean end + config.before(:each, type: :feature) do + Bullet.start_request + end + + config.after(:each, type: :feature) do + Bullet.perform_out_of_channel_notifications if Bullet.notification? + Bullet.end_request + end + # Allows RSpec to persist some state between runs in order to support # the `--only-failures` and `--next-failure` CLI options. config.example_status_persistence_file_path = "spec/examples.txt" @@ -57,4 +66,6 @@ RSpec.configure do |config| # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed + + end From 9112cacde48f90f9e0d0b07ab0e8e171456a60a5 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 26 Aug 2015 19:40:37 +0200 Subject: [PATCH 3/5] Adds Debate#for_render --- app/controllers/debates_controller.rb | 2 +- app/models/debate.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index f8e7b968a..990f6489b 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -5,7 +5,7 @@ class DebatesController < ApplicationController respond_to :html, :js def index - @debates = Debate.includes(:tags).includes(:inappropiate_flags).search(params).page(params[:page]) + @debates = Debate.search(params).page(params[:page]).for_render set_debate_votes(@debates) end diff --git a/app/models/debate.rb b/app/models/debate.rb index 7b43bc71c..15590fc60 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -27,6 +27,7 @@ class Debate < ActiveRecord::Base scope :pending, -> { where(archived_at: nil, hidden_at: nil) } scope :archived, -> { where("archived_at IS NOT NULL AND hidden_at IS NULL") } scope :flagged_as_inappropiate, -> { where("inappropiate_flags_count > 0") } + scope :for_render, -> { includes(:tags) } # Ahoy setup visitable # Ahoy will automatically assign visit_id on create From 48ef4df0c31497e391595cc10eb23844c21cfd2a Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 26 Aug 2015 19:41:42 +0200 Subject: [PATCH 4/5] Adds Comment#for_render --- app/controllers/debates_controller.rb | 2 +- app/models/comment.rb | 3 ++- app/views/comments/_comment.html.erb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 990f6489b..cf6d14f65 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -11,7 +11,7 @@ class DebatesController < ApplicationController def show set_debate_votes(@debate) - @comments = @debate.root_comments.with_hidden.includes(:inappropiate_flags).recent.page(params[:page]) + @comments = @debate.root_comments.recent.page(params[:page]).for_render end def new diff --git a/app/models/comment.rb b/app/models/comment.rb index dca894b7c..db8fb35fc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -14,7 +14,6 @@ class Comment < ActiveRecord::Base has_many :inappropiate_flags, :as => :flaggable - default_scope { includes(:user) } scope :recent, -> { order(id: :desc) } scope :sorted_for_moderation, -> { order(inappropiate_flags_count: :desc, updated_at: :desc) } @@ -22,6 +21,8 @@ class Comment < ActiveRecord::Base scope :archived, -> { where("archived_at IS NOT NULL AND hidden_at IS NULL") } scope :flagged_as_inappropiate, -> { where("inappropiate_flags_count > 0") } + scope :for_render, -> { with_hidden.includes(user: :organization) } + def self.build(commentable, user, body) new commentable: commentable, user_id: user.id, diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 1b6f713ba..5ffff4a85 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -95,7 +95,7 @@ <% end %>
- <%= render comment.children.with_hidden.reorder('id DESC, lft') %> + <%= render comment.children.for_render.reorder('id DESC, lft') %>
From b9634cffabe3338ae9cd6886a46b8750fa7643a9 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 26 Aug 2015 19:41:55 +0200 Subject: [PATCH 5/5] Adds User#for_render --- app/controllers/admin/officials_controller.rb | 6 +++--- app/models/user.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/officials_controller.rb b/app/controllers/admin/officials_controller.rb index 023f0f7fc..f83aa1e18 100644 --- a/app/controllers/admin/officials_controller.rb +++ b/app/controllers/admin/officials_controller.rb @@ -1,11 +1,11 @@ class Admin::OfficialsController < Admin::BaseController def index - @officials = User.officials.page(params[:page]) + @officials = User.officials.page(params[:page]).for_render end def search - @users = User.with_email(params[:email]).page(params[:page]) + @users = User.with_email(params[:email]).page(params[:page]).for_render end def edit @@ -29,4 +29,4 @@ class Admin::OfficialsController < Admin::BaseController params.require(:user).permit(:official_position, :official_level) end -end \ No newline at end of file +end diff --git a/app/models/user.rb b/app/models/user.rb index 7cff8d732..d7b77a7e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,11 +25,11 @@ class User < ActiveRecord::Base accepts_nested_attributes_for :organization - default_scope { includes(:organization) } scope :administrators, -> { joins(:administrators) } scope :moderators, -> { joins(:moderator) } scope :organizations, -> { joins(:organization) } scope :officials, -> { where("official_level > 0") } + scope :for_render, -> { includes(:organization) } def self.find_for_oauth(auth, signed_in_resource = nil) # Get the identity and user if they exist