-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Simpson
committed
Mar 7, 2024
1 parent
cfeaa6b
commit 9186417
Showing
2 changed files
with
1,608 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
module HykuKnapsack | ||
module Actors | ||
class JSONFieldsActor < Hyrax::Actors::AbstractActor | ||
def create(env) | ||
puts "LOG_CREATE_AT_JSONFieldsActor_BEFORE_jsonify_fields #{env.inspect}" | ||
jsonify_fields(env) && next_actor.create(env) | ||
|
||
end | ||
|
||
def update(env) | ||
jsonify_fields(env) && next_actor.update(env) | ||
end | ||
|
||
private | ||
|
||
def jsonify_fields(env) | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_18 #{env.inspect}" | ||
env.curation_concern.class.json_fields.each do |field| | ||
# This handles the case when field is a key/value pair coming from the yaml schema | ||
field = field.first if field.is_a?(Array) | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_22_env #{env.inspect}" | ||
|
||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_24_field #{field.inspect}" | ||
if name_blank?(field, env.attributes[field]) || recursive_blank?(env.attributes[field]) | ||
env.attributes.delete(field) | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_27_env #{env.inspect}" | ||
else | ||
env.attributes[field].reject! { |o| name_blank?(field, o) || recursive_blank?(o) } if env.attributes[field].is_a?(Array) | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_30_env #{env.inspect}" | ||
env.attributes[field] = env.attributes[field].to_json | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_32_env #{env.inspect}" | ||
end | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_34_env #{env.inspect}" | ||
ensure_multiple!(env, field) | ||
puts "LOG_jsonify_fields_AT_JSONFieldsActor_Line_36_env #{env.inspect}" | ||
end | ||
end | ||
|
||
def name_blank?(field, obj) | ||
return false unless field.in? [:creator, :contributor, :editor] | ||
|
||
recursive_blank?(Array(obj).map { |o| o.reject { |k, _v| k == "#{field}_name_type" } }) | ||
end | ||
|
||
def recursive_blank?(obj) | ||
case obj | ||
when Hash | ||
obj.values.all? { |o| recursive_blank?(o) } | ||
when Array | ||
obj.all? { |o| recursive_blank?(o) } | ||
else | ||
obj.blank? | ||
end | ||
end | ||
|
||
def ensure_multiple!(env, field) | ||
env.attributes[field] = Array(env.attributes[field]) if env.curation_concern.class.multiple?(field) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.