Module: Tramway::Core::InputsHelper

Includes:
Tramway::Core::Inputs::AssociationsHelper, Tramway::Core::Inputs::PolymorphicAssociationsHelper
Defined in:
app/helpers/tramway/core/inputs_helper.rb

Instance Method Summary collapse

Instance Method Details

#association_params(form_object:, property:, value:, object:, options: {}) ⇒ Object



7
8
9
10
11
12
# File 'app/helpers/tramway/core/inputs_helper.rb', line 7

def association_params(form_object:, property:, value:, object:, options: {})
  build_input_attributes(object: object, property: property, options: options,
                         value: build_value_for_association(form_object, property, value),
                         collection: build_collection_for_association(form_object, property),
                         selected: form_object.model.send("#{property}_id") || value)
end

#build_input_attributes(**options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/tramway/core/inputs_helper.rb', line 28

def build_input_attributes(**options)
  {
    label: false,
    input_html: {
      name: "#{options[:object]}[#{options[:property]}]",
      id: "#{options[:object]}_#{options[:property]}",
      value: options[:value]
    },
    selected: options[:selected],
    collection: options[:collection]
  }.merge options[:options]
end

#build_simple_value(form_object, property, value, input_html_value) ⇒ Object



78
79
80
81
# File 'app/helpers/tramway/core/inputs_helper.rb', line 78

def build_simple_value(form_object, property, value, input_html_value)
  value_to_add = input_html_value || value
  value_to_add || form_object.send(property)
end

#default_params(property:, object:, form_object:, value:, options: {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/tramway/core/inputs_helper.rb', line 53

def default_params(property:, object:, form_object:, value:, options: {})
  {
    label: false,
    input_html: {
      name: "#{object}[#{property}]",
      id: "#{object}_#{property}",
      value: (form_object.send(property) || form_object.model.send(property) || value)
    },
    selected: (form_object.model.send(property) || value)
  }.merge options
end

#merge_with_user_options(builded_options, options) ⇒ Object



83
84
85
86
87
88
89
# File 'app/helpers/tramway/core/inputs_helper.rb', line 83

def merge_with_user_options(builded_options, options)
  if options[:options].present?
    options[:options].dig(:input_html)&.delete(:value)
    builded_options.merge!(options) || {}
  end
  builded_options
end

#polymorphic_association_params(object:, form_object:, property:, value:, options: {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/tramway/core/inputs_helper.rb', line 14

def polymorphic_association_params(object:, form_object:, property:, value:, options: {})
  build_input_attributes object: object, property: property,
                         selected: build_value_for_polymorphic_association(form_object, property, value),
                         value: build_value_for_polymorphic_association(form_object, property, value),
                         collection: build_collection_for_polymorphic_association(form_object, property),
                         options: options.merge(
                           as: :select,
                           label_method: ->(obj) { "#{obj.class.model_name.human} | #{obj.name}" },
                           value_method: lambda { |obj|
                             "#{obj.class.to_s.underscore.sub(/_decorator$/, '')}_#{obj.id}"
                           }
                         )
end

#simple_params(**options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/tramway/core/inputs_helper.rb', line 65

def simple_params(**options)
  builded_options = { as: options[:type], label: false,
                      input_html: {
                        name: "#{options[:object]}[#{options[:property]}]",
                        id: "#{options[:object]}_#{options[:property]}",
                        value: build_simple_value(
                          *options.values_at(:form_object, :property, :value),
                          options.dig(:options, :input_html, :value)
                        )
                      } }
  merge_with_user_options builded_options, options
end

#value_from_params(model_class:, property:, type:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/tramway/core/inputs_helper.rb', line 41

def value_from_params(model_class:, property:, type:)
  case type
  when :polymorphic_association, 'polymorphic_association'
    {
      id: params.dig(model_class.to_s.underscore, property.to_s),
      type: params.dig(model_class.to_s.underscore, "#{property}_type")
    }
  else
    params.dig(model_class.to_s.underscore, property.to_s)
  end
end