Module: ActiveAdmin::Filters::ResourceExtension

Defined in:
lib/active_admin/filters/resource_extension.rb

Instance Method Summary collapse

Instance Method Details

#add_filter(attribute, options = {}) ⇒ Object

Add a filter for this resource. If filters are not enabled, this method will raise a RuntimeError

Parameters:

  • attribute (Symbol)

    The attribute to filter on

  • options (Hash) (defaults to: {})

    The set of options that are passed through to ransack for the field definition.



59
60
61
62
63
64
65
66
# File 'lib/active_admin/filters/resource_extension.rb', line 59

def add_filter(attribute, options = {})
  unless filters_enabled?
    raise RuntimeError, "Can't add a filter when filters are disabled. Enable filters with 'config.filters = true'"
  end

  @filters ||= []
  @filters << options.merge({ :attribute => attribute })
end

#filtersArray

Returns the filters for this resource. If filters are not enabled, it will always return an empty array.

Returns:

  • (Array)

    Filters that apply for this resource



15
16
17
18
# File 'lib/active_admin/filters/resource_extension.rb', line 15

def filters
  return [] unless filters_enabled?
  filter_lookup
end

#filters=(bool) ⇒ Object

Setter to enable / disable filters on this resource.

Set to ‘nil` to inherit the setting from the namespace



23
24
25
# File 'lib/active_admin/filters/resource_extension.rb', line 23

def filters=(bool)
  @filters_enabled = bool
end

#filters_enabled?Boolean

Returns If filters are enabled for this resource.

Returns:

  • (Boolean)

    If filters are enabled for this resource



28
29
30
# File 'lib/active_admin/filters/resource_extension.rb', line 28

def filters_enabled?
  @filters_enabled.nil? ? namespace.filters : @filters_enabled
end

#initializeObject



6
7
8
9
# File 'lib/active_admin/filters/resource_extension.rb', line 6

def initialize(*)
  super
  add_filters_sidebar_section
end

#preserve_default_filters!Object



32
33
34
# File 'lib/active_admin/filters/resource_extension.rb', line 32

def preserve_default_filters!
  @preserve_default_filters = true
end

#preserve_default_filters?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_admin/filters/resource_extension.rb', line 36

def preserve_default_filters?
  @preserve_default_filters == true
end

#remove_filter(attribute) ⇒ Object

Remove a filter for this resource. If filters are not enabled, this method will raise a RuntimeError

Parameters:

  • attribute (Symbol)

    The attribute to not filter on



44
45
46
47
48
49
50
51
# File 'lib/active_admin/filters/resource_extension.rb', line 44

def remove_filter(attribute)
  unless filters_enabled?
    raise RuntimeError, "Can't remove a filter when filters are disabled. Enable filters with 'config.filters = true'"
  end

  @filters_to_remove ||= []
  @filters_to_remove << attribute
end

#reset_filters!Object

Reset the filters to use defaults



69
70
71
72
# File 'lib/active_admin/filters/resource_extension.rb', line 69

def reset_filters!
  @filters = nil
  @filters_to_remove = nil
end