foodsoft/app/inputs/delta_input.rb
Philipp Rothmann fb2b4d8a8a chore: rubocop
chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
2023-06-09 17:35:05 +02:00

26 lines
1.1 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class DeltaInput < SimpleForm::Inputs::StringInput
# for now, need to pass id or it won't work
def input(wrapper_options)
options = merge_wrapper_options(input_html_options, wrapper_options)
options[:type] = 'text'
options[:data] ||= {}
options[:data][:delta] ||= 1
options[:autocomplete] ||= 'off'
# TODO: get generated id, don't know how yet - `add_default_name_and_id_for_value` might be an option
template.content_tag :div, class: 'delta-input input-prepend input-append' do
delta_button(content_tag(:i, nil, class: 'icon icon-minus'), -1, options) +
@builder.text_field(attribute_name, options) +
delta_button(content_tag(:i, nil, class: 'icon icon-plus'), 1, options)
end
end
# template.button_tag('', type: :submit, data: {decrement: options[:id]}, tabindex: -1, class: 'btn') +
private
def delta_button(title, direction, options)
data = { (direction > 0 ? 'increment' : 'decrement') => options[:id] }
delta = direction * options[:data][:delta]
template.button_tag(title, type: :button, name: 'delta', value: delta, data: data, tabindex: -1, class: 'btn')
end
end