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.

Raises:



75
76
77
78
79
# File 'lib/active_admin/filters/resource_extension.rb', line 75

def add_filter(attribute, options = {})
  raise Disabled, "add" unless filters_enabled?

  (@filters ||= {})[attribute.to_sym] = options
end

#current_filters=(bool) ⇒ Object

Setter to enable/disable showing current filters on this resource.

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



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

def current_filters=(bool)
  @current_filters_enabled = bool
end

#current_filters_enabled?Boolean

Returns If show current filters are enabled for this resource.

Returns:

  • (Boolean)

    If show current filters are enabled for this resource



47
48
49
# File 'lib/active_admin/filters/resource_extension.rb', line 47

def current_filters_enabled?
  @current_filters_enabled.nil? ? namespace.current_filters : @current_filters_enabled
end

#filtersHash

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

Returns:

  • (Hash)

    Filters that apply for this resource



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

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



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

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



42
43
44
# File 'lib/active_admin/filters/resource_extension.rb', line 42

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

#initializeObject



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

def initialize(*)
  super
  add_filters_sidebar_section
  add_active_search_sidebar_section
end

#preserve_default_filters!Object



51
52
53
# File 'lib/active_admin/filters/resource_extension.rb', line 51

def preserve_default_filters!
  @preserve_default_filters = true
end

#preserve_default_filters?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_admin/filters/resource_extension.rb', line 55

def preserve_default_filters?
  @preserve_default_filters == true
end

#remove_filter(*attributes) ⇒ Object

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

Parameters:

  • attributes (Symbol)

    The attributes to not filter on

Raises:



63
64
65
66
67
# File 'lib/active_admin/filters/resource_extension.rb', line 63

def remove_filter(*attributes)
  raise Disabled, "remove" unless filters_enabled?

  attributes.each { |attribute| (@filters_to_remove ||= []) << attribute.to_sym }
end

#reset_filters!Object

Reset the filters to use defaults



82
83
84
85
# File 'lib/active_admin/filters/resource_extension.rb', line 82

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