Skip to content

Commit

Permalink
make search string used to look up objects configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
orangewolf committed Nov 9, 2023
1 parent a310e51 commit d394861
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Bixby coding conventions
require: rubocop-factory_bot

# Added AllCops config for further modification
AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 2.7
DisabledByDefault: true
DisplayCopNames: true
Exclude:
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/bulkrax/importers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def importer_params
end

def list_external_sets
url = params[:base_url] || (@harvester ? @harvester.base_url : nil)
url = params[:base_url] || @harvester&.base_url
setup_client(url) if url.present?

@sets = [['All', 'all']]
Expand Down
12 changes: 4 additions & 8 deletions app/factories/bulkrax/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ class ObjectFactory # rubocop:disable Metrics/ClassLength
class_attribute :transformation_removes_blank_hash_values, default: false

define_model_callbacks :save, :create
attr_reader :attributes, :object, :source_identifier_value, :klass, :replace_files, :update_files, :work_identifier, :related_parents_parsed_mapping, :importer_run_id
attr_reader :attributes, :object, :source_identifier_value, :klass, :replace_files, :update_files, :work_identifier, :work_identifier_search_field, :related_parents_parsed_mapping, :importer_run_id

# rubocop:disable Metrics/ParameterLists
def initialize(attributes:, source_identifier_value:, work_identifier:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false)
def initialize(attributes:, source_identifier_value:, work_identifier:, work_identifier_search_field:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false)
@attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
@replace_files = replace_files
@update_files = update_files
@user = user || User.batch_user
@work_identifier = work_identifier
@work_identifier_search_string = work_identifier_search_field
@related_parents_parsed_mapping = related_parents_parsed_mapping
@source_identifier_value = source_identifier_value
@klass = klass || Bulkrax.default_work_type.constantize
Expand Down Expand Up @@ -103,12 +104,7 @@ def find_or_create
end

def search_by_identifier
# TODO(alishaevn): return the proper `work_index` value below
# ref: https://github.com/samvera-labs/bulkrax/issues/866
# ref:https://github.com/samvera-labs/bulkrax/issues/867
# work_index = ::ActiveFedora.index_field_mapper.solr_name(work_identifier, :facetable)
work_index = work_identifier
query = { work_index =>
query = { work_identifier_search_field =>
source_identifier_value }
# Query can return partial matches (something6 matches both something6 and something68)
# so we need to weed out any that are not the correct full match. But other items might be
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/bulkrax/import_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def factory
@factory ||= of.new(attributes: self.parsed_metadata,
source_identifier_value: identifier,
work_identifier: parser.work_identifier,
work_identifier_search_field: parser.work_identifier_search_field,
related_parents_parsed_mapping: parser.related_parents_parsed_mapping,
replace_files: replace_files,
user: user,
Expand Down
7 changes: 7 additions & 0 deletions app/parsers/bulkrax/application_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ def work_identifier
@work_identifier ||= get_field_mapping_hash_for('source_identifier')&.keys&.first&.to_sym || :source
end

# @return [Symbol] the solr property of the source_identifier. Used for searching.
# defaults to work_identifier value + "_sim"
# @see #work_identifier
def work_identifier_search_field
@work_identifier_search_field ||= get_field_mapping_hash_for('source_identifier')&.values&.first&.[]('search_field')&.first&.to_s || "#{work_identifier}_sim"
end

# @return [String]
def generated_metadata_mapping
@generated_metadata_mapping ||= 'generated'
Expand Down
10 changes: 8 additions & 2 deletions spec/factories/bulkrax/object_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
context 'default behavior' do
it "does not empty arrays that only have empty values" do
attributes = { empty_array: ["", ""], empty_string: "", filled_array: ["A", "B"], filled_string: "A" }
factory = described_class.new(attributes: attributes, source_identifier_value: 123, work_identifier: "filled_string")
factory = described_class.new(attributes: attributes,
source_identifier_value: 123,
work_identifier: "filled_string",
work_identifier_search_field: 'filled_string_sim')
factory.base_permitted_attributes = %i[empty_array empty_string filled_array filled_string]
expect(factory.send(:transform_attributes)).to eq(attributes.stringify_keys)
end
Expand All @@ -18,7 +21,10 @@
context 'when :transformation_removes_blank_hash_values = true' do
it "empties arrays that only have empty values" do
attributes = { empty_array: ["", ""], empty_string: "", filled_array: ["A", "B"], filled_string: "A" }
factory = described_class.new(attributes: attributes, source_identifier_value: 123, work_identifier: "filled_string")
factory = described_class.new(attributes: attributes,
source_identifier_value: 123,
work_identifier: "filled_string",
work_identifier_search_string: 'filled_string_sim')
factory.base_permitted_attributes = %i[empty_array empty_string filled_array filled_string]
factory.transformation_removes_blank_hash_values = true
expect(factory.send(:transform_attributes))
Expand Down
3 changes: 2 additions & 1 deletion spec/factories/bulkrax_object_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
new(
attributes: {},
source_identifier_value: :source_identifier,
work_identifier: :source
work_identifier: :source,
work_identifier_search_field: 'source_sim'
)
end
end
Expand Down

0 comments on commit d394861

Please sign in to comment.