module ActiveAdmin::Filters::ResourceExtension
def active_search_sidebar_section
def active_search_sidebar_section name = :active_search ActiveAdmin::SidebarSection.new name, only: :index, if: -> { active_admin_config.current_filters_enabled? && (params[:q] || params[:scope]) } do filters = ActiveAdmin::Filters::Active.new(active_admin_config, assigns[:search]) render "active_filters", active_filters: filters end end
def add_active_search_sidebar_section
def add_active_search_sidebar_section self.sidebar_sections << active_search_sidebar_section end
def add_filter(attribute, options = {})
-
options
(Hash
) -- The set of options that are passed through to -
attribute
(Symbol
) -- The attribute to filter on
def add_filter(attribute, options = {}) raise Disabled, "add" unless filters_enabled? (@filters ||= {})[attribute.to_sym] = options end
def add_filters_sidebar_section
def add_filters_sidebar_section self.sidebar_sections << filters_sidebar_section end
def current_filters=(bool)
Setter to enable/disable showing current filters on this resource.
def current_filters=(bool) @current_filters_enabled = bool end
def current_filters_enabled?
-
(Boolean)
- If show current filters are enabled for this resource
def current_filters_enabled? @current_filters_enabled.nil? ? namespace.current_filters : @current_filters_enabled end
def custom_ransack_filters
def custom_ransack_filters if resource_class.respond_to?(:_ransackers) resource_class._ransackers.keys.map(&:to_sym) else [] end end
def default_association_filters
def default_association_filters if resource_class.respond_to?(:reflect_on_all_associations) poly, not_poly = resource_class.reflect_on_all_associations.partition { |r| r.macro == :belongs_to && r.options[:polymorphic] } # remove deeply nested associations not_poly.reject! { |r| r.chain.length > 2 } filters = poly.map(&:foreign_type) + not_poly.map(&:name) # Check high-arity associations for filterable columns max = namespace.maximum_association_filter_arity if max != :unlimited high_arity, low_arity = not_poly.partition do |r| r.klass.reorder(nil).limit(max + 1).count > max end # Remove high-arity associations with no searchable column high_arity = high_arity.select { |r| searchable_column_for(r) } high_arity = high_arity.map { |r| r.name.to_s + "_" + searchable_column_for(r) + namespace.filter_method_for_large_association } filters = poly.map(&:foreign_type) + low_arity.map(&:name) + high_arity end filters.map &:to_sym else [] end end
def default_filters
-
(Array)
- The array of default filters for this resource
def default_filters result = [] result.concat default_association_filters if namespace.include_default_association_filters result.concat content_columns result.concat custom_ransack_filters result end
def filter_lookup
Collapses the waveform, if you will, of which filters should be displayed.
def filter_lookup filters = @filters.try(:dup) || {} if filters.empty? || preserve_default_filters? default_filters.each do |f| filters[f] ||= {} end end if @filters_to_remove @filters_to_remove.each { |filter| filters.delete(filter) } end filters end
def filters
-
(Hash)
- Filters that apply for this resource
def filters filters_enabled? ? filter_lookup : {} end
def filters=(bool)
Setter to enable / disable filters on this resource.
def filters=(bool) @filters_enabled = bool end
def filters_enabled?
-
(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 name = :filters ActiveAdmin::SidebarSection.new name, only: :index, if: -> { active_admin_config.filters.any? } do h3 I18n.t("active_admin.sidebars.#{name}", default: name.to_s.titlecase), class: "filters-form-title" active_admin_filters_form_for assigns[:search], **active_admin_config.filters end end
def initialize(*)
def initialize(*) super add_filters_sidebar_section add_active_search_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(*attributes)
-
attributes
(Symbol
) -- The attributes to not filter on
def remove_filter(*attributes) raise Disabled, "remove" unless filters_enabled? attributes.each { |attribute| (@filters_to_remove ||= []) << attribute.to_sym } end
def reset_filters!
def reset_filters! @filters = nil @filters_to_remove = nil end
def search_columns
def search_columns @search_columns ||= namespace.filter_columns_for_large_association.map(&:to_s) end
def searchable_column_for(relation)
def searchable_column_for(relation) relation.klass.column_names.find { |name| search_columns.include?(name) } end