Class: Hyrax::Forms::Dashboard::NestCollectionForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/forms/hyrax/forms/dashboard/nest_collection_form.rb

Overview

Responsible for validating that both the parent and child are valid for nesting; If so, then also responsible for persisting those changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, child: nil, parent_id: nil, child_id: nil, context:, query_service: default_query_service, persistence_service: default_persistence_service) ⇒ NestCollectionForm

rubocop:disable Metrics/ParameterLists

Parameters:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 22

def initialize(parent: nil,
               child: nil,
               parent_id: nil,
               child_id: nil,
               context:,
               query_service: default_query_service,
               persistence_service: default_persistence_service)
  self.context = context
  self.query_service = query_service
  self.persistence_service = persistence_service
  self.parent = parent || (parent_id.present? && find_parent(parent_id))
  self.child = child || (child_id.present? && find_child(child_id))
end

Instance Attribute Details

#childObject

rubocop:enable Metrics/ParameterLists



36
37
38
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 36

def child
  @child
end

#parentObject

rubocop:enable Metrics/ParameterLists



36
37
38
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 36

def parent
  @parent
end

Instance Method Details

#removeObject



58
59
60
61
62
63
64
65
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 58

def remove
  if context.can? :edit, parent
    persistence_service.remove_nested_relationship_for(parent: parent, child: child, user: context.current_user)
  else
    errors.add(:parent, :cannot_remove_relationship)
    false
  end
end

#saveObject



42
43
44
45
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 42

def save
  return false unless valid?
  persistence_service.persist_nested_collection_for(parent: parent, child: child, user: context.current_user)
end

#validate_addObject

when creating a NEW collection, we need to do some basic validation before rerouting to new_dashboard_collection_path to add the new collection as a child. Since we don’t yet have a child collection, the valid? option can’t be used here.



50
51
52
53
54
55
56
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 50

def validate_add
  unless nestable?(parent)
    errors.add(:parent, :cannot_have_child_nested)
    return false
  end
  true
end