Module: ActiveAdmin::Filters::FormtasticAddons

Included in:
FormBuilder, Inputs::Filters::Base
Defined in:
lib/active_admin/filters/formtastic_addons.rb

Instance Method Summary collapse

Instance Method Details

#columnObject


26
27
28
# File 'lib/active_admin/filters/formtastic_addons.rb', line 26

def column
  column_for method
end

#column_for(method) ⇒ Object


22
23
24
# File 'lib/active_admin/filters/formtastic_addons.rb', line 22

def column_for(method)
  klass.columns_hash[method.to_s] if klass.respond_to? :columns_hash
end

#has_predicate?Boolean

If the given method has a predicate (like _eq or _lteq), it's pretty likely we're dealing with a valid search method.

Returns:

  • (Boolean)

62
63
64
# File 'lib/active_admin/filters/formtastic_addons.rb', line 62

def has_predicate?
  !!Ransack::Predicate.detect_from_string(method.to_s)
end

#humanized_method_nameObject

The below are Formtastic method overrides that jump inside of the Ransack search object to get at the object being searched upon.


10
11
12
13
14
15
16
# File 'lib/active_admin/filters/formtastic_addons.rb', line 10

def humanized_method_name
  if klass.respond_to?(:human_attribute_name)
    klass.human_attribute_name(method)
  else
    method.to_s.public_send(builder.label_str_method)
  end
end

#klassObject

The resource class, unwrapped from Ransack


35
36
37
# File 'lib/active_admin/filters/formtastic_addons.rb', line 35

def klass
  @object.object.klass
end

#polymorphic_foreign_type?(method) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
42
# File 'lib/active_admin/filters/formtastic_addons.rb', line 39

def polymorphic_foreign_type?(method)
  klass.reflect_on_all_associations.select{ |r| r.macro == :belongs_to && r.options[:polymorphic] }
    .map(&:foreign_type).include? method.to_s
end

#ransacker?Boolean

Ransack lets you define custom search methods, called ransackers.

Returns:

  • (Boolean)

67
68
69
# File 'lib/active_admin/filters/formtastic_addons.rb', line 67

def ransacker?
  klass._ransackers.key? method.to_s
end

#reflection_for(method) ⇒ Object


18
19
20
# File 'lib/active_admin/filters/formtastic_addons.rb', line 18

def reflection_for(method)
  klass.reflect_on_association(method) if klass.respond_to? :reflect_on_association
end

#scope?Boolean

Ransack supports exposing selected scopes on your model for advanced searches.

Returns:

  • (Boolean)

72
73
74
75
# File 'lib/active_admin/filters/formtastic_addons.rb', line 72

def scope?
  context = Ransack::Context.for klass
  context.respond_to?(:ransackable_scope?) && context.ransackable_scope?(method.to_s, klass)
end

#searchable_has_many_through?Boolean

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

Returns:

  • (Boolean)

48
49
50
51
52
53
54
# File 'lib/active_admin/filters/formtastic_addons.rb', line 48

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

#seems_searchable?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/active_admin/filters/formtastic_addons.rb', line 56

def seems_searchable?
  has_predicate? || ransacker? || scope?
end