让 ActiveAdmin 使用包含日期过滤器?

发布于 2024-12-19 16:04:24 字数 175 浏览 3 评论 0原文

通常,在选择日期范围时,您期望的日期是 包容性。即“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2024-12-26 16:04:24

虽然 Raels 的主要想法是正确的,但解决方案太老套了。您不应该修补客户端。我们所要做的就是使用 before_filter 将时间扩展添加到日期时间列的 *_lte 输入中。我们可以轻松实现对ActiveAdmin::ResourceDSLfilter 方法进行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 using before_filter. We can easily achieve that monkeypatching the filter method of ActiveAdmin::ResourceDSL.

Here is the universal solution that solves this issue: https://gist.github.com/4015836.

£噩梦荏苒 2024-12-26 16:04:24

AA 中的代码使用 gte 和 lte,因此它试图具有包容性。我发现问题是使用日期时间而不是日期。问题在于代码仅指定日期部分,当扩展到日期时间时会产生午夜的相同日期 (00:00:00.000000)。 gte(无害)和 lte(致命)部分都会发生这种情况。比较 lte 的部分需要与 23:59:59.999999 的时间部分进行比较。

所以,这就是我所做的似乎有效的事情。

在相对路径“app/assets/javascript/make_datetime_lte_work.js.coffee”处创建一个包含以下内容的 CoffeeScript 文件:

    $(document).ready ->
      $('input.datepickerlte').datepicker 'option', {dateFormat: 'yy-mm-dd     23:59:59.99999'}

接下来,我们将猴子修补日期范围过滤器代码。将以下内容放入相对路径“config/initializers/make_datetime_lte_work.rb”的 ruby​​ 文件中:

module ActiveAdmin
  module Inputs
    class FilterDateRangeInput

      def to_html
        input_wrapping do
          [ label_html,
            builder.text_field(gt_input_name, input_html_options(gt_input_name)),
            template.content_tag(:span, "-", :class => "seperator"),
            builder.text_field(lt_input_name, input_html_options(lt_input_name, ' datepickerlte')),
          ].join("\n").html_safe
        end
      end

      def input_html_options(input_name = gt_input_name, extra_class = '')
        current_value = @object.send(input_name)
        { :size => 12,
          :class => "datepicker" + extra_class,
          :max => 10,
          :value => current_value.respond_to?(:strftime) ? current_value.strftime("%Y-%m-%d") : "" }
      end
    end
  end
end

现在,请确保编辑您的 app/assets/javascript/active_admin.js 以通过添加对新 javascript 文件的引用来引用它一条评论。这是我的样子:

//= require active_admin/base
//= require make_datetime_lte_work

重新启动 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:

    $(document).ready ->
      $('input.datepickerlte').datepicker 'option', {dateFormat: 'yy-mm-dd     23:59:59.99999'}

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':

module ActiveAdmin
  module Inputs
    class FilterDateRangeInput

      def to_html
        input_wrapping do
          [ label_html,
            builder.text_field(gt_input_name, input_html_options(gt_input_name)),
            template.content_tag(:span, "-", :class => "seperator"),
            builder.text_field(lt_input_name, input_html_options(lt_input_name, ' datepickerlte')),
          ].join("\n").html_safe
        end
      end

      def input_html_options(input_name = gt_input_name, extra_class = '')
        current_value = @object.send(input_name)
        { :size => 12,
          :class => "datepicker" + extra_class,
          :max => 10,
          :value => current_value.respond_to?(:strftime) ? current_value.strftime("%Y-%m-%d") : "" }
      end
    end
  end
end

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:

//= require active_admin/base
//= require make_datetime_lte_work

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文