Skip to content

Commit 4bcac34

Browse files
authored
2781 translation page bug (#2782)
1 parent e214190 commit 4bcac34

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

.rubocop.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Metrics/BlockLength:
6060
- 'app/models/spotlight/resource.rb'
6161

6262
Metrics/ClassLength:
63-
Max: 120
63+
Max: 150
6464
Exclude:
6565
- 'app/models/spotlight/resources/iiif_manifest.rb'
6666
- 'app/controllers/spotlight/browse_controller.rb'

app/controllers/spotlight/pages_controller.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
module Spotlight
44
##
55
# Base CRUD controller for pages
6-
# rubocop:disable Metrics/ClassLength
7-
# Disableing class length because this is a base
8-
# controller that gives other controllers their behavior
96
class PagesController < Spotlight::ApplicationController
107
before_action :authenticate_user!, except: [:show]
118
before_action :load_locale_specific_page, only: %i[destroy edit show update]
@@ -32,7 +29,7 @@ def index
3229

3330
respond_to do |format|
3431
format.html
35-
format.json { render json: @pages.for_locale.published.to_json(methods: [:thumbnail_image_url]) }
32+
format.json { render json: @pages.for_default_locale.published.to_json(methods: [:thumbnail_image_url]) }
3633
end
3734
end
3835

@@ -208,5 +205,4 @@ def redirect_page_to_related_locale_version
208205
end
209206
end
210207
end
211-
# rubocop:enable Metrics/ClassLength
212208
end

app/helpers/spotlight/job_trackers_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Spotlight
44
# HTML <meta> tag helpers
55
module JobTrackersHelper
66
def job_status_icon(job_tracker)
7-
content_tag :span, title: t(job_tracker.status || 'missing', scope: 'spotlight.job_trackers.status') do # rubocop:disable Rails/ContentTag
7+
content_tag :span, title: t(job_tracker.status || 'missing', scope: 'spotlight.job_trackers.status') do
88
if job_tracker.enqueued? || job_tracker.in_progress?
99
'⏱'
1010
elsif job_tracker.completed?

app/models/sir_trevor_rails/blocks/featured_pages_block.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def page_options(id)
1212
end
1313

1414
def pages
15-
@pages ||= parent.exhibit.pages.published.where(slug: item_ids).sort do |a, b|
15+
@pages ||= parent.exhibit.pages.for_default_locale.published.where(slug: item_ids).sort do |a, b|
1616
ordered_items.index(a.slug) <=> ordered_items.index(b.slug)
1717
end
1818
end
@@ -28,7 +28,7 @@ def as_json(*)
2828

2929
result[:data][:item].transform_values! do |v|
3030
begin
31-
v['thumbnail_image_url'] = parent.exhibit.pages.find(v['id']).thumbnail_image_url
31+
v['thumbnail_image_url'] = parent.exhibit.pages.for_default_locale.find(v['id']).thumbnail_image_url
3232
rescue ActiveRecord::RecordNotFound
3333
v = nil
3434
end

app/models/spotlight/page.rb

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class Page < ActiveRecord::Base
4646

4747
after_update :update_translated_pages_weights_and_parent_page
4848

49+
def title
50+
return super if I18n.locale == I18n.default_locale
51+
52+
translated_page_for(I18n.locale)&.title || super
53+
end
54+
4955
def content_changed!
5056
@content = nil
5157
end
@@ -133,6 +139,8 @@ def updated_after?(other_page)
133139
end
134140

135141
def translated_page_for(locale)
142+
return self if locale == self.locale
143+
136144
translated_pages.for_locale(locale).first
137145
end
138146

spec/models/sir_trevor_rails/blocks/featured_pages_block_spec.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
describe SirTrevorRails::Blocks::FeaturedPagesBlock do
44
subject { described_class.new({ type: '', data: block_data }, page) }
55

6-
let(:page) { FactoryBot.create(:feature_page) }
6+
let(:page) { FactoryBot.create(:feature_page, exhibit: exhibit) }
7+
let(:exhibit) { FactoryBot.create(:exhibit) }
78
let(:block_data) { {} }
89

910
describe '#items' do
@@ -21,6 +22,23 @@
2122
end
2223
end
2324

25+
describe '#pages' do
26+
let!(:page_a) { FactoryBot.create(:feature_page, slug: 'a', exhibit: exhibit) }
27+
let!(:translated_page_a) { page_a.clone_for_locale('a').tap { |x| x.update(published: true) && x.save } }
28+
let!(:page_b) { FactoryBot.create(:feature_page, slug: 'b', exhibit: exhibit) }
29+
30+
before do
31+
block_data[:item] = {
32+
'0': { id: 'a', display: 'true' },
33+
'1': { id: 'b', display: 'true' }
34+
}
35+
end
36+
37+
it 'retrieves the pages from the default locale' do
38+
expect(subject.pages.length).to eq 2
39+
end
40+
end
41+
2442
describe '#as_json' do
2543
context 'when no items are present' do
2644
it 'returns an empty items value' do

0 commit comments

Comments
 (0)