Class: AbAdmin::Views::SearchFormBuilder

Inherits:
Ransack::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/ab_admin/views/search_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#ac_select_field(attr, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ab_admin/views/search_form_builder.rb', line 39

def ac_select_field(attr, options={})
  klass = options[:klass]
  options[:param] ||= "#{options[:value_attr] || attr}_eq"
  pre_select = params.val(:q, options[:param]) ? klass.where(id: params[:q][options[:param]]).map(&:for_input_token) : []
  options[:input_html] ||= {}
  options[:input_html].deep_merge! class: 'fancy_select', data: {class: klass.name, pre: pre_select.to_json}
  string_field attr, options
end

#ac_string_field(attr, options = {}) ⇒ Object



65
66
67
68
# File 'lib/ab_admin/views/search_form_builder.rb', line 65

def ac_string_field(attr, options={})
  options.reverse_deep_merge!({input_html: {class: 'ac_field', data: {class: @object.klass.name}}})
  string_field(attr, options)
end

#boolean_field(attr, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ab_admin/views/search_form_builder.rb', line 79

def boolean_field(attr, options={})
  (:div, class: 'pull-left') do
    param = options[:param] || "#{attr}_eq"
    (:label, class: 'checkbox inline') do
      check_box_tag("q[#{param}]", 1, params[:q][param].to_i == 1, class: 'inline', id: "q_#{attr}") + I18n.t('simple_form.yes')
    end +
    (:label, class: 'checkbox inline') do
      check_box_tag("q[#{param}]", 0, params[:q][param] && params[:q][param].to_i == 0, class: 'inline') + I18n.t('simple_form.no')
    end
  end + label(attr, options[:label], class: 'right-label')
end

#date_field(attr, options = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ab_admin/views/search_form_builder.rb', line 48

def date_field(attr, options={})
  label(attr, options[:label]) + (:div, class: 'controls') do
    gt_param, lt_param = "#{attr}_gteq", "#{attr}_lteq"
    text_field_tag("q[#{gt_param}]", params[:q][gt_param], class: 'input-small datepicker', autocomplete: 'off') + ' - ' +
    text_field_tag("q[#{lt_param}]", params[:q][lt_param], class: 'input-small datepicker', autocomplete: 'off', id: "q_#{attr}")
  end
end

#field_type(attr, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ab_admin/views/search_form_builder.rb', line 109

def field_type(attr, options={})
  return options[:as].to_sym if options[:as]
  return :string if attr =~ /^translations_/

  input_type = @object.klass.columns_hash[attr.to_s].try(:type)

  if input_type
    return :select if options[:collection]
    return :string if input_type == :text
  elsif @object.klass.try!(:translates?) && @object.klass.translated?(attr)
    options[:value_attr] = "translations_#{attr}"
    return :string
  elsif assoc = @object.klass.reflect_on_association(attr.to_sym)
    options[:collection] ||= assoc.klass.limit(500)
    options[:value_attr] = "#{attr}_id"
    return :select
  end

  case input_type
    when :timestamp, :datetime, :date
      :date
    when :decimal, :float, :integer
      :number
    else
      input_type or raise "No available input type for #{attr}"
  end
end

#hidden_field(attr, options = {}) ⇒ Object



100
101
102
# File 'lib/ab_admin/views/search_form_builder.rb', line 100

def hidden_field(attr, options={})
  hidden_field_tag("q[#{attr}_eq]", options[:value], options)
end

#input(attr, options = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/ab_admin/views/search_form_builder.rb', line 8

def input(attr, options={})
  field_type = field_type(attr, options)
   :div, class: "clearfix #{field_type} #{options[:wrapper_class]}" do
    public_send("#{field_type}_field", attr, options)
  end
end

#label(attr, text = nil, options = {}) ⇒ Object



104
105
106
107
# File 'lib/ab_admin/views/search_form_builder.rb', line 104

def label(attr, text=nil, options={})
  text ||= @object.klass.han(attr)
  super(attr, text, options)
end

#null_field(attr, options = {}) ⇒ Object



95
96
97
# File 'lib/ab_admin/views/search_form_builder.rb', line 95

def null_field(attr, options={})
  yes_no_field(attr, options.merge(predicates: {yes: %w(null 0), no: %w(null 1)}))
end

#number_field(attr, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/ab_admin/views/search_form_builder.rb', line 70

def number_field(attr, options={})
  label(attr, options[:label]) + (:div, class: 'controls') do
    opts = [['=', 'eq'], ['>', 'gt'], ['<', 'lt']].map { |m| [m[0], "#{attr}_#{m[1]}"] }
    current_filter = (opts.detect { |m| params[:q][m[1]].present? } || opts.first)[1]
    select_tag('', options_for_select(opts, current_filter), class: 'input-small predicate-select') +
    text_field_tag("q[#{current_filter}]", params[:q][current_filter], class: 'input-small', type: :number)
  end
end

#presence_field(attr, options = {}) ⇒ Object



91
92
93
# File 'lib/ab_admin/views/search_form_builder.rb', line 91

def presence_field(attr, options={})
  yes_no_field(attr, options.merge(predicates: {yes: %w(present 1), no: %w(present 0)}))
end

#select_field(attr, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ab_admin/views/search_form_builder.rb', line 15

def select_field(attr, options={})
  label(attr, options[:label]) + (:div, class: 'controls') do
    param = options[:param] || "#{options[:value_attr] || attr}_eq"

    if options[:collection].is_a?(Proc)
      collection = options[:collection].call
    else
      collection = options[:collection] || []
    end

    if collection.first.try(:respond_to?, :id)
      collection = collection.map{|r| [AbAdmin.display_name(r), r.id] }
    end

    options[:input_html] ||= {}
    if options[:fancy] || collection.length > 30
      options[:input_html][:class] = [options[:input_html][:class], 'fancy_select'].join(' ')
    end

    html_options = options[:input_html].merge(include_blank: true, id: "q_#{attr}")
    select_tag("q[#{param}]", options_for_select(collection, params[:q][param]), html_options)
  end
end

#string_field(attr, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ab_admin/views/search_form_builder.rb', line 56

def string_field(attr, options={})
  label(attr, options[:label]) + (:div, class: 'controls') do
    param = options[:param] || "#{options[:value_attr] || attr}_cont"
    options[:input_html] ||= {}
    options[:input_html][:id] = "q_#{attr}"
    text_field_tag("q[#{param}]", params[:q][param], options[:input_html])
  end
end

#yes_no_field(attr, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ab_admin/views/search_form_builder.rb', line 137

def yes_no_field(attr, options={})
  predicates = options[:predicates]
  (:div, class: 'pull-left') do
    (:label, class: 'checkbox inline') do
      param = "#{attr}_#{predicates[:yes][0]}"
      check_box_tag("q[#{param}]", predicates[:yes][1], params[:q][param] == predicates[:yes][1], class: 'inline', id: "q_#{attr}") + I18n.t('simple_form.yes')
    end +
    (:label, class: 'checkbox inline') do
      param = "#{attr}_#{predicates[:no][0]}"
      check_box_tag("q[#{param}]", predicates[:no][1], params[:q][param] == predicates[:no][1], class: 'inline') + I18n.t('simple_form.no')
    end
  end + label(attr, options[:label], class: 'right-label')
end