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

#available_child_collectionsObject

Deprecated.

this method is unused by hyrax, and is effectively a delegation to `Hyrax::Collections::NestedCollectionQueryService`. if you want to be sure to use nested indexing to generate this list, use the query service directly.

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


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

def available_child_collections
  Deprecation.warn "#{self.class}#available_child_collections " \
                   "is deprecated. the helper of the same name or " \
                   "Hyrax::Collections::NestedCollectionQueryService " \
                   "instead."

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

#available_parent_collectionsObject

Deprecated.

this method is unused by hyrax, and is effectively a delegation to `Hyrax::Collections::NestedCollectionQueryService`. if you want to be sure to use nested indexing to generate this list, use the query service directly.

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


73
74
75
76
77
78
79
80
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 73

def available_parent_collections
  Deprecation.warn "#{self.class}#available_parent_collections " \
                   "is deprecated. the helper of the same name or " \
                   "Hyrax::Collections::NestedCollectionQueryService " \
                   "instead."

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

#removeObject


94
95
96
97
98
99
100
101
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 94

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


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

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.


85
86
87
88
89
90
91
92
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 85

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