让 ActiveAdmin 使用包含日期过滤器?
通常,在选择日期范围时,您期望的日期是 包容性。即“11/07/2011” - “11/09/2011”您期望它返回 11 月 7 日、8 日和 9 日公布结果。 ActiveAdmin 过滤器的工作方式 现在看起来该日期范围只会返回以下结果 第8个是违反直觉的。
为了将此行为更改为预期的行为,我必须修改什么?
Usually when selecting date ranges you expect the dates to be
inclusive. i.e. "11/07/2011" - "11/09/2011" you expect it to return
results from Nov. 7th, 8th, and 9th. The way the ActiveAdmin filter is working
right now looks like that date range would only return results from
the 8th which is counter-intuitive.
What would I have to modify in order to change this behaviour to what is expected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然 Raels 的主要想法是正确的,但解决方案太老套了。您不应该修补客户端。我们所要做的就是使用
before_filter
将时间扩展添加到日期时间列的*_lte
输入中。我们可以轻松实现对ActiveAdmin::ResourceDSL
的filter
方法进行monkeypatching。这是解决此问题的通用解决方案:https://gist.github.com/4015836。
While Raels' main idea is correct the solution is too hacky. You should not patch the client side. All we have to do is to add time extension to the
*_lte
inputs of datetime columns usingbefore_filter
. We can easily achieve that monkeypatching thefilter
method ofActiveAdmin::ResourceDSL
.Here is the universal solution that solves this issue: https://gist.github.com/4015836.
AA 中的代码使用 gte 和 lte,因此它试图具有包容性。我发现问题是使用日期时间而不是日期。问题在于代码仅指定日期部分,当扩展到日期时间时会产生午夜的相同日期 (00:00:00.000000)。 gte(无害)和 lte(致命)部分都会发生这种情况。比较 lte 的部分需要与 23:59:59.999999 的时间部分进行比较。
所以,这就是我所做的似乎有效的事情。
在相对路径“app/assets/javascript/make_datetime_lte_work.js.coffee”处创建一个包含以下内容的 CoffeeScript 文件:
接下来,我们将猴子修补日期范围过滤器代码。将以下内容放入相对路径“config/initializers/make_datetime_lte_work.rb”的 ruby 文件中:
现在,请确保编辑您的 app/assets/javascript/active_admin.js 以通过添加对新 javascript 文件的引用来引用它一条评论。这是我的样子:
重新启动 Rails 应用程序,以便调用初始化程序。
现在,过滤器中日期范围的结束部分将在日期后附加 23:59:59.999999,其中将包括(几乎)一整天。
希望有帮助!
The code in AA uses gte and lte so it is trying to be inclusive. I found the issue to be using datetimes instead of dates. The issue is that the code only specifies the date part, which when extended to a datetime yields the same date at midnight (00:00:00.000000). This happens for both the gte (where it is harmless) and lte (where it is deadly) parts. The part comparing lte needs to instead compare against a time part of 23:59:59.999999.
So, here is what I did that seems to work.
Create a coffeescript file at relative path 'app/assets/javascript/make_datetime_lte_work.js.coffee' with the following contents:
Next, we will monkey patch the date range filter code. Put the following content into a ruby file at relative path 'config/initializers/make_datetime_lte_work.rb':
Now, make sure to edit your app/assets/javascript/active_admin.js to reference your new javascript file by adding the reference to it in a comment. Here is what mine looks like:
Restart your rails app so the initializers get called.
Now, the ending part of the date range in the filter will have 23:59:59.999999 appended to the date, which will include (almost) the entire day.
Hope that helps!