/
base_actor.rb
77 lines (65 loc) · 2.56 KB
/
base_actor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module CurationConcerns
module Actors
# The CurationConcern base actor responds to two primary actions:
# * #create
# * #update
# it must instantiate the next actor in the chain and instantiate it.
# it should respond to curation_concern, user and attributes.
class BaseActor < AbstractActor
attr_reader :cloud_resources
def create(attributes)
@cloud_resources = attributes.delete(:cloud_resources.to_s)
apply_creation_data_to_curation_concern
apply_save_data_to_curation_concern(attributes)
save && next_actor.create(attributes) && run_callbacks(:after_create_concern)
end
def update(attributes)
apply_update_data_to_curation_concern
apply_save_data_to_curation_concern(attributes)
next_actor.update(attributes) && save && run_callbacks(:after_update_metadata)
end
protected
def run_callbacks(hook)
CurationConcerns.config.callback.run(hook, curation_concern, user)
true
end
def apply_creation_data_to_curation_concern
apply_depositor_metadata
apply_deposit_date
end
def apply_update_data_to_curation_concern
true
end
def apply_depositor_metadata
curation_concern.depositor = user.user_key
end
def apply_deposit_date
curation_concern.date_uploaded = CurationConcerns::TimeService.time_in_utc
end
def save
curation_concern.save
end
def apply_save_data_to_curation_concern(attributes)
attributes[:rights] = Array(attributes[:rights]) if attributes.key? :rights
remove_blank_attributes!(attributes)
curation_concern.attributes = attributes
curation_concern.date_modified = CurationConcerns::TimeService.time_in_utc
end
# If any attributes are blank remove them
# e.g.:
# self.attributes = { 'title' => ['first', 'second', ''] }
# remove_blank_attributes!
# self.attributes
# => { 'title' => ['first', 'second'] }
def remove_blank_attributes!(attributes)
multivalued_form_attributes(attributes).each_with_object(attributes) do |(k, v), h|
h[k] = v.instance_of?(Array) ? v.select(&:present?) : v
end
end
# Return the hash of attributes that are multivalued and not uploaded files
def multivalued_form_attributes(attributes)
attributes.select { |_, v| v.respond_to?(:select) && !v.respond_to?(:read) }
end
end
end
end