Class: Hyrax::Action::CreateValkyrieWork

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/action/create_valkyrie_work.rb

Overview

Encapsulating the logic of several interacting objects. Namely the idea of using a form to #validate given parameters then to #perform the action by leveraging the #transactions‘s #transaction_name configured with appropriate #step_args and ultimately providing the #form to then perform that work.

Since:

  • 5.0.0

Class Attributes collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form:, transactions:, user:, params:, work_attributes_key:) ⇒ CreateValkyrieWork

Returns a new instance of CreateValkyrieWork.

Parameters:

  • form (Object)

    the form that we’ll use for validation and performing the action.

  • transactions (Hyrax::Transactions::Container)

    the transactions

  • user (User)

    the person performing the action

  • params (Hash)

    the contextual parameters for the action; ApplicationController#params if you will.

  • work_attribute_key (String)

    the name of the key within the params that contains the work’s attributes

Since:

  • 5.0.0



31
32
33
34
35
36
37
38
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 31

def initialize(form:, transactions:, user:, params:, work_attributes_key:)
  @form = form
  @transactions = transactions
  @user = user
  @params = params
  @work_attributes_key = work_attributes_key
  @work_attributes = @params.fetch(work_attributes_key, {})
end

Instance Attribute Details

#formObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def form
  @form
end

#paramsObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def params
  @params
end

#parent_idObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def parent_id
  @parent_id
end

#transaction_nameString

Returns the name of the transaction that we call for the CreateValkyrieWorkAction.

Returns:

  • (String)

    the name of the transaction that we call for the CreateValkyrieWorkAction.



19
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 19

class_attribute :transaction_name, default: 'change_set.create_work'

#transactionsObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def transactions
  @transactions
end

#uploaded_filesObject (readonly)

rubocop:disable Lint/DuplicateMethods

Since:

  • 5.0.0



74
75
76
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 74

def uploaded_files
  @uploaded_files
end

#userObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def user
  @user
end

#work_attributesObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def work_attributes
  @work_attributes
end

#work_attributes_keyObject (readonly)

Since:

  • 5.0.0



40
41
42
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 40

def work_attributes_key
  @work_attributes_key
end

Instance Method Details

#perform#value_or

The resulting created work, when successful. When not successful, the returned value call the given block.

Returns:

  • (#value_or)

    the resulting created work, when successful. When not successful, the returned value call the given block.

Since:

  • 5.0.0



54
55
56
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 54

def perform
  transactions[transaction_name].with_step_args(**step_args).call(form)
end

#step_argsHash<String,Hash>

Returns the step args to use for the given #transactions‘s #transaction_name.

Returns:

Since:

  • 5.0.0



63
64
65
66
67
68
69
70
71
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 63

def step_args
  {
    'work_resource.add_to_parent' => { parent_id: params[:parent_id], user: user },
    'work_resource.add_file_sets' => { uploaded_files: uploaded_files, file_set_params: work_attributes[:file_set] },
    'change_set.set_user_as_depositor' => { user: user },
    'work_resource.change_depositor' => { user: ::User.find_by_user_key(form.on_behalf_of) },
    'work_resource.save_acl' => { permissions_params: form.input_params["permissions"] }
  }
end

#validateTrueClass, FalseClass

Returns:

  • (TrueClass)

    when the object is valid.

  • (FalseClass)

    when the object is valid.

Since:

  • 5.0.0



46
47
48
# File 'app/services/hyrax/action/create_valkyrie_work.rb', line 46

def validate
  form.validate(work_attributes)
end