Module: Tramway::InputsHelper

Includes:
Tramway::Inputs::AssociationsHelper, Tramway::Inputs::PolymorphicAssociationsHelper
Defined in:
app/helpers/tramway/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
13
14
15
16
17
# File 'app/helpers/tramway/inputs_helper.rb', line 7

def association_params(form_object:, property:, value:, object:, options: {})
  full_class_name_association = form_object.class.full_class_name_association(property)

  value = current_user&.id if full_class_name_association.to_s == 'Tramway::User'

  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),
    include_blank: true,
    selected: form_object.model.send("#{property}_id") || value)
end

#build_input_attributes(**options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/tramway/inputs_helper.rb', line 34

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



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

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



59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/tramway/inputs_helper.rb', line 59

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



89
90
91
92
93
94
95
# File 'app/helpers/tramway/inputs_helper.rb', line 89

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

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/tramway/inputs_helper.rb', line 19

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,
      include_blank: true,
      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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/tramway/inputs_helper.rb', line 71

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



47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/tramway/inputs_helper.rb', line 47

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