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 13 14 15 16 17 18 19 20 |
# File 'app/helpers/tramway/core/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) if full_class_name_association.to_s == 'Tramway::User::User' user = defined?(current_user) ? current_user : current_admin value = user.id end 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), include_blank: true, selected: form_object.model.send("#{property}_id") || value) end |
#build_input_attributes(**options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 37 def build_input_attributes(**) { label: false, input_html: { name: "#{options[:object]}[#{options[:property]}]", id: "#{options[:object]}_#{options[:property]}", value: [:value] }, selected: [:selected], collection: [:collection] }.merge [:options] end |
#build_simple_value(form_object, property, value, input_html_value) ⇒ Object
87 88 89 90 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 87 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
62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 62 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
92 93 94 95 96 97 98 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 92 def (, ) if [:options].present? [:options].dig(:input_html)&.delete(:value) .merge!() || {} end end |
#polymorphic_association_params(object:, form_object:, property:, value:, options: {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 22 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, 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
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 74 def simple_params(**) = { as: [:type], label: false, input_html: { name: "#{options[:object]}[#{options[:property]}]", id: "#{options[:object]}_#{options[: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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/helpers/tramway/core/inputs_helper.rb', line 50 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 |