blacklight-spotlight 3.0.0.alpha.9 → 3.0.0.alpha.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/app/assets/javascripts/spotlight/admin/blocks/browse_block.js +55 -1
  4. data/app/assets/javascripts/spotlight/admin/blocks/uploaded_items_block.js +7 -2
  5. data/app/assets/javascripts/spotlight/admin/crop.es6 +5 -0
  6. data/app/assets/javascripts/spotlight/admin/sir-trevor/locales.js +3 -2
  7. data/app/assets/stylesheets/spotlight/_accessibility.scss +1 -1
  8. data/app/assets/stylesheets/spotlight/_browse.scss +16 -0
  9. data/app/assets/stylesheets/spotlight/_curation.scss +6 -0
  10. data/app/assets/stylesheets/spotlight/_featured_browse_categories_block.scss +3 -0
  11. data/app/assets/stylesheets/spotlight/_header.scss +1 -1
  12. data/app/assets/stylesheets/spotlight/_translations.scss +1 -0
  13. data/app/controllers/spotlight/appearances_controller.rb +1 -0
  14. data/app/controllers/spotlight/browse_controller.rb +1 -1
  15. data/app/controllers/spotlight/exhibits_controller.rb +2 -3
  16. data/app/controllers/spotlight/featured_images_controller.rb +1 -1
  17. data/app/controllers/spotlight/pages_controller.rb +1 -1
  18. data/app/controllers/spotlight/searches_controller.rb +3 -2
  19. data/app/controllers/spotlight/translations_controller.rb +46 -0
  20. data/app/helpers/spotlight/crop_helper.rb +4 -1
  21. data/app/models/concerns/spotlight/translatables.rb +17 -1
  22. data/app/models/spotlight/about_page.rb +3 -1
  23. data/app/models/spotlight/exhibit.rb +4 -2
  24. data/app/models/spotlight/feature_page.rb +3 -1
  25. data/app/models/spotlight/featured_image.rb +29 -12
  26. data/app/models/spotlight/home_page.rb +3 -1
  27. data/app/models/spotlight/main_navigation.rb +2 -2
  28. data/app/models/spotlight/masthead.rb +1 -1
  29. data/app/models/spotlight/page.rb +5 -1
  30. data/app/models/spotlight/search.rb +5 -1
  31. data/app/models/spotlight/solr_document_sidecar.rb +7 -5
  32. data/app/models/spotlight/temporary_image.rb +8 -0
  33. data/app/services/spotlight/exhibit_import_export_service.rb +454 -0
  34. data/app/views/catalog/_save_search.html.erb +1 -1
  35. data/app/views/spotlight/browse/_search.html.erb +3 -2
  36. data/app/views/spotlight/browse/_search_title.html.erb +2 -1
  37. data/app/views/spotlight/pages/_form.html.erb +1 -1
  38. data/app/views/spotlight/searches/_form.html.erb +1 -0
  39. data/app/views/spotlight/searches/_search.html.erb +1 -0
  40. data/app/views/spotlight/sir_trevor/blocks/_browse_block.html.erb +1 -0
  41. data/app/views/spotlight/sir_trevor/blocks/_uploaded_items_block.html.erb +7 -1
  42. data/app/views/spotlight/translations/_browse_categories.html.erb +29 -3
  43. data/app/views/spotlight/translations/_general.html.erb +7 -7
  44. data/app/views/spotlight/translations/_import.html.erb +24 -0
  45. data/app/views/spotlight/translations/_metadata.html.erb +1 -1
  46. data/app/views/spotlight/translations/_page.html.erb +5 -5
  47. data/app/views/spotlight/translations/_pages.html.erb +4 -4
  48. data/app/views/spotlight/translations/_pages_table.html.erb +5 -5
  49. data/app/views/spotlight/translations/_search_fields.html.erb +3 -3
  50. data/app/views/spotlight/translations/edit.html.erb +8 -6
  51. data/app/views/spotlight/translations/show.yaml.yamlbuilder +81 -0
  52. data/config/locales/spotlight.ar.yml +43 -24
  53. data/config/locales/spotlight.en.yml +65 -53
  54. data/config/routes.rb +6 -1
  55. data/db/migrate/20200403161512_add_subtitle_to_searches.rb +7 -0
  56. data/lib/spotlight/engine.rb +20 -0
  57. data/lib/spotlight/version.rb +1 -1
  58. data/spec/controllers/spotlight/home_pages_controller_spec.rb +1 -1
  59. data/spec/controllers/spotlight/translations_controller_spec.rb +53 -2
  60. data/spec/factories/featured_images.rb +4 -0
  61. data/spec/features/exhibits/translation_editing_spec.rb +2 -2
  62. data/spec/features/home_page_spec.rb +9 -0
  63. data/spec/features/javascript/blocks/uploaded_items_block_spec.rb +4 -1
  64. data/spec/i18n_spec.rb +0 -2
  65. data/spec/models/spotlight/exhibit_spec.rb +9 -1
  66. data/spec/models/spotlight/featured_image_spec.rb +28 -0
  67. data/spec/models/spotlight/page_spec.rb +6 -1
  68. data/spec/models/spotlight/resources/upload_spec.rb +43 -79
  69. data/spec/{serializers/spotlight/exhibit_export_serializer_spec.rb → services/spotlight/exhibit_import_export_service_spec.rb} +108 -22
  70. data/spec/views/spotlight/translations/_import.html.erb_spec.rb +24 -0
  71. metadata +14 -11
  72. data/app/serializers/spotlight/exhibit_export_serializer.rb +0 -205
  73. data/app/serializers/spotlight/featured_image_representer.rb +0 -29
  74. data/app/serializers/spotlight/main_navigation_representer.rb +0 -13
  75. data/app/serializers/spotlight/page_representer.rb +0 -33
