Class: Koi::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
GOVUKDesignSystemFormBuilder::Builder
Defined in:
lib/koi/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#admin_archive(text = "Archive", **kwargs) ⇒ Object

Generates an archive link formatted as a button that will perform a turbo delete with a confirmation.



27
28
29
# File 'lib/koi/form_builder.rb', line 27

def admin_archive(text = "Archive", **kwargs)
  admin_delete(text, **kwargs)
end

#admin_delete(text = "Delete", url: nil, confirm: "Are you sure?", data: {}, **kwargs) ⇒ Object

Generates a delete link formatted as a button that will perform a turbo delete with a confirmation.



16
17
18
19
20
21
22
23
# File 'lib/koi/form_builder.rb', line 16

def admin_delete(text = "Delete", url: nil, confirm: "Are you sure?", data: {}, **kwargs)
  return unless object.persisted?

  link_to(text, url || url_for(action: :destroy),
          class: "button button--secondary",
          data:  data.reverse_merge(turbo_method: :delete, turbo_confirm: confirm),
          **kwargs)
end

#admin_discard(text = "Discard", url: :back, **kwargs) ⇒ Object

Generates a discard changes link formatted as a text button that navigates the user back to the previous page.



33
34
35
# File 'lib/koi/form_builder.rb', line 33

def admin_discard(text = "Discard", url: :back, **kwargs)
  link_to(text, url, class: "button button--text", **kwargs)
end

#admin_save(text = "Save", name: :commit, value: :save, class: "button button--primary", **kwargs) ⇒ Object

Generates a submit button for saving admin resources.



10
11
12
# File 'lib/koi/form_builder.rb', line 10

def admin_save(text = "Save", name: :commit, value: :save, class: "button button--primary", **kwargs)
  button(text, name:, value:, class:, **kwargs)
end

#govuk_document_field(attribute_name, hint: {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/koi/form_builder.rb', line 39

def govuk_document_field(attribute_name, hint: {}, **, &)
  if hint.is_a?(Hash)
    max_size = hint.fetch(:max_size, Koi.config.document_size_limit)
    hint[:text] ||= t("helpers.hint.default.document", max_size: @template.number_to_human_size(max_size))
  end

  super(attribute_name, hint:, **, &)
end

#govuk_image_field(attribute_name, hint: {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/koi/form_builder.rb', line 50

def govuk_image_field(attribute_name, hint: {}, **, &)
  if hint.is_a?(Hash)
    max_size = hint.fetch(:max_size, Koi.config.image_size_limit)
    hint[:text] ||= t("helpers.hint.default.document", max_size: @template.number_to_human_size(max_size))
  end

  super(attribute_name, hint:, **, &)
end

#govuk_rich_text_area(attribute_name, data: {}, **kwargs, &block) ⇒ Object

Use content editor trix setup by default.

See Also:

  • GOVUKDesignSystemFormBuilder::Builder#govuk_rich_text_area


63
64
65
66
67
68
69
70
# File 'lib/koi/form_builder.rb', line 63

def govuk_rich_text_area(attribute_name, data: {}, **kwargs, &block)
  data = data.reverse_merge(
    direct_upload_url: @template.katalyst_content.direct_uploads_url,
    controller:        "content--editor--trix",
    action:            "trix-initialize->content--editor--trix#trixInitialize",
  )
  super(attribute_name, data:, **kwargs, &block)
end