-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTMLs for range filters don't conform to the HTML standards #325
Comments
I am open for the change to accept attributes values as I imagine the first step in supporting Besides that, I designed range filter attribute value as an array of two elements because Ruby didn't support infinite ranges ( Let me know what you think or let me digest my own thoughts for a few days and get back. |
Thank you very much for taking this up! Conceptually, I agree with you that a range filter should ideally return a Range, regardless of how it is actually dealt with in HTML forms and Rails built-in processing. Ruby 3 supports both I understand such changes in Range filters are more or less backward-incompatible. A major-version upgrade in versioning may be necessary? Personally, I would accept and welcome the breaking changes with this matter. A way to mitigate to minimise the disruption I can think of is this. This is not perfect, but I guess it should accommodate most use-cases. I suppose two primary, potentially breaking changes for users are the The parameter given to the block is more tricky. However, I guess most users access the given object (currently a 2-element Array) through methods of Obviously, users may have other use-cases. In particular, their controller tests of Grid, where parameters are directly passed to GET, will fail. But a bright side is all W3C-validation tests (in Controller and System tests) that currently fail with Datagrid with Range filters will pass at last. The above is just an idea of hopefully reducing (or minimising) the potential pain of Datagrid users associated with the breaking change. Please feel free to take it or leave it! |
Phase 1 - minor version release: Here is how it is usually done: we introduce a new configuration parameter Additional optional convenience would be to the ability to override a global configuraiton on per filter basis with the same parameter name. It would help larger projects with gradual migration. Phase 2 - major version release: We make the parameter default to Phase 3 - minor version release: We remove the parameter, saying instead that it is deprecated and new behaviour is enforced.
Good point. I believe we should maintain the ability to assign a range attribute as a 2 elements array. I don't see any problems with that. The other problem I see is serialization: in current version if you put datagrid attributes to database (like Job Queue for CSV export), it would serialize to JSON and deserialize normally. With ranges it would break:
So I believe, to make it smoother, we need the ability to assign range in the string format as well.
Yeah that is a problem.
That seems too crazy: changing built-in object behavior even in a scope of the library doesn't look good to me. |
The proposed procedure of phases 1--3 sounds definitely ideal! If you introduce a configuration parameter filter(:year, :integer, range: true, new_ranges_behaviour: false){ |record| }
# record is a 2-element Array.
filter(:year, :integer, range: true, new_ranges_behaviour: true){ |record| }
# record is a Range. This seems to me the cleanest way with the 3-phase gradual migration. I didn't think of serialization… Very good point! |
@masasakano I've addressed all the issues with discussed here. |
@bogdan Marvellous!! Thank you so much. You have adressed all the issues in the best way possible and modernized DataGrid very well! I have trid the branch (ff5193f) in my project, and here is my feedback. Specifically, my environment was Ruby-3.1.2, Rails-7.0.8, and I upgraded Datagrid from 1.7.0 (as in the Gem version) to the First of all, once I have followed the instrucition on Github of upgrading to Ver.2, including changing the base class model from Admittedly, there are a fair amount of points to upgrade, especially the test suite, as CSS class names have changed. But I very much understand that is a necessary price to pay to modernize, and I am happy to follow it. minor points
significant point
major pointsAn integer Range filter works fine and needs no extra work in default in upgrading to Ver.2; for example, this simple filter needed no work: However, I found the date range filter does not work anymore,
For your reference, this is the output from Datagrid Ver.1, which worked as a filter as expected:
I notice that the filter columns are now in I figure out date_filter.masa.20241113.patch That is it for now. Thank you so much again for putting effort to this and modernizing DataGrid. I am extremely grateful! |
Updated.
Typo: help => held
Mentioned.
Very good point. I've addressed it making sure
I can not reproduce that. require 'datagrid/version'
class TestGrid < ApplicationGrid
scope { Article }
filter(:release_date, :date, range: true)
end
g = TestGrid.new(release_date: {from: '2024-08-12', to: '2024-09-05'})
puts Datagrid::VERSION
puts g.release_date.inspect
puts g.assets.to_sql |
Thank you for your update and thorough response! Here is my feedback, including new points. (another) W3C validation failureWhen you specify
I note each docIn README,
Did you mean "hard-coding" instead of "hardcore"? date-filterLet me summarize first. In a word, my assessment was found to be wrong, and Range filters for Date actually work fine! Here is the detail. First of all, your suggestion of producing outputs was most helpful, and I greatly appreciate it! Here is the output (with the latest commit d488792), which actually looks fine. I put it here for record. irb> require 'datagrid/version'
irb> class TestGrid < ApplicationGrid
irb* scope { Article }
irb* filter(:release_date, :date, range: true)
irb> end
=> #<Datagrid::Filters::DateFilter:0x000000010df51258 @block=nil, @grid_class=TestGrid(release_date: date), @name=:release_date, @options={:range=>true}>
irb> g = TestGrid.new(release_date: {from: '2024-08-12', to: '2024-09-05'})
=> #<TestGrid order: nil, descending: nil, release_date: Mon, 12 Aug 2024..Thu, 05 Sep 2024>
irb> puts Datagrid::VERSION
2.0.0
irb> puts g.release_date.inspect
Mon, 12 Aug 2024..Thu, 05 Sep 2024
irb> puts g.assets.to_sql
SELECT "articles".* FROM "articles" WHERE (articles.release_date >= '2024-08-12') AND (articles.release_date <= '2024-09-05')
irb> This (= the fact it seems to be working OK) prompted me to make further investigation, and I found my mistake... Basically, none of the Range filters were working(!) for my app, but I wrongly asserted some of them were working and only date-filter was not working, partly because my system tests accidentally passed (as the result of the conditions being met unintentionally for the specific situation). I dug deeper and found that the cause of the filter not working AND the cause of the input values not appearing in the corresponding fields on the refreshed screen after submission were both related to the handling of Rails In the end, I successfully made it (Datagrid Range filters) work with this: # In Controller:
@grid = ArticlesGrid.new(params[:articles_grid].merge(order: :release_date, descending: true)) do |scope|
# In View:
datagrid_table @grid For your information, what I was caught and ended up spending some time was the following. prms = params[:articles_grid].permit!
@grid = ArticlesGrid.new(order: :release_date, **prms) I found this did not work with the new Range filters. I had to modify it as follows, basically needing to explicitly convert @grid = ArticlesGrid.new(order: :release_date, **(prms.to_h)) In the official doc of DataGrid, the given example This may be just me, but I mention it in case you are interested... (To be honest, I don't think you would need any modifications because of this potential.) a little suggestion for a piece of codeThe def apply(grid_object, scope, value)
if driver.timestamp_column?(scope, name)
value = Datagrid::Utils.format_date_as_timestamp(value)
end
super
end That's it. Thank you again! |
The range filter produced by Datagrid generates a HTML that violates the HTML-5 standards:
(See HTML5 standards about
input
for detail of the specification.)The form in the HTML for a range filter of Datagrid tries to pass a (2-element) Array when submitted, and Rails does interpret them as an Array (and so the filter works). But the HTML standards basically do not allow such pair of fields of input with type=text.
It should be better if the generated HTML for a range filter conforms to the HTML standard.
I asked a question about it in Stackoverflow, and an answer suggests using forms of:
my_model[my_attribute][from]
my_model[my_attribute][to]
as opposed to the current way of multiple
my_model[my_attribute][]
So, this may be a way to implement the (grammatically) correct pair of HTML-form fields for a range filter. Obviously, I suppose there are many other ways.
The text was updated successfully, but these errors were encountered: