-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring VisibilityCopyJob and documenting
Reducing duplication of behavior to a consistent process. Improving documentation. The duplication was identified via `flay`
- Loading branch information
Showing
1 changed file
with
20 additions
and
10 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 |
---|---|---|
@@ -1,18 +1,28 @@ | ||
# Responsible for copying the following attributes from the work to each file in the file_sets | ||
# | ||
# * visibility | ||
# * lease | ||
# * embargo | ||
class VisibilityCopyJob < ActiveJob::Base | ||
# @api public | ||
# @param [#file_sets, #visibility, #lease, #embargo] work - a Work model | ||
def perform(work) | ||
work.file_sets.each do |file| | ||
file.visibility = work.visibility # visibility must come first, because it can clear an embargo/lease | ||
if work.lease | ||
file.build_lease unless file.lease | ||
file.lease.attributes = work.lease.attributes.except('id') | ||
file.lease.save | ||
end | ||
if work.embargo | ||
file.build_embargo unless file.embargo | ||
file.embargo.attributes = work.embargo.attributes.except('id') | ||
file.embargo.save | ||
end | ||
copy_visibility_modifier(work: work, file: file, modifier: :lease) | ||
copy_visibility_modifier(work: work, file: file, modifier: :embargo) | ||
file.save! | ||
end | ||
end | ||
|
||
private | ||
|
||
def copy_visibility_modifier(work:, file:, modifier:) | ||
work_modifier = work.public_send(modifier) | ||
return unless work_modifier | ||
file_modifier = file.public_send(modifier) | ||
file.public_send("build_#{modifier}") unless file_modifier | ||
file_modifier.attributes = work_modifier.attributes.except('id') | ||
file_modifier.save | ||
end | ||
end |