@@ -5,7 +5,9 @@ module Spotlight
5
5
  # Feature pages
6
6
  class FeaturePage < Spotlight::Page
7
7
  extend FriendlyId
8
- friendly_id :title, use: %i[slugged scoped finders history], scope: %i[exhibit locale]
8
+ friendly_id :title, use: %i[slugged scoped finders history], scope: %i[exhibit locale] do |config|
9
+ config.reserved_words.concat(%w[update_all])
10
+ end
9
11
 
10
12
  has_many :child_pages, class_name: 'Spotlight::FeaturePage', inverse_of: :parent_page, foreign_key: 'parent_page_id'
11
13
  belongs_to :parent_page, class_name: 'Spotlight::FeaturePage', optional: true
@@ -6,6 +6,24 @@ module Spotlight
6
6
  class FeaturedImage < ActiveRecord::Base
7
7
  mount_uploader :image, Spotlight::FeaturedImageUploader
8
8
 
9
+ before_validation do
10
+ next unless upload_id.present? && source == 'remote'
11
+
12
+ # copy the image from the temp upload
13
+ temp_image = Spotlight::TemporaryImage.find(upload_id)
14
+ self.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(temp_image.image.read),
15
+ filename: temp_image.image.identifier,
16
+ content_type: temp_image.image.content_type
17
+
18
+ # Unset the incoming iiif_tilesource, which points at the temp image
19
+ self.iiif_tilesource = nil
20
+ end
21
+
22
+ after_commit do
23
+ # Clean up the temporary image
24
+ Spotlight::TemporaryImage.find(upload_id).delete if upload_id.present?
25
+ end
26
+
9
27
  after_save do
10
28
  if image.present?
11
29
  image.cache! unless image.cached?
@@ -13,7 +31,7 @@ module Spotlight
13
31
  end
14
32
  end
15
33
 
16
- after_create :set_tilesource_from_uploaded_resource
34
+ attr_accessor :upload_id
17
35
 
18
36
  def iiif_url
19
37
  return unless iiif_service_base.present?
