Sha256: 317a0942fc2346111bae2dc4595b09331632424e920234e26fed4d3ea2bd8865

Contents?: true

Size: 1.15 KB

Versions: 50

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # After renaming an exhibit-specific field, we also
  # need to update the sidecars that may contain that field
  class RenameSidecarFieldJob < Spotlight::ApplicationJob
    include Spotlight::JobTracking
    with_job_tracking(resource: ->(job) { job.arguments.first })

    def perform(exhibit, old_field, new_field, old_slug = nil, new_slug = nil)
      exhibit.solr_document_sidecars.find_each do |s|
        # this data migration should be relatively rare
        migrate_data!(s, old_slug, new_slug)
        migrate_data!(s, old_field, new_slug || new_field) # for backwards compatibility

        # more likely, the indexing rules changed and we have to reindex
        reindex_document!(s) if old_field != new_field && s.data[new_slug || new_field]
      end
    end

    private

    def migrate_data!(sidecar, old_field, new_field)
      return if old_field == new_field || !sidecar.data.key?(old_field)

      sidecar.data_will_change!
      sidecar.data[new_field] = sidecar.data.delete(old_field)
      sidecar.save
    end

    def reindex_document!(sidecar)
      sidecar.document.reindex
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
blacklight-spotlight-4.6.1 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.6.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.5.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.4.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.6 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.5 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.4 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.3 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.2 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.1 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.3.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.2.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.1.2 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.1.1 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.1.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.0.3 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.0.2 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.0.1 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-4.0.0 app/jobs/spotlight/rename_sidecar_field_job.rb
blacklight-spotlight-3.6.0.beta10 app/jobs/spotlight/rename_sidecar_field_job.rb