Skip to content

Commit

Permalink
filter code style update
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlinsley committed Nov 20, 2013
1 parent 642776c commit 50ae6b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
15 changes: 9 additions & 6 deletions lib/active_admin/filters/formtastic_addons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ def polymorphic_foreign_type?(method)
.map(&:foreign_type).include? method.to_s
end

def searchable_through_association?(method = method)
ref = reflection_for(method)
ref.options[:through] && ref.through_reflection.klass.ransackable_attributes.include?(ref.foreign_key) if ref
end

#
# These help figure out whether the given method will be recognized by Ransack.
# These help figure out whether the given method or association will be recognized by Ransack.
#

def searchable_has_many_through?
if reflection && reflection.options[:through]
reflection.through_reflection.klass.ransackable_attributes.include? reflection.foreign_key
else
false
end
end

def seems_searchable?
has_predicate? || ransacker?
end
Expand Down
7 changes: 2 additions & 5 deletions lib/active_admin/inputs/filter_check_boxes_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ def selected_values
end

def searchable_method_name
# Deal with has_many :through relationships in filters
# If the relationship is a HMT, we set the search logic to be something
# like :#{through_association}_#{end_association_id}.
if searchable_through_association?
[reflection.through_reflection.name, reflection.foreign_key].join('_')
if searchable_has_many_through?
"#{reflection.through_reflection.name}_#{reflection.foreign_key}"
else
association_primary_key || method
end
Expand Down
14 changes: 3 additions & 11 deletions lib/active_admin/inputs/filter_select_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@ module Inputs
class FilterSelectInput < ::Formtastic::Inputs::SelectInput
include FilterBase

# If Ransack will likely respond to the given method, use it.
#
# Otherwise:
# When it's a HABTM or has_many association, Formtastic builds "object_ids".
# That doesn't fit our scenario, so we override it here.
def input_name
return method if seems_searchable?

searchable_method_name.concat multiple? ? '_in' : '_eq'
end

def searchable_method_name
# Deal with has_many :through relationships in filters
# If the relationship is a HMT, we set the search logic to be something
# like :#{through_association}_#{end_association_id}.
if searchable_through_association?
name = [reflection.through_reflection.name, reflection.foreign_key].join('_')
if searchable_has_many_through?
"#{reflection.through_reflection.name}_#{reflection.foreign_key}"
else
name = method.to_s
name.concat '_id' if reflection
name
end
name
end

# Provide the AA translation to the blank input field.
Expand Down

0 comments on commit 50ae6b9

Please sign in to comment.