From eeb0fae1e06cf5aedaaa765416f0fe46a7cdd219 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Mon, 13 Nov 2023 11:18:19 +0200 Subject: [PATCH] Prepare internals for input type customization --- app/views/datagrid/_range_filter.html.erb | 4 +-- lib/datagrid/filters/base_filter.rb | 1 + lib/datagrid/form_builder.rb | 30 +++++++++++++---------- spec/datagrid/form_builder_spec.rb | 28 ++++++++++----------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/app/views/datagrid/_range_filter.html.erb b/app/views/datagrid/_range_filter.html.erb index cd5b8c0..7a8a123 100644 --- a/app/views/datagrid/_range_filter.html.erb +++ b/app/views/datagrid/_range_filter.html.erb @@ -1,3 +1,3 @@ -<%= form.text_field(filter.name, from_options) %> +<%= form.datagrid_filter_input(filter, **from_options) %> <%= I18n.t('datagrid.filters.range.separator') %> -<%= form.text_field(filter.name, to_options) %> +<%= form.datagrid_filter_input(filter, **to_options) %> diff --git a/lib/datagrid/filters/base_filter.rb b/lib/datagrid/filters/base_filter.rb index 8bdaa89..73d863f 100644 --- a/lib/datagrid/filters/base_filter.rb +++ b/lib/datagrid/filters/base_filter.rb @@ -6,6 +6,7 @@ class Datagrid::FilteringError < StandardError # @!visibility private class Datagrid::Filters::BaseFilter + class_attribute :input_helper_name, instance_writer: false attr_accessor :grid_class, :options, :block, :name def initialize(grid_class, name, options = {}, &block) diff --git a/lib/datagrid/form_builder.rb b/lib/datagrid/form_builder.rb index acade4c..dd747b1 100644 --- a/lib/datagrid/form_builder.rb +++ b/lib/datagrid/form_builder.rb @@ -8,14 +8,12 @@ module FormBuilder # * select for enum, xboolean filter types # * check_box for boolean filter type # * text_field for other filter types - def datagrid_filter(filter_or_attribute, options = {}, &block) + def datagrid_filter(filter_or_attribute, partials: nil, **options, &block) filter = datagrid_get_filter(filter_or_attribute) options = { **filter.input_options, **add_html_classes(options, filter.name, datagrid_filter_html_class(filter)), } - # Prevent partials option from appearing in HTML attributes - options.delete(:partials) # Legacy option self.send(filter.form_builder_helper_name, filter, options, &block) end @@ -28,6 +26,11 @@ def datagrid_label(filter_or_attribute, text = nil, **options, &block) label(filter.name, text || filter.header, **filter.label_options, **options, &block) end + def datagrid_filter_input(attribute_or_filter, **options) + filter = datagrid_get_filter(attribute_or_filter) + text_field filter.name, value: object.filter_value_as_string(filter), **options + end + protected def datagrid_extended_boolean_filter(attribute_or_filter, options = {}) datagrid_enum_filter(attribute_or_filter, options) @@ -46,23 +49,21 @@ def datagrid_date_time_filter(attribute_or_filter, options = {}) end def datagrid_default_filter(attribute_or_filter, options = {}) - filter = datagrid_get_filter(attribute_or_filter) - text_field filter.name, value: object.filter_value_as_string(filter), **options + datagrid_filter_input(attribute_or_filter, **options) end def datagrid_enum_filter(attribute_or_filter, options = {}, &block) filter = datagrid_get_filter(attribute_or_filter) if filter.checkboxes? - partial = partial_path('enum_checkboxes') options = add_html_classes(options, 'checkboxes') elements = object.select_options(filter).map do |element| text, value = @template.send(:option_text_and_value, element) checked = enum_checkbox_checked?(filter, value) [value, text, checked] end - @template.render( - partial: partial, - locals: { + render_partial( + 'enum_checkboxes', + { elements: elements, form: self, filter: filter, @@ -149,15 +150,14 @@ def dynamic_filter_select(name, variants, select_options, html_options) def datagrid_range_filter(type, attribute_or_filter, options = {}) filter = datagrid_get_filter(attribute_or_filter) if filter.range? - partial = partial_path('range_filter') options = options.merge(multiple: true) from_options = datagrid_range_filter_options(object, filter, :from, options) to_options = datagrid_range_filter_options(object, filter, :to, options) - @template.render partial: partial, locals: { + render_partial 'range_filter', { from_options: from_options, to_options: to_options, filter: filter, form: self } else - datagrid_default_filter(filter, options) + datagrid_filter_input(filter, **options) end end @@ -180,7 +180,7 @@ def datagrid_range_filter_options(object, filter, type, options) end def datagrid_string_filter(attribute_or_filter, options = {}) - datagrid_default_filter(attribute_or_filter, options) + datagrid_range_filter(:string, attribute_or_filter, options) end def datagrid_float_filter(attribute_or_filter, options = {}) @@ -219,6 +219,10 @@ def partial_path(name) File.join('datagrid', name) end + def render_partial(name, locals) + @template.render partial: partial_path(name), locals: locals + end + class Error < StandardError end end diff --git a/spec/datagrid/form_builder_spec.rb b/spec/datagrid/form_builder_spec.rb index 77262d5..54f7d20 100644 --- a/spec/datagrid/form_builder_spec.rb +++ b/spec/datagrid/form_builder_spec.rb @@ -112,16 +112,16 @@ class MyTemplate let(:_filter_options) { {:id => "hello"} } let(:_range) { [1,2]} it { should equal_to_dom( - '' + + '' + ' - ' + - '' + '' )} end context "with only left bound" do let(:_range) { [10, nil]} it { should equal_to_dom( - '' + + '' + ' - ' + '' )} @@ -132,7 +132,7 @@ class MyTemplate it { should equal_to_dom( '' + ' - ' + - '' + '' )} it { should be_html_safe } end @@ -140,9 +140,9 @@ class MyTemplate context "with invalid range value" do let(:_range) { 2..1 } it { should equal_to_dom( - '' + + '' + ' - ' + - '' + '' )} end @@ -174,9 +174,9 @@ class MyTemplate } let(:_range) { [1.5,2.5]} it { should equal_to_dom( - '' + + '' + ' - ' + - '' + '' )} end @@ -192,7 +192,7 @@ class MyTemplate let(:_range) { ["2012-01-03", nil]} it { should equal_to_dom( - '' + + '' + ' - ' + '' )} @@ -206,9 +206,9 @@ class MyTemplate end let(:_range) { ["2013/01/01", '2013/02/02']} it { should equal_to_dom( - '' + + '' + ' - ' + - '' + '' )} end context "with only right bound" do @@ -217,7 +217,7 @@ class MyTemplate it { should equal_to_dom( '' + ' - ' + - '' + '' )} it { should be_html_safe } end @@ -225,9 +225,9 @@ class MyTemplate context "with invalid range value" do let(:_range) { Date.parse('2012-01-02')..Date.parse('2012-01-01') } it { should equal_to_dom( - '' + + '' + ' - ' + - '' + '' )} end context "with blank range value" do