From a82c83235488b5b752516d292764495f344cf6cb Mon Sep 17 00:00:00 2001 From: echarp Date: Sat, 8 Feb 2020 15:16:57 +0100 Subject: [PATCH] Optimisations --- app/controllers/events_controller.rb | 6 ++---- app/controllers/tags_controller.rb | 2 +- app/models/event.rb | 14 ++++++-------- app/models/orga.rb | 1 + db/seeds.rb | 4 ++-- test/controllers/events_controller_test.rb | 4 ++-- test/models/event_callbacks_test.rb | 4 ++-- test/models/event_test.rb | 4 ++-- 8 files changed, 18 insertions(+), 21 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 6dde7bc4..1eb2fd7c 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -83,10 +83,8 @@ class EventsController < ApplicationController private def set_events - # The 3000 limit is purely arbitrary... - @events = apply_scopes Event.moderated.order('id desc') - .limit(params[:format] == 'rss' ? 20 : 3000) - @events = @events.includes :region if params[:format].present? + @events = apply_scopes Event.moderated + @events = @events.limit(20) if params[:format] == 'rss' end def new_event diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 81f47604..deb5fdaa 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -36,7 +36,7 @@ class TagsController < InheritedResources::Base @orgas = (apply_scopes(Orga.moderated.active) + Orga.moderated.active .where( - 'lower(name) like lower(?)', + 'lower(orgas.name) like lower(?)', "%#{params[:id].tr '-', '%'}%" ) ).uniq diff --git a/app/models/event.rb b/app/models/event.rb index 1e21b478..152de3b1 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -42,7 +42,10 @@ class Event < ApplicationRecord after_destroy EventCallbacks - default_scope { includes(:taggings, :region) } + # The 3000 limit is purely arbitrary... + default_scope do + joins(:region).includes(:base_tags).order('events.id desc').limit(3000) + end scope :moderated, ->(*) { where moderated: true } scope :unmoderated, ->(*) { where moderated: false } scope :past, -> { where 'start_time <= ?', Time.zone.now } @@ -60,8 +63,7 @@ class Event < ApplicationRecord end) scope :period, (lambda do |year, week| start_date = Date.commercial( - year.to_i, - (week || (Time.zone.today + 7.days).cweek).to_i + year.to_i, (week || (Time.zone.today + 7.days).cweek).to_i ) where '? <= end_time and start_time <= ?', start_date, start_date.end_of_week.end_of_day @@ -79,9 +81,6 @@ class Event < ApplicationRecord before_validation on: :create do self.submission_time = Time.zone.now self.decision_time = Time.zone.now - - # Populate submitter using contact info if absent - self.submitter ||= contact end before_validation on: :update do @@ -110,8 +109,7 @@ class Event < ApplicationRecord def to_tweet url = Rails.application.routes.url_helpers.event_url( - self, - host: ActionMailer::Base.default_url_options[:host] + self, host: ActionMailer::Base.default_url_options[:host] ) tweet = "#{self} #{url}" diff --git a/app/models/orga.rb b/app/models/orga.rb index 7ab653d8..8dc76bd2 100644 --- a/app/models/orga.rb +++ b/app/models/orga.rb @@ -21,6 +21,7 @@ class Orga < ApplicationRecord # after_validation :geocode, if: -> (obj) { obj.saved_change_to_address? } after_validation :geocode + default_scope { includes(:region, :kind, :base_tags).order('orgas.id desc') } scope :active, ->(value = true) { where active: value } scope :inactive, ->(value = false) { where active: value } scope :moderated, -> { where moderated: true } diff --git a/db/seeds.rb b/db/seeds.rb index fe6eb01f..898305be 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -22,7 +22,7 @@ Kind.create name: 'glug', icon: 'support' Kind.create name: 'provider', icon: 'tty' Kind.create name: 'institution', icon: 'institution' -# rubocop:disable Metrics/LineLength +# rubocop:disable Layout/LineLength I18n::Backend::ActiveRecord::Translation.create( [ { locale: 'fr', key: 'mail_suffix', value: '[AdL] ' }, @@ -410,4 +410,4 @@ Ces recommandations de modération sont à discuter et à améliorer au fur et " } ] ) -# rubocop:enable Metrics/LineLength +# rubocop:enable Layout/LineLength diff --git a/test/controllers/events_controller_test.rb b/test/controllers/events_controller_test.rb index eaf6faf6..e6774fe7 100644 --- a/test/controllers/events_controller_test.rb +++ b/test/controllers/events_controller_test.rb @@ -24,7 +24,7 @@ class EventsControllerTest < ActionDispatch::IntegrationTest start_time: @event.start_time, end_time: @event.end_time, description: @event.description, city: @event.city, region_id: @event.region.id, - url: @event.url, contact: @event.contact, tag_list: 'helo world' + url: @event.url, submitter: @event.contact, tag_list: 'helo world' } } assert_empty assigns(:event).errors @@ -40,7 +40,7 @@ class EventsControllerTest < ActionDispatch::IntegrationTest start_time: @event.start_time, end_time: @event.end_time, description: @event.description, city: @event.city, region_id: @event.region.id, - url: @event.url, contact: @event.contact, tag_list: 'helo world' + url: @event.url, submitter: @event.contact, tag_list: 'helo world' } } assert_empty assigns(:event).errors.messages diff --git a/test/models/event_callbacks_test.rb b/test/models/event_callbacks_test.rb index 36c511af..9315bcf0 100644 --- a/test/models/event_callbacks_test.rb +++ b/test/models/event_callbacks_test.rb @@ -13,7 +13,7 @@ class EventCallbacksTest < ActiveSupport::TestCase description: 'et hop!', city: City.first, region: Region.first, url: 'http://example.com', - contact: 'contact@example.com', + submitter: 'contact@example.com', tag_list: 'hello world' ) assert_difference 'Event.count' do @@ -29,7 +29,7 @@ class EventCallbacksTest < ActiveSupport::TestCase description: 'et hop!', city: City.first, region: Region.first, url: 'http://example.com', - contact: 'contact@example.com', + submitter: 'contact@example.com', tag_list: 'hello world' ) diff --git a/test/models/event_test.rb b/test/models/event_test.rb index 2a886327..8cfe8d33 100644 --- a/test/models/event_test.rb +++ b/test/models/event_test.rb @@ -17,7 +17,6 @@ class EventTest < ActiveSupport::TestCase city: City.first, region: Region.first, url: 'http://example.com', - contact: 'contact@example.com', submitter: 'submitter@example.com', tag_list: 'hello world' ) @@ -40,6 +39,7 @@ class EventTest < ActiveSupport::TestCase region: Region.first, url: 'http://example.com', contact: 'contact@example.com', + submitter: 'contact@example.com', tag_list: 'hello world' ) @@ -69,7 +69,7 @@ class EventTest < ActiveSupport::TestCase city: City.first, region: Region.first, url: 'http://example.com', - contact: 'contact@example.com', + submitter: 'contact@example.com', tag_list: 'hello world' )