Module: Sipity::Commands::SipCommands

Extended by:
ActiveSupport::Concern
Defined in:
app/repositories/sipity/commands/sip_commands.rb

Overview

Commands

Constant Summary collapse

BASE_HEADER_ATTRIBUTES =

TODO: This is duplicationed

[:title, :work_publication_strategy].freeze

Instance Method Summary collapse

Instance Method Details

#submit_create_sip_form(form, requested_by:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'app/repositories/sipity/commands/sip_commands.rb', line 18

def submit_create_sip_form(form, requested_by:)
  form.submit do |f|
    sip = Models::Sip.create!(title: f.title, work_publication_strategy: f.work_publication_strategy)
    CollaboratorCommands.create_collaborators_for_sip!(sip: sip, collaborators: f.collaborators)
    AdditionalAttributeCommands.update_sip_publication_date!(sip: sip, publication_date: f.publication_date)
    PermissionCommands.grant_creating_user_permission_for!(entity: sip, user: requested_by)
    EventLogCommands.log_event!(entity: sip, user: requested_by, event_name: __method__)
    sip
  end
end

#submit_update_sip_form(form, requested_by:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'app/repositories/sipity/commands/sip_commands.rb', line 29

def submit_update_sip_form(form, requested_by:)
  form.submit do |f|
    sip = find_sip(f.sip.id)
    with_sip_attributes_for_form(f) { |attributes| sip.update(attributes) }
    with_each_additional_attribute_for_sip_form(f) do |key, values|
      AdditionalAttributeCommands.update_sip_attribute_values!(sip: sip, key: key, values: values)
    end
    EventLogCommands.log_event!(entity: sip, user: requested_by, event_name: __method__) if requested_by
    sip
  end
end

#update_processing_state!(sip:, new_processing_state:) ⇒ Object



12
13
14
15
16
# File 'app/repositories/sipity/commands/sip_commands.rb', line 12

def update_processing_state!(sip:, new_processing_state:)
  # REVIEW: Should this be re-finding the sip? Is it cheating to re-use
  #   the given sip? Is it unsafe as far as state is concerned?
  sip.update(processing_state: new_processing_state)
end

#with_each_additional_attribute_for_sip_form(form) ⇒ Object (private)



43
44
45
46
47
48
# File 'app/repositories/sipity/commands/sip_commands.rb', line 43

def with_each_additional_attribute_for_sip_form(form)
  Queries::AdditionalAttributeQueries.sip_attribute_keys_for(sip: form.sip).each do |key|
    next unless  form.exposes?(key)
    yield(key, form.public_send(key))
  end
end

#with_sip_attributes_for_form(form) {|attributes| ... } ⇒ Object (private)

Yields:

  • (attributes)


50
51
52
53
54
55
56
# File 'app/repositories/sipity/commands/sip_commands.rb', line 50

def with_sip_attributes_for_form(form)
  attributes = {}
  BASE_HEADER_ATTRIBUTES.each do |attribute_name|
    attributes[attribute_name] = form.public_send(attribute_name) if form.exposes?(attribute_name)
  end
  yield(attributes) if attributes.any?
end