Created
April 17, 2014 06:30
-
-
Save shahoxo/10957777 to your computer and use it in GitHub Desktop.
ActiveAdminで日付入力フォームを独自に定義する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#active_adminのlib/active_admin/inputs/filter_date_range_input.rb を参照 | |
module ActiveAdmin | |
module Inputs | |
class FilterCustomDateRangeInput < ::Formtastic::Inputs::StringInput | |
include FilterBase | |
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)), | |
].join("\n").html_safe | |
end | |
end | |
def gt_input_name | |
"#{method}_gt" | |
end | |
alias :input_name :gt_input_name | |
def lt_input_name | |
"#{method}_lteq" | |
end | |
def input_html_options(input_name = gt_input_name) | |
current_value = @object.send(input_name) | |
{ :size => 12, | |
:class => "datepicker", | |
:max => 10, | |
:value => current_value.respond_to?(:strftime) ? current_value.strftime("%Y-%m-%d") : "" } | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveAdmin.register Hoge do | |
filter :reported_at, as: :custom_date_range | |
end |
lteqとかgteqとか書き変えたときに日付が変ると検索したときの日付じゃなくなるのでややこしいのもあるんですが、その辺どう思います?
最初は気にしてなかったんですが結構うざかったりします。
表示では04/15~04/15指定で15日分を検索できる
内部的には帳尻合わせてgt+lteqする
で良いと思います
これって結局scope毎にbefore_actionでgt,ltの値を置換する必要ある感じですよね…
まあいいのか
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CustomDateRangeは変えたのL18だけです