Module: Infold::FormColumnDecorator

Included in:
FormAssociationColumnDecorator
Defined in:
app/decorators/infold/form_column_decorator.rb

Instance Method Summary collapse

Instance Method Details

#code_form_object(ns_camel, form_object_char = 'form', nested_form: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
53
54
55
# File 'app/decorators/infold/form_column_decorator.rb', line 9

def code_form_object(ns_camel, form_object_char = 'form', nested_form: false)
  required = label = ''
  if nested_form
    label = ', label: false'
  else
    required = ', required: true' if model_column.required_validate?
  end
  if form_kind_reference?
    reference_model = model_association.parent_model
    reference_modal_view = AppViewModal.joins(:app).find_by(apps: { model: reference_model })
    return "= render #{ns_camel}::Form::RelationFieldsetComponent.new(:#{reference_model.name_pluralize}, " +
      "#{form_object_char}, :#{model_association.belongs_name}, :#{model_column_name}, :#{modal_search_label_name}#{required}#{label}#{', nested_form: true' if nested_form})" if reference_modal_view
  end
  if form_kind_select? || form_kind_reference?
    _options = if model_column.kind_enum?
                 "#{ns_camel}::#{model.name}.#{model_column_name.pluralize}_i18n.invert"
               elsif model_column.kind_reference?
                 "#{ns_camel}::#{model_association.parent_model_name}.all.limit(100).pluck(:#{model_association.parent_model.label_column_name}, :id)"
               else
                 raise "FormColumn Select 想定外のカラムです model_column_id: #{self.model_column.id}"
               end
    "= render #{ns_camel}::Form::SelectFieldsetComponent.new(#{form_object_char}, :#{model_column_name}, #{_options}, selected_value: #{form_object_char}.object&.#{model_column_name}#{required}#{label})"
  elsif form_kind == 'radio'
    "= render #{ns_camel}::Form::RadioFieldsetComponent.new(#{form_object_char}, :#{model_column_name}, #{ns_camel}::#{model.name}.#{model_column_name.pluralize}_i18n.invert#{required}#{label})"
  elsif form_kind == 'check'
    "= render #{ns_camel}::Form::SwitchElementComponent.new(#{form_object_char}, :#{model_column_name})"
  elsif form_kind_datetime?
    "= render #{ns_camel}::Form::DatetimeFieldsetComponent.new(#{form_object_char}, :#{model_column_name}#{required}#{label})"
  else
    _datepicker = model_column.kind_date? ? ", datepicker: true" : ''
    _unit = if model_column.unit.present? && model_column.unit_position_prefix?
              ", prepend: '#{model_column.unit}'"
            elsif model_column.unit.present? && model_column.unit_position_suffix?
              ", append: '#{model_column.unit}'"
            else
              ""
            end
    _type = if form_kind == 'textarea'
              ", form_kind: :text_area, rows: 5"
            elsif model_column.kind_number? && model_column.number_kind.present? && %(integer bigint).include?(model_column.number_kind)
              ", form_kind: :number_field"
            else
              ""
            end
    "= render #{ns_camel}::Form::InputFieldsetComponent.new(#{form_object_char}, :#{model_column_name}#{_type}#{_datepicker}#{_unit}#{required}#{label})"
  end
end


4
5
6
7
# File 'app/decorators/infold/form_column_decorator.rb', line 4

def modal_search_label_name
  association_model_name = model_association.belongs_name
  "#{association_model_name}_#{model_association.parent_model.label_column_name}"
end