Skip to content

Instantly share code, notes, and snippets.

@oakbow
Last active August 29, 2015 14:14
Show Gist options
  • Save oakbow/7fdb303c4650947bc2dc to your computer and use it in GitHub Desktop.
Save oakbow/7fdb303c4650947bc2dc to your computer and use it in GitHub Desktop.
ActiveAdminの検索条件から、has_manyとhas_oneを除外する ref: http://qiita.com/Oakbow/items/3d577468c0106ccc320b
module ActiveAdmin
module Filters
# lib/active_admin/filters/resource_extension.rb
# サイドバーの検索条件から、default_association_filters を対象外にする。
# has_manyやhas_oneがプルダウン条件になって恐ろしく重くなる現象の回避策。
module ResourceExtension
# Returns a default set of filters for the associations
def default_association_filters
if resource_class.respond_to?(:reflect_on_all_associations)
poly, not_poly = resource_class.reflect_on_all_associations.
select{ |rr| rr.macro == :belongs_to }.partition{ |r| r.options[:polymorphic] }
# remove deeply nested associations
not_poly.reject!{ |r| r.chain.length > 2 }
filters = poly.map(&:foreign_type) + not_poly.map(&:name)
filters.map &:to_sym
else
[]
end
end
end
end
end
module ActiveAdmin
module Filters
# lib/active_admin/filters/resource_extension.rb
# サイドバーの検索条件から、default_association_filters を対象外にする。
# has_manyやhas_oneがプルダウン条件になって恐ろしく重くなる現象の回避策。
module ResourceExtension
# Returns a default set of filters for the associations
def default_association_filters
if resource_class.respond_to?(:reflect_on_all_associations)
poly, not_poly = resource_class.reflect_on_all_associations.
select{ |rr| rr.macro == :belongs_to }.partition{ |r| r.options[:polymorphic] }
# remove deeply nested associations
not_poly.reject!{ |r| r.chain.length > 2 }
filters = poly.map(&:foreign_type) + not_poly.map(&:name)
filters.map &:to_sym
else
[]
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment