Skip to content

Commit

Permalink
Properly format value for date and datetime-local input types
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Oct 20, 2024
1 parent 2c867a3 commit 4c90f94
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
13 changes: 6 additions & 7 deletions lib/datagrid/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.included(base)

module ClassMethods

# Returns filter definition object by name
# @return [Datagrid::Filters::Filter] filter definition object by name
def filter_by_name(attribute)
if attribute.is_a?(Datagrid::Filters::BaseFilter)
unless ancestors.include?(attribute.grid_class)
Expand Down Expand Up @@ -151,12 +151,12 @@ def assets
apply_filters(super, filters)
end

# Returns filter value for given filter definition
# @return [Object] filter value for given filter definition
def filter_value(filter)
self[filter.name]
end

# Returns string representation of filter value
# @return [String] string representation of filter value
def filter_value_as_string(name)
filter = filter_by_name(name)
value = filter_value(filter)
Expand All @@ -167,13 +167,12 @@ def filter_value_as_string(name)
end
end

# Returns filter object with the given name
# @return [Datagrid::Filters::Filter] filter object with the given name
def filter_by_name(name)
self.class.filter_by_name(name)
end

# Returns assets filtered only by specified filters
# Allows partial filtering
# @return [Array<Object>] assets filtered only by specified filters
def filter_by(*filters)
apply_filters(scope, filters.map{|f| filter_by_name(f)})
end
Expand All @@ -199,7 +198,7 @@ def default_filter
self.class.default_filter
end

# Returns all currently enabled filters
# @return [Array<Datagrid::Filters::Filter>] all currently enabled filters
def filters
self.class.filters.select do |filter|
filter.enabled?(self)
Expand Down
9 changes: 7 additions & 2 deletions lib/datagrid/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def datagrid_filter(filter_or_attribute, partials: nil, **options, &block)
# @param filter_or_attribute [Datagrid::Filters::BaseFilter, String, Symbol] filter object or filter name
# @param text [String, nil] label text, defaults to <tt>filter.header</tt>
# @param options [Hash] options of rails <tt>label</tt> helper
# @return [String] a form label html for the corresponding filter name
# @return [String] a form label tag for the corresponding filter name
def datagrid_label(filter_or_attribute, text = nil, **options, &block)
filter = datagrid_get_filter(filter_or_attribute)
label(filter.name, text || filter.header, **filter.label_options, **options, &block)
Expand All @@ -27,7 +27,12 @@ def datagrid_label(filter_or_attribute, text = nil, **options, &block)
def datagrid_filter_input(attribute_or_filter, **options)
filter = datagrid_get_filter(attribute_or_filter)
value = object.filter_value_as_string(filter)
if options[:type]&.to_sym == :textarea
type = options[:type]&.to_sym
if type == :"datetime-local"
datetime_local_field filter.name, **options
elsif type == :"date"
date_field filter.name, **options
elsif type == :textarea
text_area filter.name, value: value, **options, type: nil
else
text_field filter.name, value: value, **options
Expand Down
26 changes: 26 additions & 0 deletions spec/datagrid/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ class MyTemplate
'<textarea class="name string_filter" name="report[name]" id="report_name"/>'
)}
end

context "type is datetime-local" do
let(:_filter) { :created_at }
let(:_grid) {
test_report(created_at: Time.new(2024, 1, 1, 9, 25, 15)) do
scope {Entry}
filter(:created_at, :datetime, input_options: {type: "datetime-local"})
end
}
it { should equal_to_dom(
'<input type="datetime-local" class="created_at date_time_filter" value="2024-01-01T09:25:15" name="report[created_at]" id="report_created_at"/>'
)}
end

context "type is date" do
let(:_filter) { :created_at }
let(:_grid) {
test_report(created_at: Date.new(2024, 1, 1)) do
scope {Entry}
filter(:created_at, :datetime, input_options: {type: :date})
end
}
it { should equal_to_dom(
'<input type="date" class="created_at date_time_filter" value="2024-01-01" name="report[created_at]" id="report_created_at"/>'
)}
end
end

context "with integer filter type and range option" do
Expand Down

0 comments on commit 4c90f94

Please sign in to comment.