Class: Spotlight::ExhibitImportExportService

Inherits:
Object
  • Object
show all
Defined in:
app/services/spotlight/exhibit_import_export_service.rb

Overview

Utility service for importing and exporting exhibit data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exhibit, include: Spotlight::Engine.config.exports) ⇒ ExhibitImportExportService

Returns a new instance of ExhibitImportExportService.



22
23
24
25
# File 'app/services/spotlight/exhibit_import_export_service.rb', line 22

def initialize(exhibit, include: Spotlight::Engine.config.exports)
  @exhibit = exhibit
  @include = include
end

Instance Attribute Details

#exhibitObject (readonly)

Returns the value of attribute exhibit.



20
21
22
# File 'app/services/spotlight/exhibit_import_export_service.rb', line 20

def exhibit
  @exhibit
end

#includeObject (readonly)

Returns the value of attribute include.



20
21
22
# File 'app/services/spotlight/exhibit_import_export_service.rb', line 20

def include
  @include
end

Instance Method Details

#as_json(*_args) ⇒ Object



227
228
229
230
231
# File 'app/services/spotlight/exhibit_import_export_service.rb', line 227

def as_json(*_args)
  self.class.serialization_pipeline.inject({}) do |memo, step|
    method(step).call(memo)
  end
end


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/services/spotlight/exhibit_import_export_service.rb', line 211

def deserialize_featured_image(obj, method, data)
  file = data.delete(:image)
  image = obj.public_send("build_#{method}")
  image.update(data)
  if file
    image.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
                                                 filename: file[:filename],
                                                 content_type: file[:content_type]
    # Unset the iiif_tilesource field as the new image should be different, because
    # the source has been reloaded
    image.iiif_tilesource = nil
  end
  image.save!
  obj.update(method => image)
end

#from_hash!(hash) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/services/spotlight/exhibit_import_export_service.rb', line 27

