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
- #association_params(form_object:, property:, value:, object:, options: {}) ⇒ Object
- #build_input_attributes(**options) ⇒ Object
- #build_simple_value(form_object, property, value, input_html_value) ⇒ Object
- #default_params(property:, object:, form_object:, value:, options: {}) ⇒ Object
- #merge_with_user_options(builded_options, options) ⇒ Object
- #polymorphic_association_params(object:, form_object:, property:, value:, options: {}) ⇒ Object
- #simple_params(**options) ⇒ Object
- #value_from_params(model_class:, property:, type:) ⇒ Object
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: , 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(**) { label: false, input_html: { name: "#{[:object]}[#{[:property]}]", id: "#{[:object]}_#{[:property]}", value: [:value] }, selected: [:selected], collection: [:collection] }.merge [: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 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 (, ) if [:options].present? [:options].dig(:input_html)&.delete(:value) .merge!() || {} end 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: .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(**) = { as: [:type], label: false, input_html: { name: "#{[:object]}[#{[:property]}]", id: "#{[:object]}_#{[:property]}", value: build_simple_value( *.values_at(:form_object, :property, :value), .dig(:options, :input_html, :value) ) } } , 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 |