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, context:, query_service: default_query_service, persistence_service: default_persistence_service) ⇒ NestCollectionForm

Returns a new instance of NestCollectionForm.

Parameters:


17
18
19
20
21
22
23
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 17

def initialize(parent: nil, child: nil, context:, query_service: default_query_service, persistence_service: default_persistence_service)
  self.parent = parent
  self.child = child
  self.context = context
  self.query_service = query_service
  self.persistence_service = persistence_service
end

Instance Attribute Details

#childObject

Returns the value of attribute child


25
26
27
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 25

def child
  @child
end

#parentObject

Returns the value of attribute parent


25
26
27
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 25

def parent
  @parent
end

Instance Method Details

#available_child_collectionsObject

For the given parent, what are all of the available collections that can be added as sub-collection of the parent.


39
40
41
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 39

def available_child_collections
  query_service.available_child_collections(parent: parent, scope: context)
end

#available_parent_collectionsObject

For the given child, what are all of the available collections to which the child can be added as a sub-collection.


45
46
47
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 45

def available_parent_collections
  query_service.available_parent_collections(child: child, scope: context)
end

#removeObject


61
62
63
64
65
66
67
68
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 61

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

#saveObject


32
33
34
35
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 32

def save
  return false unless valid?
  persistence_service.persist_nested_collection_for(parent: parent, child: child)
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.


52
53
54
55
56
57
58
59
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 52

def validate_add
  if parent.try(:nestable?)
    nesting_within_maximum_depth
  else
    errors.add(:parent, :cannot_have_child_nested)
    false
  end
end