Skip to content

Commit 10d7795

Browse files
committed
Remove default scopes; rewrite migrations to be more robust against model changes
1 parent b217b91 commit 10d7795

24 files changed

+70
-72
lines changed

app/controllers/facets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class FacetsController < ApplicationController
33

44
def index
55
# TODO: Is there a way to do this with fewer SQL queries?
6-
@facets = Facet.includes(terms: %i[parent children]).all
6+
@facets = Facet.order(:ord).includes(terms: %i[parent children]).all
77
render jsonapi: @facets
88
end
99
end

app/controllers/items_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def filtered_items
9393
items = items.where(suppressed: false)
9494
items = items.with_all_keywords(keywords) if keywords
9595
items.includes(:terms)
96+
# TODO: add sorting to API
97+
# TODO: allow sorting by facet values
98+
items.order(:artist, :title, :date)
9699
end
97100
end
98101
end

app/models/facet.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ class Facet < ApplicationRecord
1111

1212
validates :name, presence: true
1313
validates :name, uniqueness: true
14-
15-
# ------------------------------------------------------------
16-
# Scopes
17-
18-
default_scope { order(:ord) }
1914
end

app/models/item.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ class Item < ApplicationRecord
3030
# ------------------------------------------------------------
3131
# Scopes
3232

33-
# TODO: add sorting to API
34-
# TODO: allow sorting by facet values
35-
default_scope { order(:artist, :title, :date) }
36-
3733
pg_search_scope(
3834
:with_all_keywords,
3935
against: { title: 'A', artist: 'B', description: 'C', date: 'D' },

app/models/term.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class Term < ApplicationRecord
1919
# ------------------------------------------------------------
2020
# Scopes
2121

22-
default_scope { order(:facet_id, :parent_id, :ord, :value) }
23-
2422
scope :find_by_self_or_parent, ->(**conditions) {
2523
sql = <<~SQL.squish
2624
INNER JOIN (#{Term.where(**conditions).to_sql})#{' '}

app/serializers/facet_serializer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ class FacetSerializer
33

44
attributes(:name)
55

6-
has_many :terms
6+
has_many :terms do |facet|
7+
# NOTE: We don't do this with a default_scope on Term because that
8+
# will break migrations written before :ord was added
9+
facet.terms.order(:ord, :value)
10+
end
711
end

config/environments/production.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
}
8787
config.action_mailer.smtp_settings = smtp_settings
8888
config.after_initialize do
89-
settings_cleaned = smtp_settings.merge(password: mail_smtp_password.gsub(/./, '*'))
90-
Rails.logger.info('SMTP configured', smtp_settings: settings_cleaned)
89+
settings_cleaned = smtp_settings.merge(password: mail_smtp_password&.gsub(/./, '*'))
90+
Rails.logger.info('SMTP configured', smtp_settings: settings_cleaned.inspect)
9191
end
9292

9393
if ENV['INTERCEPT_EMAILS'].present?

db/migrate/20220311190417_populate_items_table.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Ensure migration can run without error even if we delete/rename the models
2-
(class Item < ActiveRecord::Base; end) unless defined?(Item)
3-
41
class PopulateItemsTable < ActiveRecord::Migration[7.0]
52
DATA = [
63
{ id: 1, image: 'anonymous (apples).jpg', image_base: 'anonymous (apples)', title: 'Apples', artist: 'Unknown', artist_url: nil, date: '1876-1885', decade: 'Before 1900', description: '(Plate xxix) Published by H. Bull & R. Hogg for the Herefordshire Pomona, London.', medium: 'Lithograph', colors: 'Color', genre: 'Still Life', dimensions: '16.5 x 20.5"', size: 'Small', series: nil, bib_number: 'b16390480', mms_id: '991051103819706532', barcode: 'C093330688', circulation: nil, location: nil, value: '100', appraisal_date: '2006', notes: nil, reserve_date: '2019-08-29' },
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Ensure migration can run without error even if we delete/rename the models
2-
(class Item < ActiveRecord::Base; end) unless defined?(Item)
3-
41
class DropImageBase < ActiveRecord::Migration[7.0]
52
def up
63
remove_column(:items, :image_base)
74
end
85

96
def down
107
add_column(:items, :image_base, :string)
8+
Item.reset_column_information
9+
1110
Item.update_all("image_base = REGEXP_REPLACE(image, '\\.jpg$', '', 'i')")
1211
end
1312
end

db/migrate/20220406203912_drop_bib_number.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def up
55

66
def down
77
add_column(:items, :bib_number, :string)
8+
Item.reset_column_information
89

910
stmt = <<~SQL.strip
1011
UPDATE items

0 commit comments

Comments
 (0)