Class: AjaxFilterInput

Inherits:
Formtastic::Inputs::StringInput
  • Object
show all
Includes:
ActiveAdmin::Inputs::Filters::Base
Defined in:
app/inputs/ajax_filter_input.rb

Instance Method Summary collapse

Instance Method Details

#eq_input_nameObject



13
14
15
# File 'app/inputs/ajax_filter_input.rb', line 13

def eq_input_name
  "#{method}_eq"
end

#get_selected_value(display_name) ⇒ Object

rubocop:disable Style/RescueModifier



33
34
35
36
37
# File 'app/inputs/ajax_filter_input.rb', line 33

def get_selected_value(display_name)
  filter_class = method.to_s.chomp("_id").classify.constantize rescue @object.klass
  selected_value = @object.conditions.find { |c| c.attributes.map(&:name).include?(method.to_s) }.values.first.value rescue nil
  filter_class.find(selected_value).send(display_name) if !!selected_value
end

#input_html_optionsObject

rubocop:disable Metrics/CyclomaticComplexity



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/inputs/ajax_filter_input.rb', line 18

def input_html_options
  opts = {}
  opts[:class] = ['select2-ajax'].concat([@options[:class]] || []).join(' ')
  opts["data-fields"] = (@options[:fields] || []).to_json
  opts["data-url"] = url
  opts["data-response_root"] = @options[:response_root] || @options[:url].to_s.split('/').last
  opts["data-display_name"] = @options[:display_name] || "name"
  opts["data-minimum_input_length"] = @options[:minimum_input_length] || 1
  opts["data-width"] = @options[:width] || "100%"
  opts["data-selected"] = get_selected_value(opts["data-display_name"])
  opts["data-order"] = @options[:order_by] if @options[:order_by]
  super.merge opts
end

#to_htmlObject



4
5
6
7
8
9
10
11
# File 'app/inputs/ajax_filter_input.rb', line 4

def to_html
  input_wrapping do
    [
      label_html,
      builder.text_field(eq_input_name, input_html_options)
    ].join("\n").html_safe
  end
end

#urlObject



39
40
41
42
43
44
45
# File 'app/inputs/ajax_filter_input.rb', line 39

def url
  if @options[:url].is_a?(Proc)
    template.instance_eval(&@options[:url]) || ""
  else
    @options[:url] || ""
  end
end