app/models/spotlight/resources/upload.rb in blacklight-spotlight-0.2.0 vs app/models/spotlight/resources/upload.rb in blacklight-spotlight-0.3.0

- old
+ new

@@ -1,61 +1,80 @@ module Spotlight class Resources::Upload < Spotlight::Resource mount_uploader :url, Spotlight::ItemUploader + include Spotlight::ImageDerivatives - def to_solr - store_url! # so that #url doesn't return the tmp directory - to_solr_hash - end - def self.fields(exhibit) - @fields ||= self.new(exhibit: exhibit).configured_fields + @fields ||= {} + @fields[exhibit] ||= self.new(exhibit: exhibit).configured_fields end def configured_fields - @configured_fields ||= configured_title_field.merge(Spotlight::Engine.config.upload_fields) + @configured_fields ||= [configured_title_field] + Spotlight::Engine.config.upload_fields end + def to_solr + store_url! # so that #url doesn't return the tmp directory + + solr_hash = super + + solr_hash[:"#{Spotlight::Engine.config.solr_fields.prefix}spotlight_resource_url#{Spotlight::Engine.config.solr_fields.string_suffix}"] = url.url + + add_default_solr_fields solr_hash + + add_image_dimensions solr_hash + + add_custom_fields solr_hash + + add_configured_fields solr_hash + + add_file_versions solr_hash + + solr_hash + end + private # this is in the upload class because it has exhibit context def configured_title_field - {title: OpenStruct.new(solr_field: exhibit.blacklight_config.index.title_field)} + Spotlight::Engine.config.upload_title_field || OpenStruct.new(field_name: exhibit.blacklight_config.index.title_field) end + + def add_default_solr_fields solr_hash + solr_hash[exhibit.blacklight_config.solr_document_model.unique_key.to_sym] = compound_id + end - def to_solr_hash - solr_hash = { - ::SolrDocument.unique_key.to_sym => compound_id, - exhibit.blacklight_config.index.full_image_field => url.url, - spotlight_resource_type_ssm: self.class.to_s.tableize - } + 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 - add_image_dimensions(solr_hash) - - add_custom_fields(solr_hash) - - Spotlight::ItemUploader.configured_versions.each do |config| - solr_hash[exhibit.blacklight_config.index.send(config[:blacklight_config_field])] = url.send(config[:version]).url + def add_custom_fields solr_hash + exhibit.custom_fields.collect(&:field).each do |solr_field| + if data[solr_field].present? + solr_hash[solr_field] = data[solr_field] + end end + end - configured_fields.each do |key, config| - if data[key].present? - solr_hash[config.solr_field] = data[key] + def add_configured_fields solr_hash + configured_fields.each do |field| + solr_fields = Array(field.solr_field || field.field_name) + if data[field.field_name].present? + solr_fields.each do |solr_field| + solr_hash[solr_field] = data[field.field_name] + end end 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 - - def add_custom_fields(solr_hash) - exhibit.custom_fields.each do |custom_field| - if data[custom_field.field].present? - solr_hash[custom_field.field] = data[custom_field.field] + 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 def compound_id