Class: AbAdmin::Views::SearchFormBuilder
- Inherits:
-
Ransack::Helpers::FormBuilder
- Object
- Ransack::Helpers::FormBuilder
- AbAdmin::Views::SearchFormBuilder
- Defined in:
- lib/ab_admin/views/search_form_builder.rb
Instance Method Summary collapse
- #ac_select_field(attr, options = {}) ⇒ Object
- #ac_string_field(attr, options = {}) ⇒ Object
- #boolean_field(attr, options = {}) ⇒ Object
- #conditional_wrapper(attr, options, &block) ⇒ Object
- #date_field(attr, options = {}) ⇒ Object
- #extract_label(attr, options) ⇒ Object
- #field_type(attr, options = {}) ⇒ Object
- #hidden_field(attr, options = {}) ⇒ Object
- #input(attr, options = {}) ⇒ Object
- #label(attr, text = nil, options = {}) ⇒ Object
- #null_field(attr, options = {}) ⇒ Object
- #number_field(attr, options = {}) ⇒ Object
-
#presence_field(attr, options = {}) ⇒ Object
Should be used only for string columns.
- #select_field(attr, options = {}) ⇒ Object
- #string_field(attr, options = {}) ⇒ Object
- #yes_no_field(attr, options = {}) ⇒ Object
Instance Method Details
#ac_select_field(attr, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 50 def ac_select_field(attr, ={}) klass = [:klass] [:param] ||= "#{[:value_attr] || attr}_eq" pre_select = params[:q].try!(:[], [:param]) ? klass.where(id: params[:q][[:param]]).map(&:for_input_token) : [] [:input_html] ||= {} [:input_html].deep_merge! class: 'fancy_select', data: {class: klass.name, pre: pre_select.to_json} string_field attr, end |
#ac_string_field(attr, options = {}) ⇒ Object
87 88 89 90 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 87 def ac_string_field(attr, ={}) .reverse_deep_merge!({input_html: {class: 'ac_field', data: {class: @object.klass.name}}}) string_field(attr, ) end |
#boolean_field(attr, options = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 101 def boolean_field(attr, ={}) content_tag(:div, class: 'pull-left') do param = [:param] || "#{attr}_eq" content_tag(: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 + content_tag(: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, [:label], class: 'right-label') end |
#conditional_wrapper(attr, options, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 67 def conditional_wrapper(attr, , &block) if [:compact_labels] [:input_html] ||= {} [:input_html][:placeholder] ||= extract_label(attr, ) wrapper_html = {'class' => 'controls js-tooltip', 'data-placement' => 'left', 'title' => [:input_html][:placeholder]} content_tag(:div, wrapper_html, &block) else label(attr, [:label]) + content_tag(:div, class: 'controls', &block) end end |
#date_field(attr, options = {}) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 59 def date_field(attr, ={}) label(attr, [:label]) + content_tag(: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 |
#extract_label(attr, options) ⇒ Object
131 132 133 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 131 def extract_label(attr, ) [:label].presence || @object.klass.han(attr) end |
#field_type(attr, options = {}) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 135 def field_type(attr, ={}) return [:as].to_sym if [:as] return :string if attr =~ /^translations_/ input_type = @object.klass.columns_hash[attr.to_s].try(:type) if input_type return :select if [:collection] return :string if input_type == :text elsif @object.klass.try!(:translates?) && @object.klass.translated?(attr) [:value_attr] = "translations_#{attr}" return :string elsif assoc = @object.klass.reflect_on_association(attr.to_sym) [:collection] ||= assoc.klass.limit(500) [: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
122 123 124 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 122 def hidden_field(attr, ={}) hidden_field_tag("q[#{attr}_eq]", [:value], ) end |
#input(attr, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 10 def input(attr, ={}) field_type = field_type(attr, ) [:wrapper_html] ||= {} [:wrapper_html][:class] = "clearfix #{field_type} #{[:wrapper_html][:class]}" content_tag :div, [:wrapper_html] do public_send("#{field_type}_field", attr, ) end end |
#label(attr, text = nil, options = {}) ⇒ Object
126 127 128 129 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 126 def label(attr, text=nil, ={}) text ||= @object.klass.han(attr) super(attr, text, ) end |
#null_field(attr, options = {}) ⇒ Object
118 119 120 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 118 def null_field(attr, ={}) yes_no_field(attr, .merge(predicates: {yes: %w(null 0), no: %w(null 1)})) end |
#number_field(attr, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 92 def number_field(attr, ={}) label(attr, [:label]) + content_tag(: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('', (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
Should be used only for string columns
114 115 116 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 114 def presence_field(attr, ={}) yes_no_field(attr, .merge(predicates: {yes: %w(present 1), no: %w(present 0)})) end |
#select_field(attr, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 19 def select_field(attr, ={}) conditional_wrapper attr, do param = [:param] || "#{[:value_attr] || attr}_eq" if [:collection].is_a?(Proc) collection = [:collection].call else collection = [:collection] || [] end if collection.first.try(:respond_to?, :id) collection = collection.map{|r| [AbAdmin.display_name(r), r.id] } end [:input_html] ||= {} [:input_html][:id] = "q_#{attr}" if [:fancy] || collection.length > 30 [:input_html][:class] = [[:input_html][:class], 'fancy_select'].join(' ') end if [:input_html][:placeholder] [:input_html][:include_blank] = false [:input_html][:prompt] ||= [:input_html][:placeholder] else [:input_html][:include_blank] = true end select_tag("q[#{param}]", (collection, params[:q][param]), [:input_html]) end end |
#string_field(attr, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 78 def string_field(attr, ={}) conditional_wrapper attr, do param = [:param] || "#{[:value_attr] || attr}_cont" [:input_html] ||= {} [:input_html][:id] = "q_#{attr}" text_field_tag("q[#{param}]", params[:q][param], [:input_html]) end end |
#yes_no_field(attr, options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ab_admin/views/search_form_builder.rb', line 163 def yes_no_field(attr, ={}) predicates = [:predicates] content_tag(:div, class: 'pull-left') do content_tag(: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 + content_tag(: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, [:label], class: 'right-label') end |