Class: Hyrax::Forms::Dashboard::NestCollectionForm
- Inherits:
-
Object
- Object
- Hyrax::Forms::Dashboard::NestCollectionForm
- 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
-
#child ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#parent ⇒ Object
rubocop:enable Metrics/ParameterLists.
Instance Method Summary collapse
-
#initialize(parent: nil, child: nil, parent_id: nil, child_id: nil, context:, query_service: default_query_service, persistence_service: default_persistence_service) ⇒ NestCollectionForm
constructor
rubocop:disable Metrics/ParameterLists.
- #remove ⇒ Object
- #save ⇒ Object
-
#validate_add ⇒ Object
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.
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
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
#child ⇒ Object
rubocop:enable Metrics/ParameterLists
36 37 38 |
# File 'app/forms/hyrax/forms/dashboard/nest_collection_form.rb', line 36 def child @child end |
#parent ⇒ Object
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
#remove ⇒ Object
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 |
#save ⇒ Object
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_add ⇒ Object
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 |