Sha256: f1711617dcf8e1164f76ec76cae72c22f6977a3a80c75727b5aa69b216a79522

Contents?: true

Size: 1.63 KB

Versions: 8

Compression:

Stored size: 1.63 KB

Contents

module ActiveAdmin
  module Inputs
    module Filters
      class DateRangeInput < ::Formtastic::Inputs::StringInput
        include Base

        def to_html
          input_wrapping do
            [ label_html,
              builder.text_field(gt_input_name, input_html_options_for(gt_input_name, gt_input_placeholder)),
              builder.text_field(lt_input_name, input_html_options_for(lt_input_name, lt_input_placeholder)),
            ].join("\n").html_safe
          end
        end

        def gt_input_name
          column && column.type == :date ? "#{method}_gteq" : "#{method}_gteq_datetime"
        end
        alias :input_name :gt_input_name

        def lt_input_name
          column && column.type == :date ? "#{method}_lteq" : "#{method}_lteq_datetime"
        end

        def input_html_options
          { size: 12,
            class: "datepicker",
            maxlength: 10 }.merge(options[:input_html] || {})
        end

        def input_html_options_for(input_name, placeholder)
          current_value = begin
                            #cast value to date object before rendering input
                            @object.public_send(input_name).to_s.to_date
                          rescue
                            nil
                          end
          { placeholder: placeholder,
            value: current_value ? current_value.strftime("%Y-%m-%d") : "" }.merge(input_html_options)
        end

        def gt_input_placeholder
          I18n.t("active_admin.filters.predicates.from")
        end

        def lt_input_placeholder
          I18n.t("active_admin.filters.predicates.to")
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/inputs/filters/date_range_input.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/inputs/filters/date_range_input.rb
activeadmin-2.9.0 lib/active_admin/inputs/filters/date_range_input.rb
activeadmin-2.8.1 lib/active_admin/inputs/filters/date_range_input.rb
activeadmin-2.8.0 lib/active_admin/inputs/filters/date_range_input.rb
activeadmin-2.7.0 lib/active_admin/inputs/filters/date_range_input.rb
activeadmin-2.6.1 lib/active_admin/inputs/filters/date_range_input.rb
activeadmin-2.6.0 lib/active_admin/inputs/filters/date_range_input.rb