Skip to content
A.K.M. Ashrafuzzaman edited this page Jul 10, 2014 · 4 revisions

Customizing Filters

Please view for filters which are supported out of box, view demo.

By default query report supports 3 types of filters.

  • text
  • date
  • boolean

For these 3 types of filters views will be automatically populated(which can be overwritten). But for date filter you would have to bind the datepicker as it can differ from project to project.

If you want to override the existing filters you can do so by defining following methods,

def query_report_text_filter(name, value, options={})
  text_field_tag name, value, options
end

def query_report_date_filter(name, value, options={})
  text_field_tag name, value, options.merge(class: :date)
end

def query_report_datetime_filter(name, value, options={})
  text_field_tag name, value, options.merge(class: :datetime)
end

def query_report_boolean_filter(name, value, options={})
  concat(label_tag options[:placeholder])
  select_tag name, options_for_select([['', ''], ['true', 'true'], ['false', 'false']], value)
end

Custom filter type

reporter(Invoice.scoped) do
  filter :invoiced_to_id, type: :user

  column :invoiced_to_name
  column :invoice_title
  column :invoice_date
end

In a helper file define the following method,

def query_report_user_filter(name, user_id, options={})
  user = User.find(user_id)
  concat hidden_field_tag name, user_id, options
  text_field_tag "#{name}", user.name, class: 'user_search' #implement the filter, it can be autocomplete
end

If a new filter type is added as user, then to render the filter query report will search for a method name query_report_user_filter. So now you can use this filter that you just generated in any other reports as well.

Filter with ransack

reporter(Invoice.scoped) do
  filter :invoice_date, type: :date, comp: {eq: "Date"}

  column :invoiced_to_name
  column :invoice_title
  column :invoice_date
end

With only one comparator date filter will be rendered once to match date equality. By default for date comparator works as shown bellow in case you did not defined any comparator,

reporter(Invoice.scoped) do
  filter :invoice_date, type: :date, comp: {gteq: I18n.t('query_report.filters.from'), lteq: I18n.t('query_report.filters.to')}

  column :invoiced_to_name
  column :invoice_title
  column :invoice_date
end

Manual filter

If you need to write a manual query for the filter, then you can do so as following,

reporter(Invoice.scoped) do
  filter :invoice_title, manual: true do |query, title|
    query.filter_by_title(title) #you can use scope here
  end

  column :invoiced_to_name
  column :invoice_title
  column :invoice_date
end

Default values for filters

Initially you can have a default value setup for the filters. For an example invoice for the last week would come by default.

reporter(Invoice.scoped) do
  filter :invoice_date, type: :date, default: [1.week.ago.to_date, Date.current]

  column :invoiced_to_name
  column :invoice_title
  column :invoice_date
end