@@ -45,24 +63,23 @@ module Spotlight
45
63
  image.file.present?
46
64
  end
47
65
 
48
- private
49
-
50
- def set_tilesource_from_uploaded_resource
51
- return if iiif_tilesource
52
-
53
- riiif = Riiif::Engine.routes.url_helpers
54
- self.iiif_tilesource = riiif.info_path(id)
55
- save
66
+ def iiif_tilesource
67
+ if self[:iiif_tilesource]
68
+ self[:iiif_tilesource]
69
+ elsif source == 'remote' && file_present?
70
+ riiif = Riiif::Engine.routes.url_helpers
71
+ riiif.info_path(id)
72
+ end
56
73
  end
57
74
 
75
+ private
76
+
58
77
  def image_size
59
78
  Spotlight::Engine.config.featured_image_thumb_size
60
79
  end
61
80
 
62
81
  def iiif_service_base
63
- return unless iiif_tilesource
64
-
65
- iiif_tilesource.sub('/info.json', '')
82
+ iiif_tilesource&.sub('/info.json', '')
66
83
  end
67
84
  end
68
85
  end
@@ -5,7 +5,9 @@ module Spotlight
5
5
  # Exhibit home page
6
6
  class HomePage < Spotlight::Page
7
7
  extend FriendlyId
8
- friendly_id :title, use: %i[slugged scoped finders], scope: %i[exhibit locale]
8
+ friendly_id :title, use: %i[slugged scoped finders history], scope: %i[exhibit locale] do |config|
9
+ config.reserved_words.concat(%w[update_all])
10
+ end
9
11
 
10
12
  before_save :publish
11
13
  before_create :default_content
@@ -26,8 +26,8 @@ module Spotlight
26
26
  end
27
27
  end
28
28
 
29
- def default_label
30
- I18n.t(:"spotlight.main_navigation.#{nav_type}")
29
+ def default_label(**options)
30
+ I18n.t(:"spotlight.main_navigation.#{nav_type}", **options)
31
31
  end
32
32
 
33
33
  private
@@ -11,7 +11,7 @@ module Spotlight
11
11
  private
12
12
 
13
13
  def image_size
14
- [1800, 180]
14
+ Spotlight::Engine.config.featured_image_masthead_size
15
15
  end
16
16
  end
17
17
  end
@@ -7,7 +7,11 @@ module Spotlight
7
7
  MAX_PAGES = Spotlight::Engine.config.max_pages
8
8
 
9
9
  extend FriendlyId
10
- friendly_id :title, use: %i[slugged scoped finders history], scope: %i[exhibit locale]
10
+ # Note: This configuration also needs to be duplicated on the
11
+ # STI models ({Spotlight::AboutPage}, {Spotlight::FeaturePage}, {Spotlight::HomePage})
12
+ friendly_id :title, use: %i[slugged scoped finders history], scope: %i[exhibit locale] do |config|
13
+ config.reserved_words.concat(%w[update_all])
14
+ end
11
15
 
12
16
  belongs_to :exhibit, touch: true
13
17
  belongs_to :created_by, class_name: Spotlight::Engine.config.user_class, optional: true
@@ -17,7 +17,7 @@ module Spotlight
17
17
  scope :published, -> { where(published: true) }
18
18
  validates :title, presence: true
19
19
 
20
- translates :title, :long_description
20
+ translates :title, :subtitle, :long_description
21
21
 
22
22
  has_paper_trail
23
23
 
@@ -26,6 +26,10 @@ module Spotlight
26
26
  accepts_nested_attributes_for :thumbnail, update_only: true, reject_if: proc { |attr| attr['iiif_tilesource'].blank? }
27
27
  accepts_nested_attributes_for :masthead, update_only: true, reject_if: proc { |attr| attr['iiif_tilesource'].blank? }
28
28
 
29
+ def full_title
30
+ [title, subtitle.presence].compact.join(' · ')
31
+ end
32
+
29
33
  def thumbnail_image_url
30
34
  return unless thumbnail&.iiif_url
