app/models/spotlight/resources/upload.rb in blacklight-spotlight-0.4.1 vs app/models/spotlight/resources/upload.rb in blacklight-spotlight-0.5.0

- old
+ new

@@ -1,81 +1,92 @@ # encoding: utf-8 module Spotlight - class Resources::Upload < Spotlight::Resource - mount_uploader :url, Spotlight::ItemUploader - include Spotlight::ImageDerivatives - - # we want to do this before reindexing - after_create :update_document_sidecar + module Resources + ## + # Exhibit-specific resources, created using uploaded and custom fields + class Upload < Spotlight::Resource + mount_uploader :url, Spotlight::ItemUploader + include Spotlight::ImageDerivatives - def self.fields(exhibit) - @fields ||= {} - @fields[exhibit] ||= [Spotlight::Engine.config.upload_title_field || OpenStruct.new(field_name: exhibit.blacklight_config.index.title_field)] + exhibit.uploaded_resource_fields - end + # we want to do this before reindexing + after_create :update_document_sidecar - def configured_fields - self.class.fields(exhibit) - end + def self.fields(exhibit) + @fields ||= {} + @fields[exhibit] ||= begin + title_field = Spotlight::Engine.config.upload_title_field || OpenStruct.new(field_name: exhibit.blacklight_config.index.title_field) + [title_field] + exhibit.uploaded_resource_fields + end + end - def to_solr - store_url! # so that #url doesn't return the tmp directory + def configured_fields + self.class.fields(exhibit) + end - solr_hash = super - - add_default_solr_fields solr_hash + def to_solr + store_url! # so that #url doesn't return the tmp directory - add_image_dimensions solr_hash + solr_hash = super - add_file_versions solr_hash + add_default_solr_fields solr_hash - add_sidecar_fields solr_hash + add_image_dimensions solr_hash - solr_hash - end + add_file_versions solr_hash - private + add_sidecar_fields solr_hash - - def add_default_solr_fields solr_hash - solr_hash[exhibit.blacklight_config.document_model.unique_key.to_sym] = compound_id - end + solr_hash + end - def add_image_dimensions solr_hash - dimensions = ::MiniMagick::Image.open(url.file.file)[:dimensions] - solr_hash[:spotlight_full_image_width_ssm] = dimensions.first - solr_hash[:spotlight_full_image_height_ssm] = dimensions.last - end + private - def add_file_versions solr_hash - spotlight_image_derivatives.each do |config| - if config[:version] - solr_hash[config[:field]] = url.send(config[:version]).url - else - solr_hash[config[:field]] = url.url + def add_default_solr_fields(solr_hash) + solr_hash[exhibit.blacklight_config.document_model.unique_key.to_sym] = compound_id + end + + def add_image_dimensions(solr_hash) + dimensions = ::MiniMagick::Image.open(url.file.file)[:dimensions] + solr_hash[:spotlight_full_image_width_ssm] = dimensions.first + solr_hash[:spotlight_full_image_height_ssm] = dimensions.last + end + + def add_file_versions(solr_hash) + spotlight_image_derivatives.each do |config| + if config[:version] + solr_hash[config[:field]] = url.send(config[:version]).url + else + solr_hash[config[:field]] = url.url + end end end - end - def add_sidecar_fields solr_hash - solr_hash.merge! sidecar.to_solr - end + def add_sidecar_fields(solr_hash) + solr_hash.merge! sidecar.to_solr + end - def compound_id - "#{exhibit_id}-#{id}" - end - - def update_document_sidecar - sidecar_updates = data.slice(*exhibit.custom_fields.map(&:field).map(&:to_s)).select { |k,v| v.present? } + def compound_id + "#{exhibit_id}-#{id}" + end - sidecar_updates["configured_fields"] = data.slice(*configured_fields.map(&:field_name).map(&:to_s)).select { |k,v| v.present? } + def update_document_sidecar + sidecar.update(data: sidecar.data.merge(sidecar_update_data)) + end - sidecar.update(data: sidecar.data.merge(sidecar_updates)) + def sidecar_update_data + custom_fields_data.merge('configured_fields' => configured_fields_data) + end - sidecar.save - end + def custom_fields_data + data.slice(*exhibit.custom_fields.map(&:field).map(&:to_s)).select { |_k, v| v.present? } + end - def sidecar - @sidecar ||= document_model.new(id: compound_id).sidecar(exhibit) - end + def configured_fields_data + data.slice(*configured_fields.map(&:field_name).map(&:to_s)).select { |_k, v| v.present? } + end + def sidecar + @sidecar ||= document_model.new(id: compound_id).sidecar(exhibit) + end + end end end