Module: ActiveScaffold::Helpers::SearchColumnHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/search_column_helpers.rb
Overview
Helpers that assist with the rendering of a Form Column
Instance Method Summary collapse
-
#active_scaffold_search_boolean(column, options) ⇒ Object
(also: #active_scaffold_search_checkbox)
we can’t use active_scaffold_input_boolean because we need to have a nil value even when column can’t be null to decide whether search for this field or not.
- #active_scaffold_search_date(column, options) ⇒ Object
- #active_scaffold_search_datetime(column, options) ⇒ Object (also: #active_scaffold_search_timestamp)
-
#active_scaffold_search_for(column, options = nil) ⇒ Object
This method decides which input to use for the given column.
-
#active_scaffold_search_multi_select(column, options) ⇒ Object
Search input methods.
- #active_scaffold_search_null(column, options) ⇒ Object
-
#active_scaffold_search_options(column) ⇒ Object
the standard active scaffold options used for class, name and scope.
- #active_scaffold_search_range(column, options) ⇒ Object (also: #active_scaffold_search_integer, #active_scaffold_search_decimal, #active_scaffold_search_float, #active_scaffold_search_string)
- #active_scaffold_search_range_comparator_options(column) ⇒ Object
- #active_scaffold_search_range_string?(column) ⇒ Boolean
- #active_scaffold_search_select(column, html_options) ⇒ Object
- #active_scaffold_search_text(column, options) ⇒ Object
- #active_scaffold_search_time(column, options) ⇒ Object
- #field_search_datetime_value(value) ⇒ Object
- #field_search_params_range_values(column) ⇒ Object
- #include_null_comparators?(column) ⇒ Boolean
-
#override_search(form_ui) ⇒ Object
the naming convention for overriding search input types with helpers.
- #override_search_field(column) ⇒ Object
- #search_attribute(column, record) ⇒ Object
-
#search_column_label(column, record) ⇒ Object
Search column override signatures.
- #search_label_for(column, options) ⇒ Object
- #searched_by?(column) ⇒ Boolean
- #visibles_and_hiddens(search_config) ⇒ Object
Instance Method Details
#active_scaffold_search_boolean(column, options) ⇒ Object Also known as: active_scaffold_search_checkbox
we can’t use active_scaffold_input_boolean because we need to have a nil value even when column can’t be null to decide whether search for this field or not
121 122 123 124 125 126 127 128 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 121 def active_scaffold_search_boolean(column, ) = [] << [as_(:_select_), nil] << [as_(:true), true] << [as_(:false), false] select_tag([:name], (, column.column.type_cast(field_search_params[column.name])), :id => [:id]) end |
#active_scaffold_search_date(column, options) ⇒ Object
225 226 227 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 225 def active_scaffold_search_date(column, ) active_scaffold_search_datetime(column, .merge!(:discard_time => true)) end |
#active_scaffold_search_datetime(column, options) ⇒ Object Also known as: active_scaffold_search_timestamp
216 217 218 219 220 221 222 223 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 216 def active_scaffold_search_datetime(column, ) opt_value, from_value, to_value = field_search_params_range_values(column) = column..merge() helper = "select_#{'date' unless [:discard_date]}#{'time' unless [:discard_time]}" send(helper, field_search_datetime_value(from_value), {:include_blank => true, :prefix => "#{[:name]}[from]"}.merge()) << ' - '.html_safe << send(helper, field_search_datetime_value(to_value), {:include_blank => true, :prefix => "#{[:name]}[to]"}.merge()) end |
#active_scaffold_search_for(column, options = nil) ⇒ Object
This method decides which input to use for the given column. It does not do any rendering. It only decides which method is responsible for rendering.
7 8 9 10 11 12 13 14 15 16 17 18 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 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 7 def active_scaffold_search_for(column, = nil) ||= (column) # first, check if the dev has created an override for this specific field for search if (method = override_search_field(column)) send(method, [:object] || @record, ) # second, check if the dev has specified a valid search_ui for this column, using specific ui for searches elsif column.search_ui and (method = override_search(column.search_ui)) send(method, column, ) # third, check if the dev has specified a valid search_ui for this column, using generic ui for forms elsif column.search_ui and (method = override_input(column.search_ui)) send(method, column, ) # fourth, check if the dev has created an override for this specific field elsif (method = override_form_field(column)) send(method, [:object] || @record, ) # fallback: we get to make the decision else if column.association or column.virtual? active_scaffold_search_text(column, ) else # regular model attribute column # if we (or someone else) have created a custom render option for the column type, use that if (method = override_search(column.column.type)) send(method, column, ) # if we (or someone else) have created a custom render option for the column type, use that elsif (method = override_input(column.column.type)) send(method, column, ) # final ultimate fallback: use rails' generic input method else # for textual fields we pass different options text_types = [:text, :string, :integer, :float, :decimal] = () if text_types.include?(column.column.type) text_field(:record, column.name, .merge(column.)) end end end end |
#active_scaffold_search_multi_select(column, options) ⇒ Object
Search input methods
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 68 def active_scaffold_search_multi_select(column, ) associated = .delete :value associated = [associated].compact unless associated.is_a? Array associated.collect!(&:to_i) if column.association = (column.association).collect {|r| [r.to_label, r.id]} else = column.[:options].collect do |text, value| active_scaffold_translated_option(column, text, value) end end return as_(:no_options) if .empty? active_scaffold_checkbox_list(column, , associated, ) end |
#active_scaffold_search_null(column, options) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 132 def active_scaffold_search_null(column, ) = [] << [as_(:_select_), nil] .concat ActiveScaffold::Finder::NullComparators.collect {|comp| [as_(comp), comp]} select_tag([:name], (, field_search_params[column.name]), :id => [:id]) end |
#active_scaffold_search_options(column) ⇒ Object
the standard active scaffold options used for class, name and scope
50 51 52 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 50 def (column) { :name => "search[#{column.name}]", :class => "#{column.name}-input", :id => "search_#{column.name}", :value => field_search_params[column.name] } end |
#active_scaffold_search_range(column, options) ⇒ Object Also known as: active_scaffold_search_integer, active_scaffold_search_decimal, active_scaffold_search_float, active_scaffold_search_string
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 179 def active_scaffold_search_range(column, ) opt_value, from_value, to_value = field_search_params_range_values(column) = (column) if active_scaffold_search_range_string?(column) text_field_size = 15 opt_value ||= '%?%' else text_field_size = 10 opt_value ||= '=' end from_value = controller.class.condition_value_for_numeric(column, from_value) to_value = controller.class.condition_value_for_numeric(column, to_value) from_value = format_number_value(from_value, column.) if from_value.is_a?(Numeric) to_value = format_number_value(to_value, column.) if to_value.is_a?(Numeric) html = select_tag("#{[:name]}[opt]", (, opt_value), :id => "#{[:id]}_opt", :class => "as_search_range_option") html << content_tag("span", :id => "#{[:id]}_numeric", :style => ActiveScaffold::Finder::NullComparators.include?(opt_value) ? "display: none" : nil) do text_field_tag("#{[:name]}[from]", from_value, (:id => [:id], :size => text_field_size)) << content_tag(:span, (' - ' + text_field_tag("#{[:name]}[to]", to_value, (:id => "#{[:id]}_to", :size => text_field_size))).html_safe, :id => "#{[:id]}_between", :class => "as_search_range_between", :style => (opt_value == 'BETWEEN') ? nil : "display: none") end content_tag :span, html, :class => 'search_range' end |
#active_scaffold_search_range_comparator_options(column) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 159 def (column) = ActiveScaffold::Finder::NumericComparators.collect {|comp| [as_(comp.downcase.to_sym), comp]} if active_scaffold_search_range_string?(column) .unshift *ActiveScaffold::Finder::StringComparators.collect {|title, comp| [as_(title), comp]} end if include_null_comparators? column += ActiveScaffold::Finder::NullComparators.collect {|comp| [as_(comp), comp]} end end |
#active_scaffold_search_range_string?(column) ⇒ Boolean
146 147 148 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 146 def active_scaffold_search_range_string?(column) (column.column && column.column.text?) || column.search_ui == :string end |
#active_scaffold_search_select(column, html_options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 85 def active_scaffold_search_select(column, ) associated = .delete :value if column.association associated = associated.is_a?(Array) ? associated.map(&:to_i) : associated.to_i unless associated.nil? method = column.association.macro == :belongs_to ? column.association.foreign_key : column.name = (column.association, false) else method = column.name = column.[:options].collect do |text, value| active_scaffold_translated_option(column, text, value) end end = { :selected => associated }.merge! column. .merge! column.[:html_options] || {} if [:multiple] [:name] += '[]' else [:include_blank] ||= as_(:_select_) end if optgroup = .delete(:optgroup) select(:record, method, (column, , optgroup), , ) elsif column.association collection_select(:record, method, , :id, :to_label, , ) else select(:record, method, , , ) end end |
#active_scaffold_search_text(column, options) ⇒ Object
115 116 117 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 115 def active_scaffold_search_text(column, ) text_field :record, column.name, () end |
#active_scaffold_search_time(column, options) ⇒ Object
228 229 230 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 228 def active_scaffold_search_time(column, ) active_scaffold_search_datetime(column, .merge!(:discard_date => true)) end |
#field_search_datetime_value(value) ⇒ Object
212 213 214 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 212 def field_search_datetime_value(value) DateTime.new(value[:year].to_i, value[:month].to_i, value[:day].to_i, value[:hour].to_i, value[:minute].to_i, value[:second].to_i) unless value.nil? || value[:year].blank? end |
#field_search_params_range_values(column) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 139 def field_search_params_range_values(column) values = field_search_params[column.name] return nil if values.nil? return values[:opt], (values[:from].blank? ? nil : values[:from]), (values[:to].blank? ? nil : values[:to]) end |
#include_null_comparators?(column) ⇒ Boolean
150 151 152 153 154 155 156 157 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 150 def include_null_comparators?(column) return column.[:null_comparators] if column..has_key? :null_comparators if column.association column.association.macro != :belongs_to || active_scaffold_config.columns[column.association.foreign_key].column.try(:null) else column.column.try(:null) end end |
#override_search(form_ui) ⇒ Object
the naming convention for overriding search input types with helpers
246 247 248 249 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 246 def override_search(form_ui) method = "active_scaffold_search_#{form_ui}" method if respond_to? method end |
#override_search_field(column) ⇒ Object
241 242 243 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 241 def override_search_field(column) override_helper column, 'search_column' end |
#search_attribute(column, record) ⇒ Object
54 55 56 57 58 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 54 def search_attribute(column, record) = (column).merge(:object => record) field = active_scaffold_search_for column, %|<dl><dt>#{label_tag search_label_for(column, ), search_column_label(column, record)}</dt><dd>#{field}</dd></dl>|.html_safe end |
#search_column_label(column, record) ⇒ Object
Search column override signatures
237 238 239 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 237 def search_column_label(column, record) column.label end |
#search_label_for(column, options) ⇒ Object
60 61 62 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 60 def search_label_for(column, ) [:id] unless [:range, :integer, :decimal, :float, :string, :date_picker, :datetime_picker, :calendar_date_select].include? column.search_ui end |
#searched_by?(column) ⇒ Boolean
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 265 def searched_by?(column) value = field_search_params[column.name] case value when Hash !value['from'].blank? when String !value.blank? else false end end |
#visibles_and_hiddens(search_config) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 251 def visibles_and_hiddens(search_config) visibles = [] hiddens = [] search_config.columns.each do |column| next unless column.search_sql if search_config.optional_columns.include?(column.name) && !searched_by?(column) hiddens << column else visibles << column end end return visibles, hiddens end |