def from_hash!(hash)
  hash = hash.deep_symbolize_keys.reverse_merge(
    main_navigations: {},
    contact_emails: {},
    searches: {},
    about_pages: {},
    feature_pages: {},
    contacts: {},
    custom_fields: {},
    solr_document_sidecars: {},
    resources: {},
    attachments: {},
    languages: {},
    translations: {},
    owned_taggings: {},
    groups: {}
  )

  exhibit_attributes = hash.reject { |_k, v| v.is_a?(Array) || v.is_a?(Hash) }
  exhibit.update(exhibit_attributes.except(:theme))
  exhibit.theme = exhibit_attributes[:theme] if exhibit.themes.include? exhibit_attributes[:theme]

  deserialize_featured_image(exhibit, :masthead, hash[:masthead]) if hash[:masthead]
  deserialize_featured_image(exhibit, :thumbnail, hash[:thumbnail]) if hash[:thumbnail]

  exhibit.blacklight_configuration.update hash[:blacklight_configuration].with_indifferent_access if hash[:blacklight_configuration]

  hash[:main_navigations].each do |attr|
    ar = exhibit.main_navigations.find_or_initialize_by(nav_type: attr[:nav_type])
    ar.update(attr)
  end

  hash[:contact_emails].each do |attr|
    ar = exhibit.contact_emails.find_or_initialize_by(email: attr[:email])
    ar.update(attr)
  end

  hash[:groups].each do |attr|
    gr = exhibit.groups.find_or_initialize_by(slug: attr[:slug])
    gr.update(attr)
  end

  hash[:searches].each do |attr|
    group_slugs = attr.delete(:group_slugs) || []
    masthead = attr.delete(:masthead)
    thumbnail = attr.delete(:thumbnail)

    ar = exhibit.searches.find_or_initialize_by(slug: attr[:slug])
    ar.update(attr)

    ar.update(groups: exhibit.groups.select { |x| group_slugs.include? x.slug })

    deserialize_featured_image(ar, :masthead, masthead) if masthead
    deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
  end

  hash[:about_pages].each do |attr|
    masthead = attr.delete(:masthead)
    thumbnail = attr.delete(:thumbnail)
    translated_pages = attr.delete(:translated_pages) || []

    ar = exhibit.about_pages.find_or_initialize_by(slug: attr[:slug])
    ar.update(attr)

    deserialize_featured_image(ar, :masthead, masthead) if masthead
    deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail

    translated_pages.each do |tattr|
      masthead = tattr.delete(:masthead)
      thumbnail = tattr.delete(:thumbnail)

      tar = ar.translated_page_for(tattr[:locale]) || ar.clone_for_locale(tattr[:locale])
      tar.update(tattr)

      deserialize_featured_image(ar, :masthead, masthead) if masthead
      deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
    end
  end

  hash[:feature_pages].each do |attr|
    masthead = attr.delete(:masthead)
    thumbnail = attr.delete(:thumbnail)

    ar = exhibit.feature_pages.find_or_initialize_by(slug: attr[:slug])
    ar.update(attr.except(:parent_page_slug, :translated_pages))

    deserialize_featured_image(ar, :masthead, masthead) if masthead
    deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
  end

  feature_pages = exhibit.feature_pages.index_by(&:slug)
  hash[:feature_pages].each do |attr|
    next unless attr[:parent_page_slug]

    feature_pages[attr[:slug]].parent_page_id = feature_pages[attr[:parent_page_slug]].id
  end

  hash[:feature_pages].each do |attr|
    ar = exhibit.feature_pages.find_or_initialize_by(slug: attr[:slug])

    (attr[:translated_pages] || []).each do |tattr|
      masthead = tattr.delete(:masthead)
      thumbnail = tattr.delete(:thumbnail)

      tar = ar.translated_page_for(tattr[:locale]) || ar.clone_for_locale(tattr[:locale])
      tar.update(tattr)

      deserialize_featured_image(ar, :masthead, masthead) if masthead
      deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
    end
  end

  if hash[:home_page]
    translated_pages = hash[:home_page].delete(:translated_pages) || []
    exhibit.home_page.update(hash[:home_page].except(:thumbnail))
    deserialize_featured_image(exhibit.home_page, :thumbnail, hash[:home_page][:thumbnail]) if hash[:home_page][:thumbnail]

    translated_pages.each do |tattr|
      masthead = tattr.delete(:masthead)
      thumbnail = tattr.delete(:thumbnail)

      tar = exhibit.home_page.translated_page_for(tattr[:locale]) || exhibit.home_page.clone_for_locale(tattr[:locale])
      tar.update(tattr)

      deserialize_featured_image(ar, :masthead, masthead) if masthead
      deserialize_featured_image(ar, :thumbnail, thumbnail) if thumbnail
    end
  end

  hash[:contacts].each do |attr|
    avatar = attr.delete(:avatar)

    ar = exhibit.contacts.find_or_initialize_by(slug: attr[:slug])
    ar.update(attr)

    deserialize_featured_image(ar, :avatar, avatar) if avatar
  end

  hash[:custom_fields].each do |attr|
    ar = exhibit.custom_fields.find_or_initialize_by(slug: attr[:slug])
    attr[:configuration] = attr[:configuration].clone.transform_keys(&:to_s)
    ar.update(attr)
  end

  hash[:solr_document_sidecars].each do |attr|
    ar = exhibit.solr_document_sidecars.find_or_initialize_by(document_id: attr[:document_id])
    ar.update(attr)
  end

  hash[:resources].each do |attr|
    upload = attr.delete(:upload)

    ar = exhibit.resources.find_or_initialize_by(type: attr[:type], url: attr[:url])
    ar.update(attr)

    deserialize_featured_image(ar, :upload, upload) if upload
  end

  hash[:attachments].each do |attr|
    file = attr.delete(:file)

    # dedupe by something??
    ar = exhibit.attachments.build(attr)
    ar.file = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
                                             filename: file[:filename],
                                             content_type: file[:content_type]
  end

  hash[:languages].each do |attr|
    ar = exhibit.languages.find_or_initialize_by(locale: attr[:locale])
    ar.update(attr)
  end

  hash[:translations].each do |attr|
    ar = exhibit.translations.find_or_initialize_by(locale: attr[:locale], key: attr[:key])
    ar.update(attr)
  end

  hash[:owned_taggings].each do |attr|
    tag = ActsAsTaggableOn::Tag.find_or_create_by(name: attr[:tag][:name])
    exhibit.owned_taggings.build(attr.except(:tag).merge(tag_id: tag.id))
  end
end