Module: ChobbleForms::Helpers

Extended by:
T::Sig
Defined in:
lib/chobble_forms/helpers.rb

Constant Summary collapse

SelectOption =
T.type_alias do
  [String, T.any(String, Integer)]
end
LocalAssignValue =
T.type_alias do
  T.any(
    String,
    Symbol,
    Integer,
    Float,
    T::Boolean,
    T::Array[SelectOption],
    T::Hash[Symbol, T.untyped]
  )
end
FieldValue =
T.type_alias do
  T.nilable(T.any(
    String,
    Integer,
    Float,
    Date,
    DateTime,
    Time,
    T::Boolean,
    T.untyped
  ))
end
FieldSetupResult =
T.type_alias do
  {
    form_object: T.untyped,
    i18n_base: String,
    value: FieldValue,
    prefilled: T::Boolean,
    field_label: String,
    field_hint: T.nilable(String),
    field_placeholder: T.nilable(String)
  }
end

Instance Method Summary collapse

Instance Method Details

#comment_field_options(form, comment_field, base_field_label) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chobble_forms/helpers.rb', line 73

def comment_field_options(form, comment_field, base_field_label)
  raise ArgumentError, "form_object required" unless form

  model = form.object

  comment_value, comment_prefilled =
    get_field_value_and_prefilled_status(
      form,
      comment_field
    )

  has_comment = comment_value.present?

  base_field = comment_field.to_s.chomp("_comment")

  placeholder_text = t("shared.field_comment_placeholder", field: base_field_label)
  textarea_id = "#{base_field}_comment_textarea_#{model.object_id}"
  checkbox_id = "#{base_field}_has_comment_#{model.object_id}"
  display_style = has_comment ? "block" : "none"

  {
    options: {
      rows: 2,
      placeholder: placeholder_text,
      id: textarea_id,
      style: "display: #{display_style};",
      value: comment_value
    },
    prefilled: comment_prefilled,
    has_comment: has_comment,
    checkbox_id: checkbox_id
  }
end

#form_field_setup(field, local_assigns) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/chobble_forms/helpers.rb', line 52

def form_field_setup(field, local_assigns)
  validate_local_assigns(local_assigns)
  validate_form_context

  field_translations = build_field_translations(field)
  form_obj = T.unsafe(instance_variable_get(:@_current_form))
  value, prefilled = get_field_value_and_prefilled_status(form_obj, field)

  build_field_setup_result(field_translations, value, prefilled)
end

#get_field_value_and_prefilled_status(form_object, field) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/chobble_forms/helpers.rb', line 64

def get_field_value_and_prefilled_status(form_object, field)
  return [nil, false] unless form_object&.object

  model = form_object.object
  resolved = resolve_field_value(model, field)
  [resolved[:value], resolved[:prefilled]]
end

#radio_button_options(prefilled, checked_value, expected_value) ⇒ Object



111
112
113
# File 'lib/chobble_forms/helpers.rb', line 111

def radio_button_options(prefilled, checked_value, expected_value)
  (prefilled && checked_value == expected_value) ? {checked: true} : {}
end