Class: HakumiComponents::FormBuilder::ModelBinding

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/form_builders/hakumi_components/form_builder/model_binding.rb

Overview

typed: strict frozen_string_literal: true

Instance Method Summary collapse

Constructor Details

#initialize(object_name:, object:) ⇒ ModelBinding

Returns a new instance of ModelBinding.



8
9
10
11
# File 'app/form_builders/hakumi_components/form_builder/model_binding.rb', line 8

def initialize(object_name:, object:)
  @object_name = T.let(object_name, T.nilable(String))
  @object = T.let(object, T.nilable(ActiveModel::Model))
end

Instance Method Details

#build_field_context(method:, explicit_value:, id_suffix: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/form_builders/hakumi_components/form_builder/model_binding.rb', line 20

def build_field_context(method:, explicit_value:, id_suffix: nil)
  field_name = if @object_name
    "#{@object_name}[#{method}]"
  else
    method.to_s
  end

  base_id = if @object_name
    "#{@object_name}_#{method}"
  else
    method.to_s
  end

  field_id = if id_suffix
    "#{base_id}_#{id_suffix}".parameterize.underscore
  else
    base_id
  end

  value = explicit_value.nil? ? object_value(method) : explicit_value

  HakumiComponents::FormBuilder::FieldContext.new(
    name: field_name,
    id: field_id,
    value: value,
    errors: object_errors(method)
  )
end

#enhance_options(method:, options:) ⇒ Object



65
66
67
68
69
70
71
72
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
106
107
108
109
110
111
# File 'app/form_builders/hakumi_components/form_builder/model_binding.rb', line 65

def enhance_options(method:, options:)
  model = form_object
  return options unless model

  enhanced = options.dup

  if enhanced[:label].nil?
    detected_label = HakumiComponents::Rails::AttributeIntrospection.human_attribute_name(model, method)
    enhanced[:label] = detected_label if detected_label
  end

  if enhanced[:required].nil?
    enhanced[:required] = HakumiComponents::Rails::ValidationIntrospection.required?(model, method)
  end

  if enhanced[:placeholder].nil?
    detected_placeholder = HakumiComponents::Rails::AttributeIntrospection.placeholder(model, method)
    enhanced[:placeholder] = detected_placeholder if detected_placeholder
  end

  if enhanced[:caption].nil?
    detected_hint = HakumiComponents::Rails::AttributeIntrospection.hint(model, method)
    enhanced[:caption] = detected_hint if detected_hint
  end

  detected_rules = HakumiComponents::Rails::ValidationMapper.to_frontend_rules(model, method)
  manual_rules = enhanced[:rules]

  if manual_rules.nil?
    enhanced[:rules] = detected_rules unless detected_rules.empty?
  elsif manual_rules.is_a?(Array) && !detected_rules.empty?
    enhanced[:rules] = HakumiComponents::Rails::ValidationMapper.merge_rules(manual_rules, detected_rules)
  end

  if enhanced[:type].nil?
    if HakumiComponents::Rails::ValidationIntrospection.email_field?(model, method)
      enhanced[:type] = :email
    elsif HakumiComponents::Rails::ValidationIntrospection.url_field?(model, method)
      enhanced[:type] = :url
    else
      detected_type = HakumiComponents::Rails::AttributeIntrospection.input_type_from_column(model, method)
      enhanced[:type] = detected_type if detected_type != :text
    end
  end

  enhanced
end

#errors_for(method) ⇒ Object



55
56
57
# File 'app/form_builders/hakumi_components/form_builder/model_binding.rb', line 55

def errors_for(method)
  object_errors(method)
end

#value_for(method) ⇒ Object



50
51
52
# File 'app/form_builders/hakumi_components/form_builder/model_binding.rb', line 50

def value_for(method)
  object_value(method)
end