Class: Hyrax::Transactions::Steps::SetUploadedDateUnlessPresent

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/transactions/steps/set_uploaded_date_unless_present.rb

Overview

A step that sets the uploaded date for an input ‘Valkyrie::Resource` or `ValkyrieChangeSet`.

The uploaded date is derived in the following way:

- if a `date_uploaded` is already present, keep it;
- if there is no current `date_uploaded` but `date_modified` is
  present, use the value of `date_modified`.
- if neither `date_uploaded` nor `date_modified` is present, set the
  time to now using the given `time_service`. `Hyrax::TimeService`
  is used by default.

A useful pattern is to run this step immediately following one to set the ‘date_modified` to now, and just before validation and save. This pattern ensures the times for a newly created object have the same value as close to the actual save time as practicable, and avoids overwriting `date_uploaded` for existing objects.

Since:

  • 3.0.0

Instance Method Summary collapse

Constructor Details

#initialize(time_service: Hyrax::TimeService) ⇒ SetUploadedDateUnlessPresent

Returns a new instance of SetUploadedDateUnlessPresent.

Parameters:

  • time_service (#time_in_utc) (defaults to: Hyrax::TimeService)

Since:

  • 3.0.0



30
31
32
# File 'lib/hyrax/transactions/steps/set_uploaded_date_unless_present.rb', line 30

def initialize(time_service: Hyrax::TimeService)
  @time_service = time_service
end

Instance Method Details

#call(obj) ⇒ Dry::Monads::Result

Note:

the implementation sets the uploaded date to ‘#date_modified` if it exists, falling back on the current datetime.

Parameters:

  • obj (#date_uploaded=)

Returns:

  • (Dry::Monads::Result)

Since:

  • 3.0.0



41
42
43
44
45
46
47
48
# File 'lib/hyrax/transactions/steps/set_uploaded_date_unless_present.rb', line 41

def call(obj)
  return Failure[:no_date_uploaded_attribute, obj] unless
    obj.respond_to?(:date_uploaded=)

  obj.date_uploaded = date_uploaded(obj) if obj.date_uploaded.blank?

  Success(obj)
end