Skip to content

Instantly share code, notes, and snippets.

@Fivell
Created November 1, 2012 18:43
Show Gist options
  • Save Fivell/3995659 to your computer and use it in GitHub Desktop.
Save Fivell/3995659 to your computer and use it in GitHub Desktop.
activeadmin numeric range filter
module ActiveAdmin
module Inputs
class FilterNumericRangeInput < ::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}_gte"
end
alias :input_name :gt_input_name
def lt_input_name
"#{method}_lte"
end
def input_html_options(input_name = gt_input_name)
current_value = @object.send(input_name)
{ :size => 10, :id => "#{input_name}_numeric" , :value => current_value}
end
end
end
end
@Fivell
Copy link
Author

Fivell commented Mar 9, 2013

css

body.active_admin {

sidebar {

.filter_numeric_range {
input {
width: 88px;
}
.seperator {
display: inline-block;
text-align: center;
width: 12px;
}
}
}
}

@kochis
Copy link

kochis commented Oct 9, 2014

Using the latest version of ActiveAdmin, I had to change

      def gt_input_name
        "#{method}_gte"
      end
      alias :input_name :gt_input_name

      def lt_input_name
        "#{method}_lte"
      end

to

      def gt_input_name
        "#{method}_gteq"
      end
      alias :input_name :gt_input_name

      def lt_input_name
        "#{method}_lteq"
      end

@Yegorov
Copy link

Yegorov commented Sep 17, 2018

In latest version (1.0.0 or above), it must be:

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



      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}_gteq"
      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 => 10, :id => "#{input_name}_numeric" , :value => current_value}
      end
    end
  end
end

@swedishpotato
Copy link

swedishpotato commented Sep 19, 2018

I've no doubt this is a great answer - but please let us know WHERE in the Rails project you should insert the file filter_numeric_range_input.rb - I've tried several places but it simply isn't being picked up by AA.

Assuming the following example code to use it is also correct, yes?

filter :id , :as => :numeric_range

@deepakmahakale
Copy link

deepakmahakale commented Feb 17, 2019

You should create the file inside config/initializers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment