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 metasearch for the field definition.



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

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
# File 'lib/active_admin/filters/resource_extension.rb', line 15

def filters
  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



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

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



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

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



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

def preserve_default_filters!
  @preserve_default_filters = true
end

#preserve_default_filters?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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