Optimisations

This commit is contained in:
echarp 2020-02-08 15:16:57 +01:00
parent 566929a3f5
commit a82c832354
8 changed files with 18 additions and 21 deletions

View File

@ -83,10 +83,8 @@ class EventsController < ApplicationController
private private
def set_events def set_events
# The 3000 limit is purely arbitrary... @events = apply_scopes Event.moderated
@events = apply_scopes Event.moderated.order('id desc') @events = @events.limit(20) if params[:format] == 'rss'
.limit(params[:format] == 'rss' ? 20 : 3000)
@events = @events.includes :region if params[:format].present?
end end
def new_event def new_event

View File

@ -36,7 +36,7 @@ class TagsController < InheritedResources::Base
@orgas = (apply_scopes(Orga.moderated.active) + @orgas = (apply_scopes(Orga.moderated.active) +
Orga.moderated.active Orga.moderated.active
.where( .where(
'lower(name) like lower(?)', 'lower(orgas.name) like lower(?)',
"%#{params[:id].tr '-', '%'}%" "%#{params[:id].tr '-', '%'}%"
) )
).uniq ).uniq

View File

@ -42,7 +42,10 @@ class Event < ApplicationRecord
after_destroy EventCallbacks 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 :moderated, ->(*) { where moderated: true }
scope :unmoderated, ->(*) { where moderated: false } scope :unmoderated, ->(*) { where moderated: false }
scope :past, -> { where 'start_time <= ?', Time.zone.now } scope :past, -> { where 'start_time <= ?', Time.zone.now }
@ -60,8 +63,7 @@ class Event < ApplicationRecord
end) end)
scope :period, (lambda do |year, week| scope :period, (lambda do |year, week|
start_date = Date.commercial( start_date = Date.commercial(
year.to_i, year.to_i, (week || (Time.zone.today + 7.days).cweek).to_i
(week || (Time.zone.today + 7.days).cweek).to_i
) )
where '? <= end_time and start_time <= ?', where '? <= end_time and start_time <= ?',
start_date, start_date.end_of_week.end_of_day start_date, start_date.end_of_week.end_of_day
@ -79,9 +81,6 @@ class Event < ApplicationRecord
before_validation on: :create do before_validation on: :create do
self.submission_time = Time.zone.now self.submission_time = Time.zone.now
self.decision_time = Time.zone.now self.decision_time = Time.zone.now
# Populate submitter using contact info if absent
self.submitter ||= contact
end end
before_validation on: :update do before_validation on: :update do
@ -110,8 +109,7 @@ class Event < ApplicationRecord
def to_tweet def to_tweet
url = Rails.application.routes.url_helpers.event_url( url = Rails.application.routes.url_helpers.event_url(
self, self, host: ActionMailer::Base.default_url_options[:host]
host: ActionMailer::Base.default_url_options[:host]
) )
tweet = "#{self} #{url}" tweet = "#{self} #{url}"

View File

@ -21,6 +21,7 @@ class Orga < ApplicationRecord
# after_validation :geocode, if: -> (obj) { obj.saved_change_to_address? } # after_validation :geocode, if: -> (obj) { obj.saved_change_to_address? }
after_validation :geocode after_validation :geocode
default_scope { includes(:region, :kind, :base_tags).order('orgas.id desc') }
scope :active, ->(value = true) { where active: value } scope :active, ->(value = true) { where active: value }
scope :inactive, ->(value = false) { where active: value } scope :inactive, ->(value = false) { where active: value }
scope :moderated, -> { where moderated: true } scope :moderated, -> { where moderated: true }

View File

@ -22,7 +22,7 @@ Kind.create name: 'glug', icon: 'support'
Kind.create name: 'provider', icon: 'tty' Kind.create name: 'provider', icon: 'tty'
Kind.create name: 'institution', icon: 'institution' Kind.create name: 'institution', icon: 'institution'
# rubocop:disable Metrics/LineLength # rubocop:disable Layout/LineLength
I18n::Backend::ActiveRecord::Translation.create( I18n::Backend::ActiveRecord::Translation.create(
[ [
{ locale: 'fr', key: 'mail_suffix', value: '[AdL] ' }, { 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

View File

@ -24,7 +24,7 @@ class EventsControllerTest < ActionDispatch::IntegrationTest
start_time: @event.start_time, end_time: @event.end_time, start_time: @event.start_time, end_time: @event.end_time,
description: @event.description, description: @event.description,
city: @event.city, region_id: @event.region.id, 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 assert_empty assigns(:event).errors
@ -40,7 +40,7 @@ class EventsControllerTest < ActionDispatch::IntegrationTest
start_time: @event.start_time, end_time: @event.end_time, start_time: @event.start_time, end_time: @event.end_time,
description: @event.description, description: @event.description,
city: @event.city, region_id: @event.region.id, 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 assert_empty assigns(:event).errors.messages

View File

@ -13,7 +13,7 @@ class EventCallbacksTest < ActiveSupport::TestCase
description: 'et hop!', description: 'et hop!',
city: City.first, region: Region.first, city: City.first, region: Region.first,
url: 'http://example.com', url: 'http://example.com',
contact: 'contact@example.com', submitter: 'contact@example.com',
tag_list: 'hello world' tag_list: 'hello world'
) )
assert_difference 'Event.count' do assert_difference 'Event.count' do
@ -29,7 +29,7 @@ class EventCallbacksTest < ActiveSupport::TestCase
description: 'et hop!', description: 'et hop!',
city: City.first, region: Region.first, city: City.first, region: Region.first,
url: 'http://example.com', url: 'http://example.com',
contact: 'contact@example.com', submitter: 'contact@example.com',
tag_list: 'hello world' tag_list: 'hello world'
) )

View File

@ -17,7 +17,6 @@ class EventTest < ActiveSupport::TestCase
city: City.first, city: City.first,
region: Region.first, region: Region.first,
url: 'http://example.com', url: 'http://example.com',
contact: 'contact@example.com',
submitter: 'submitter@example.com', submitter: 'submitter@example.com',
tag_list: 'hello world' tag_list: 'hello world'
) )
@ -40,6 +39,7 @@ class EventTest < ActiveSupport::TestCase
region: Region.first, region: Region.first,
url: 'http://example.com', url: 'http://example.com',
contact: 'contact@example.com', contact: 'contact@example.com',
submitter: 'contact@example.com',
tag_list: 'hello world' tag_list: 'hello world'
) )
@ -69,7 +69,7 @@ class EventTest < ActiveSupport::TestCase
city: City.first, city: City.first,
region: Region.first, region: Region.first,
url: 'http://example.com', url: 'http://example.com',
contact: 'contact@example.com', submitter: 'contact@example.com',
tag_list: 'hello world' tag_list: 'hello world'
) )