Class: Compony::Components::WithForm

Inherits:
Compony::Component show all
Defined in:
lib/compony/components/with_form.rb

Overview

This component is destined to take a sub-component that is a form component. It can be called via :get or via submit_verb depending on whether its form should be shown or submitted.

Direct Known Subclasses

Edit, New

Instance Attribute Summary

Attributes inherited from Compony::Component

#comp_opts, #content_blocks, #parent_comp

DSL collapse

Instance Method Summary collapse

Methods inherited from Compony::Component

#before_render, comp_name, #content, #exposed_intents, family_name, #id, #id_path, #id_path_hash, #inspect, #param_name, #path, #remove_content, #remove_content!, #render, #resourceful?, #root_comp, #root_comp?, setup, #sub_comp

Constructor Details

#initializeWithForm

Returns a new instance of WithForm.



7
8
9
10
11
# File 'lib/compony/components/with_form.rb', line 7

def initialize(...)
  @submit_path_block = proc { Compony.path(comp_name, family_name, @data) }
  @form_cancancan_action = :missing
  super
end

Instance Method Details

#form_cancancan_action(new_form_cancancan_action = :missing) ⇒ Symbol?

DSL method Sets and gets the form's CanCanCan action, used for per-field permitted_attributes. Pass nil to disable per-field auth.

Parameters:

  • new_form_cancancan_action (Symbol, nil) (defaults to: :missing)

    The CanCanCan action (e.g. :edit); omit to read the current value.

Returns:

  • (Symbol, nil)

    The configured CanCanCan action.



56
57
58
59
60
61
# File 'lib/compony/components/with_form.rb', line 56

def form_cancancan_action(new_form_cancancan_action = :missing)
  if new_form_cancancan_action != :missing
    @form_cancancan_action = new_form_cancancan_action
  end
  return @form_cancancan_action
end

#form_compObject

Returns an instance of the form component responsible for rendering the form. Feel free to override this in subclasses.



15
16
17
18
19
20
21
22
23
# File 'lib/compony/components/with_form.rb', line 15

def form_comp
  @form_comp ||= (form_comp_class || Compony.comp_class_for!(:form, family_name)).new(
    self,
    submit_verb:,
    # If applicable, Rails adds the route keys automatically, thus, e.g. :id does not need to be passed here, as it comes from the request.
    submit_path:      @submit_path_block,
    cancancan_action: form_cancancan_action
  )
end

#form_comp_class(new_form_comp_class = nil) ⇒ Class?

DSL method Overrides the Form component class instantiated by #form_comp (defaults to the same-family Form).

Parameters:

  • new_form_comp_class (Class, nil) (defaults to: nil)

    The Form component class to use.

Returns:

  • (Class, nil)

    The configured form component class.



47
48
49
# File 'lib/compony/components/with_form.rb', line 47

def form_comp_class(new_form_comp_class = nil)
  @form_comp_class ||= new_form_comp_class
end

#submit_path {|controller| ... } ⇒ void

This method returns an undefined value.

DSL method Overrides the submit path, which otherwise defaults to this component's own path.

Yields:

  • (controller)

    Called with the controller; expected to return the Rails path the form submits to.



68
69
70
# File 'lib/compony/components/with_form.rb', line 68

def submit_path(&new_submit_path_block)
  @submit_path_block = new_submit_path_block
end

#submit_verb(new_submit_verb = nil) ⇒ Symbol

DSL method Sets or returns the HTTP verb the twinned form submits with (:post for New, :patch for Edit).

Parameters:

  • new_submit_verb (Symbol, nil) (defaults to: nil)

    If given, sets the submit verb; must be a known HTTP verb.

Returns:

  • (Symbol)

    The (possibly just set) submit verb.



32
33
34
35
36
37
38
39
40
# File 'lib/compony/components/with_form.rb', line 32

def submit_verb(new_submit_verb = nil)
  if new_submit_verb.present?
    new_submit_verb = new_submit_verb.to_sym
    available_verbs = ComponentMixins::Default::Standalone::VerbDsl::AVAILABLE_VERBS
    fail "Unknown HTTP verb #{new_submit_verb.inspect}, use one of #{available_verbs.inspect}" unless available_verbs.include?(new_submit_verb)
    @submit_verb = new_submit_verb
  end
  return @submit_verb || fail("WithForm component #{self} is missing a call to `submit_verb`.")
end