31
35
 
@@ -33,11 +33,7 @@ module Spotlight
33
33
 
34
34
  # Roll our own polymorphism because our documents are not AREL-able
35
35
  def document
36
- document_type.new document_type.unique_key => document_id
37
- end
38
-
39
- def document_type
40
- (super.constantize if defined?(super)) || default_document_type
36
+ document_type_class.new document_type_class.unique_key => document_id
41
37
  end
42
38
 
43
39
  def default_document_type
@@ -46,6 +42,12 @@ module Spotlight
46
42
 
47
43
  private
48
44
 
45
+ def document_type_class
46
+ return default_document_type unless document_type.is_a?(String)
47
+
48
+ document_type.constantize
49
+ end
50
+
49
51
  def visibility_field
50
52
  blacklight_config.document_model.visibility_field(exhibit)
51
53
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spotlight
4
+ ##
5
+ # Featured images for browse categories, feature pages, and exhibits
6
+ class TemporaryImage < Spotlight::FeaturedImage
7
+ end
8
+ end
@@ -0,0 +1,454 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'roar/decorator'
4
+ require 'roar/json'
5
+ require 'base64'
6
+ require 'tempfile'
7
+
8
+ module Spotlight
9
+ # Utility service for importing and exporting exhibit data
10
+ class ExhibitImportExportService
11
+ class_attribute :serialization_pipeline, default: %i[
12
+ raw_json
13
+ add_feature_page_hierarchy
14
+ add_page_content
15
+ attach_featured_images
16
+ attach_attachments
17
+ ]
18
+
19
+ attr_reader :exhibit, :include
20
+
21
+ def initialize(exhibit, include: Spotlight::Engine.config.exports)
22
+ @exhibit = exhibit
23
+ @include = include
24
+ end
25
+
26
+ def from_hash!(hash)
27
+ hash = hash.deep_symbolize_keys.reverse_merge(
28
+ main_navigations: {},
29
+ contact_emails: {},
30
+ searches: {},
31
+ about_pages: {},
32
+ feature_pages: {},
33
+ contacts: {},
34
+ custom_fields: {},
35
+ solr_document_sidecars: {},
36
+ resources: {},
37
+ attachments: {},
38
+ languages: {},
39
+ translations: {},
40
+ owned_taggings: {}
41
+ )
42
+
43
+ exhibit_attributes = hash.reject { |_k, v| v.is_a?(Array) || v.is_a?(Hash) }
44
+ exhibit.update_attributes(exhibit_attributes.except(:theme))
45
+ exhibit.theme = exhibit_attributes[:theme] if exhibit.themes.include? exhibit_attributes[:theme]
46
+
47
+ deserialize_featured_image(exhibit, :masthead, hash[:masthead]) if hash[:masthead]
48
+ deserialize_featured_image(exhibit, :thumbnail, hash[:thumbnail]) if hash[:thumbnail]
49
+
50
+ exhibit.blacklight_configuration.update_attributes hash[:blacklight_configuration].deep_stringify_keys if hash[:blacklight_configuration]
51
+
52
+ hash[:main_navigations].each do |attr|
53
+ ar = exhibit.main_navigations.find_or_initialize_by(nav_type: attr[:nav_type])
54
+ ar.update_attributes(attr)
55
+ end
56
+
57
+ hash[:contact_emails].each do |attr|
58
+ ar = exhibit.contact_emails.find_or_initialize_by(email: attr[:email])
59
+ ar.update_attributes(attr)
60
+ end
61
+
62
+ hash[:searches].each do |attr|
63
+ masthead = attr.delete(:masthead)
64
+ thumbnail = attr.delete(:thumbnail)
65
+
66
+ ar = exhibit.searches.find_or_initialize_by(slug: attr[:slug])
67
+ ar.update_attributes(attr)
68
+
69
+ deserialize_featured_image(ar, :masthead, masthead) if masthead
70
+ deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
71
+ end
72
+
73
+ hash[:about_pages].each do |attr|
74
+ masthead = attr.delete(:masthead)
75
+ thumbnail = attr.delete(:thumbnail)
76
+ translated_pages = attr.delete(:translated_pages) || []
77
+
78
+ ar = exhibit.about_pages.find_or_initialize_by(slug: attr[:slug])
79
+ ar.update_attributes(attr)
80
+
81
+ deserialize_featured_image(ar, :masthead, masthead) if masthead
82
+ deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
83
+
84
+ translated_pages.each do |tattr|
85
+ masthead = tattr.delete(:masthead)
86
+ thumbnail = tattr.delete(:thumbnail)
87
+
88
+ tar = ar.translated_page_for(tattr[:locale]) || ar.clone_for_locale(tattr[:locale])
89
+ tar.update_attributes(tattr)
90
+
91
+ deserialize_featured_image(ar, :masthead, masthead) if masthead
92
+ deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
93
+ end
94
+ end
95
+
96
+ hash[:feature_pages].each do |attr|
97
+ masthead = attr.delete(:masthead)
98
+ thumbnail = attr.delete(:thumbnail)
99
+
100
+ ar = exhibit.feature_pages.find_or_initialize_by(slug: attr[:slug])
101
+ ar.update_attributes(attr.except(:parent_page_slug, :translated_pages))
102
+
103
+ deserialize_featured_image(ar, :masthead, masthead) if masthead
104
+ deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
105
+ end
106
+
107
+ feature_pages = exhibit.feature_pages.index_by(&:slug)
108
+ hash[:feature_pages].each do |attr|
109
+ next unless attr[:parent_page_slug]
110
+
111
+ feature_pages[attr[:slug]].parent_page_id = feature_pages[attr[:parent_page_slug]].id
112
+ end
113
+
114
+ hash[:feature_pages].each do |attr|
115
+ ar = exhibit.feature_pages.find_or_initialize_by(slug: attr[:slug])
116
+
117
+ (attr[:translated_pages] || []).each do |tattr|
118
+ masthead = tattr.delete(:masthead)
119
+ thumbnail = tattr.delete(:thumbnail)
120
+
121
+ tar = ar.translated_page_for(tattr[:locale]) || ar.clone_for_locale(tattr[:locale])
122
+ tar.update_attributes(tattr)
123
+
124
+ deserialize_featured_image(ar, :masthead, masthead) if masthead
125
+ deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
126
+ end
127
+ end
128
+
129
+ if hash[:home_page]
130
+ translated_pages = hash[:home_page].delete(:translated_pages) || []
131
+ exhibit.home_page.update_attributes(hash[:home_page].except(:thumbnail))
132
+ deserialize_featured_image(exhibit.home_page, :thumbnail, hash[:home_page][:thumbnail]) if hash[:home_page][:thumbnail]
133
+
134
+ translated_pages.each do |tattr|
135
+ masthead = tattr.delete(:masthead)
136
+ thumbnail = tattr.delete(:thumbnail)
137
+
138
+ tar = exhibit.home_page.translated_page_for(tattr[:locale]) || exhibit.home_page.clone_for_locale(tattr[:locale])
139
+ tar.update_attributes(tattr)
140
+
141
+ deserialize_featured_image(ar, :masthead, masthead) if masthead
142
+ deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
143
+ end
144
+ end
145
+
146
+ hash[:contacts].each do |attr|
147
+ avatar = attr.delete(:avatar)
148
+
149
+ ar = exhibit.contacts.find_or_initialize_by(slug: attr[:slug])
150
+ ar.update_attributes(attr)
151
+
152
+ deserialize_featured_image(ar, :avatar, avatar) if avatar
153
+ end
154
+
155
+ hash[:custom_fields].each do |attr|
156
+ ar = exhibit.custom_fields.find_or_initialize_by(slug: attr[:slug])
157
+ ar.update_attributes(attr)
158
+ end
159
+
160
+ hash[:solr_document_sidecars].each do |attr|
161
+ ar = exhibit.solr_document_sidecars.find_or_initialize_by(document_id: attr[:document_id])
162
+ ar.update_attributes(attr)
163
+ end
164
+
165
+ hash[:resources].each do |attr|
166
+ upload = attr.delete(:upload)
167
+
168
+ ar = exhibit.resources.find_or_initialize_by(type: attr[:type], url: attr[:url])
169
+ ar.update_attributes(attr)
170
+
171
+ deserialize_featured_image(ar, :upload, upload) if upload
172
+ end
173
+
174
+ hash[:attachments].each do |attr|
175
+ file = attr.delete(:file)
176
+
177
+ # dedupe by something??
178
+ ar = exhibit.attachments.build(attr)
179
+ ar.file = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
180
+ filename: file[:filename],
181
+ content_type: file[:content_type]
182
+ end
183
+
184
+ hash[:languages].each do |attr|
185
+ ar = exhibit.languages.find_or_initialize_by(locale: attr[:locale])
186
+ ar.update_attributes(attr)
187
+ end
188
+
189
+ hash[:translations].each do |attr|
190
+ ar = exhibit.translations.find_or_initialize_by(locale: attr[:locale], key: attr[:key])
191
+ ar.update_attributes(attr)
192
+ end
193
+
194
+ hash[:owned_taggings].each do |attr|
195
+ tag = ActsAsTaggableOn::Tag.find_or_create_by(name: attr[:tag][:name])
196
+ exhibit.owned_taggings.build(attr.except(:tag).merge(tag_id: tag.id))
197
+ end
198
+ end
199
+
200
+ def deserialize_featured_image(obj, method, data)
201
+ file = data.delete(:image)
202
+ image = obj.public_send("build_#{method}")
203
+ image.update_attributes(data)
204
+ if file
205
+ image.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
206
+ filename: file[:filename],
207
+ content_type: file[:content_type]
208
+ end
209
+ image.save!
210
+ obj.update(method => image)
211
+ end
212
+
213
+ def as_json(**args)
214
+ self.class.serialization_pipeline.inject(args) do |memo, step|
215
+ method(step).call(memo)
216
+ end
217
+ end
218
+
219
+ private
220
+
221
+ def attach_featured_images(json)
222
+ json[:masthead] = serialize_featured_image(json[:masthead_id]) if json[:masthead_id]
223
+ json.delete(:masthead_id)
224
+ json[:thumbnail] = serialize_featured_image(json[:thumbnail_id]) if json[:thumbnail_id]
225
+ json.delete(:thumbnail_id)
226
+
227
+ (json[:searches] || []).each do |search|
228
+ search[:masthead] = serialize_featured_image(search[:masthead_id]) if search[:masthead_id]
229
+ search.delete(:masthead_id)
230
+ search[:thumbnail] = serialize_featured_image(search[:thumbnail_id]) if search[:thumbnail_id]
231
+ search.delete(:thumbnail_id)
232
+ end
233
+
234
+ (json[:about_pages] || []).each do |page|
235
+ page[:masthead] = serialize_featured_image(page[:masthead_id]) if page[:masthead_id]
236
+ page.delete(:masthead_id)
237
+ page[:thumbnail] = serialize_featured_image(page[:thumbnail_id]) if page[:thumbnail_id]
238
+ page.delete(:thumbnail_id)
239
+
240
+ (page[:translated_pages] || []).each do |translated_page|
241
+ translated_page[:masthead] = serialize_featured_image(translated_page[:masthead_id]) if translated_page[:masthead_id]
242
+ translated_page.delete(:masthead_id)
243
+ translated_page[:thumbnail] = serialize_featured_image(translated_page[:thumbnail_id]) if translated_page[:thumbnail_id]
244
+ translated_page.delete(:thumbnail_id)
245
+ end
246
+ end
247
+
248
+ (json[:feature_pages] || []).each do |page|
249
+ page[:masthead] = serialize_featured_image(page[:masthead_id]) if page[:masthead_id]
250
+ page.delete(:masthead_id)
251
+ page[:thumbnail] = serialize_featured_image(page[:thumbnail_id]) if page[:thumbnail_id]
252
+ page.delete(:thumbnail_id)
253
+
254
+ (page[:translated_pages] || []).each do |translated_page|
255
+ translated_page[:masthead] = serialize_featured_image(translated_page[:masthead_id]) if translated_page[:masthead_id]
256
+ translated_page.delete(:masthead_id)
257
+ translated_page[:thumbnail] = serialize_featured_image(translated_page[:thumbnail_id]) if translated_page[:thumbnail_id]
258
+ translated_page.delete(:thumbnail_id)
259
+ end
260
+ end
261
+
262
+ if json[:home_page]
263
+ json[:home_page][:masthead] = serialize_featured_image(json[:home_page][:masthead_id]) if json[:home_page][:masthead_id]
264
+ json[:home_page].delete(:masthead_id)
265
+ json[:home_page][:thumbnail] = serialize_featured_image(json[:home_page][:thumbnail_id]) if json[:home_page][:thumbnail_id]
266
+ json[:home_page].delete(:thumbnail_id)
267
+
268
+ (json[:home_page][:translated_pages] || []).each do |translated_page|
269
+ translated_page[:masthead] = serialize_featured_image(translated_page[:masthead_id]) if translated_page[:masthead_id]
270
+ translated_page.delete(:masthead_id)
271
+ translated_page[:thumbnail] = serialize_featured_image(translated_page[:thumbnail_id]) if translated_page[:thumbnail_id]
272
+ translated_page.delete(:thumbnail_id)
273
+ end
274
+ end
275
+
276
+ (json[:contacts] || []).each do |page|
277
+ page[:avatar] = serialize_featured_image(page[:avatar_id]) if page[:avatar_id]
278
+ page.delete(:avatar_id)
279
+ end
280
+
281
+ (json[:resources] || []).each do |page|
282
+ page[:upload] = serialize_featured_image(page[:upload_id]) if page[:upload_id]
283
+ page.delete(:upload_id)
284
+ end
285
+
286
+ json
287
+ end
288
+
289
+ def serialize_featured_image(id)
290
+ image = Spotlight::FeaturedImage.find(id)
291
+ file = image.image.file
292
+ if file
293
+ img = {
294
+ image: {
295
+ filename: file.identifier, content_type: file.content_type, content: Base64.encode64(file.read)
296
+ }
297
+ }
298
+ end
299
+
300
+ image.as_json(except: %i[id image]).merge(img || {}).deep_symbolize_keys
301
+ end
302
+
303
+ def attach_attachments(json)
304
+ return json unless json[:attachments]
305
+
306
+ json[:attachments].each do |attachment|
307
+ a = exhibit.attachments.find(attachment[:id])
308
+ file = a.file.file
309
+
310
+ attachment[:file] = {
311
+ filename: file.filename,
312
+ content_type: file.content_type,
313
+ content: Base64.encode64(file.read)
314
+ }
315
+
316
+ attachment.delete(:id)
317
+ end
318
+
319
+ json
320
+ end
321
+
322
+ def add_feature_page_hierarchy(json)
323
+ return json unless json[:feature_pages]
324
+
325
+ page_id_map = json[:feature_pages].map { |x| [x[:id], x[:slug]] }.to_h
326
+
327
+ json[:feature_pages].each do |page|
328
+ page.delete(:id)
329
+ next unless page[:parent_page_id]
330
+
331
+ page[:parent_page_slug] = page_id_map[page[:parent_page_id]]
332
+ page.delete(:parent_page_id)
333
+ end
334
+
335
+ json
336
+ end
337
+
338
+ def add_page_content(json)
339
+ (json[:feature_pages] || []).each do |page|
340
+ p = exhibit.feature_pages.find_by(slug: page[:slug])
341
+ page[:content] = p.read_attribute(:content)
342
+ (page[:translated_pages]).each do |translated_page|
343
+ translated_page[:content] = p.translated_page_for(translated_page[:locale]).read_attribute(:content)
344
+ end
345
+ end
346
+
347
+ (json[:about_pages] || []).each do |page|
348
+ p = exhibit.about_pages.find_by(slug: page[:slug])
349
+ page[:content] = p.read_attribute(:content)
350
+ (page[:translated_pages]).each do |translated_page|
351
+ translated_page[:content] = p.translated_page_for(translated_page[:locale]).read_attribute(:content)
352
+ end
353
+ end
354
+
355
+ json[:home_page][:content] = exhibit.home_page.read_attribute(:content) if json[:home_page]
356
+ (json.dig(:home_page, :translated_pages) || []).each do |translated_page|
357
+ translated_page[:content] = exhibit.home_page.translated_page_for(translated_page[:locale]).read_attribute(:content)
358
+ end
359
+ json
360
+ end
361
+
362
+ def raw_json(_input = nil)
363
+ exhibit.as_json(
364
+ {
365
+ except: %i[id slug site_id],
366
+ include: {}.merge(
367
+ if_include?(:config,
368
+ main_navigations: {
369
+ except: %i[id exhibit_id]
370
+ },
371
+ contact_emails: {
372
+ except: %i[id exhibit_id confirmation_token]
373
+ },
374
+ languages: {
375
+ except: %i[id exhibit_id]
376
+ },
377
+ translations: {
378
+ only: %i[locale key value interpolations is_proc]
379
+ })
380
+ ).merge(
381
+ if_include?(:pages,
382
+ searches: { # thumbnail
383
+ except: %i[id scope exhibit_id]
384
+ },
385
+ about_pages: { # thumbnail
386
+ except: %i[id scope exhibit_id parent_page_id content],
387
+ include: {
388
+ translated_pages: {
389
+ except: %i[id scope exhibit_id parent_page_id default_locale_page_id content]
390
+ }
391
+ }
392
+ },
393
+ home_page: { # thumbnail
394
+ except: %i[id slug scope exhibit_id parent_page_id content],
395
+ include: {
396
+ translated_pages: {
397
+ except: %i[id scope exhibit_id parent_page_id default_locale_page_id content]
398
+ }
399
+ }
400
+ },
401
+ feature_pages: { # thumbnail
402
+ except: %i[scope exhibit_id content],
403
+ include: {
404
+ translated_pages: {
405
+ except: %i[id scope exhibit_id parent_page_id default_locale_page_id content]
406
+ }
407
+ }
408
+ },
409
+ contacts: {
410
+ except: %i[id exhibit_id]
411
+ })
412
+ ).merge(
413
+ if_include?(:blacklight_configuration,
414
+ blacklight_configuration: {
415
+ except: %i[id exhibit_id]
416
+ },
417
+ # blacklight_configuration
418
+ custom_fields: {
419
+ except: %i[id exhibit_id]
420
+ })
421
+ ).merge(
422
+ if_include?(:resources,
423
+ # resources
424
+ solr_document_sidecars: {
425
+ except: %i[id exhibit_id]
426
+ },
427
+ owned_taggings: {
428
+ only: %i[taggable_id taggable_type context],
429
+ include: {
430
+ tag: {
431
+ only: [:name]
432
+ }
433
+ }
434
+ },
435
+ resources: { # upload
436
+ except: %i[id exhibit_id],
437
+ methods: :type
438
+ })
439
+ ).merge(
440
+ if_include?(:attachments,
441
+ # attachments
442
+ attachments: { # file
443
+ except: %i[exhibit_id]
444
+ })
445
+ )
446
+ }.merge(include[:config] ? {} : { only: %i[does_not_exist] })
447
+ ).deep_symbolize_keys
448
+ end
449
+
450
+ def if_include?(config, res)
451
+ include[config] ? res : {}
452
+ end
453
+ end
454
+ end