module ActiveAdmin::Filters::ResourceExtension

def add_filter(attribute, options = {})

Parameters:
  • options (Hash) -- The set of options that are passed through to
  • attribute (Symbol) -- The attribute to filter on
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

def add_filters_sidebar_section

def add_filters_sidebar_section
  self.sidebar_sections << filters_sidebar_section
end

def default_association_filters

Returns a default set of filters for the associations
def default_association_filters
  if resource_class.respond_to?(:reflections)
    block = if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 0
      proc{ |_,r| [r.options[:foreign_type], r.primary_key_name] }
    else
      proc{ |_,r| [r.foreign_type, r.foreign_key] }
    end
    poly, not_poly = resource_class.reflections.partition{ |_,r| r.macro == :belongs_to && r.options[:polymorphic] }
    filters        = poly.map(&block).flatten + not_poly.map(&:first)
    filters.collect{ |name| { :attribute => name.to_sym } }
  else
    []
  end
end

def default_content_filters

Returns a default set of filters for the content columns
def default_content_filters
  if resource_class.respond_to?(:content_columns)
    resource_class.content_columns.collect{|c| { :attribute => c.name.to_sym } }
  else
    []
  end
end

def default_filters

Returns:
  • (Array) - The array of default filters for this resource
def default_filters
  default_association_filters + default_content_filters
end

def filter_lookup

Removes filters and adds in default filters as desired.
Collapses the waveform, if you will, of which filters should be displayed.
def filter_lookup
  filters = @filters.try(:dup) || []
  filters.push *default_filters if filters.empty? || preserve_default_filters?
  if @filters_to_remove
    @filters_to_remove.each do |attr|
      filters.delete_if{ |f| f.fetch(:attribute) == attr }
    end
  end
  filters
end

def filters

Returns:
  • (Array) - Filters that apply for this resource
def filters
  return [] unless filters_enabled?
  filter_lookup
end

def filters=(bool)

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

Setter to enable / disable filters on this resource.
def filters=(bool)
  @filters_enabled = bool
end

def filters_enabled?

Returns:
  • (Boolean) - If filters are enabled for this resource
def filters_enabled?
  @filters_enabled.nil? ? namespace.filters : @filters_enabled
end

def filters_sidebar_section

def filters_sidebar_section
  ActiveAdmin::SidebarSection.new(:filters, :only => :index, :if => proc{ active_admin_config.filters.any? } ) do
    active_admin_filters_form_for assigns[:search], active_admin_config.filters
  end
end

def initialize(*)

def initialize(*)
  super
  add_filters_sidebar_section
end

def preserve_default_filters!

def preserve_default_filters!
  @preserve_default_filters = true
end

def preserve_default_filters?

def preserve_default_filters?
  @preserve_default_filters == true
end

def remove_filter(attribute)

Parameters:
  • attribute (Symbol) -- The attribute to not filter on
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

def reset_filters!

Reset the filters to use defaults
def reset_filters!
  @filters = nil
  @filters_to_remove = nil
end