Module: ActiveScaffold::Helpers::SearchColumnHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/search_column_helpers.rb,
lib/active_scaffold/helpers/country_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_country(column, options) ⇒ Object
- #active_scaffold_search_date(column, options) ⇒ Object
- #active_scaffold_search_datetime(column, options) ⇒ Object (also: #active_scaffold_search_timestamp)
-
#active_scaffold_search_for(column) ⇒ 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_record_select(column, options) ⇒ Object
- #active_scaffold_search_select(column, html_options) ⇒ Object
- #active_scaffold_search_text(column, options) ⇒ Object
- #active_scaffold_search_time(column, options) ⇒ Object
- #active_scaffold_search_usa_state(column, options) ⇒ Object
- #field_search_datetime_value(value) ⇒ Object
- #field_search_params_range_values(column) ⇒ Object
- #field_search_record_select_value(column) ⇒ Object
-
#override_search(form_ui) ⇒ Object
the naming convention for overriding search input types with helpers.
- #override_search?(search_ui) ⇒ Boolean
-
#override_search_field(column) ⇒ Object
the naming convention for overriding form fields with helpers.
-
#override_search_field?(column) ⇒ Boolean
Search column override signatures.
- #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
100 101 102 103 104 105 106 107 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 100 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]))) end |
#active_scaffold_search_country(column, options) ⇒ Object
343 344 345 |
# File 'lib/active_scaffold/helpers/country_helpers.rb', line 343 def active_scaffold_search_country(column, ) active_scaffold_input_country(column, .merge!(:selected => .delete(:value))) end |
#active_scaffold_search_date(column, options) ⇒ Object
192 193 194 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 192 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
183 184 185 186 187 188 189 190 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 183 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) ⇒ 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) = (column) # first, check if the dev has created an override for this specific field for search if override_search_field?(column) send(override_search_field(column), @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 override_search?(column.search_ui) send(override_search(column.search_ui), 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 override_input?(column.search_ui) send(override_input(column.search_ui), column, ) # fourth, check if the dev has created an override for this specific field elsif override_form_field?(column) send(override_form_field(column), @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 override_search?(column.column.type) send(override_search(column.column.type), column, ) # if we (or someone else) have created a custom render option for the column type, use that elsif override_input?(column.column.type) send(override_input(column.column.type), 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 58 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, false) else = Array(column.[:options]) end return as_(:no_options) if .empty? active_scaffold_checkbox_list(column, , associated, ) end |
#active_scaffold_search_null(column, options) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 111 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])) 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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 133 def active_scaffold_search_range(column, ) opt_value, from_value, to_value = field_search_params_range_values(column) = (column) text_field_size = ((column.column && column.column.text?) ? 15 : 10) 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 << ' ' << text_field_tag("#{[:name]}[from]", from_value, (:id => [:id], :size => text_field_size)) html << ' ' << 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 => "display:#{(opt_value == 'BETWEEN') ? '' : 'none'}") html end |
#active_scaffold_search_range_comparator_options(column) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 125 def (column) = ActiveScaffold::Finder::NumericComparators.collect {|comp| [as_(comp.downcase.to_sym), comp]} if column.column && column.column.text? .unshift *ActiveScaffold::Finder::StringComparators.collect {|title, comp| [as_(title), comp]} end end |
#active_scaffold_search_record_select(column, options) ⇒ Object
158 159 160 161 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 158 def active_scaffold_search_record_select(column, ) value = field_search_record_select_value(column) active_scaffold_record_select(column, , value, column.[:multiple]) end |
#active_scaffold_search_select(column, html_options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 73 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.primary_key_name : column.name = (column.association, true) else method = column.name = Array(column.[:options]) end = { :selected => associated }.merge! column. .merge! column.[:html_options] || {} if [:multiple] [:name] += '[]' else [:include_blank] ||= as_(:_select_) end select(:record, method, , , ) end |
#active_scaffold_search_text(column, options) ⇒ Object
94 95 96 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 94 def active_scaffold_search_text(column, ) text_field :record, column.name, () end |
#active_scaffold_search_time(column, options) ⇒ Object
195 196 197 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 195 def active_scaffold_search_time(column, ) active_scaffold_search_datetime(column, .merge!(:discard_date => true)) end |
#active_scaffold_search_usa_state(column, options) ⇒ Object
347 348 349 |
# File 'lib/active_scaffold/helpers/country_helpers.rb', line 347 def active_scaffold_search_usa_state(column, ) active_scaffold_input_usa_state(column, .merge!(:selected => .delete(:value))) end |
#field_search_datetime_value(value) ⇒ Object
179 180 181 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 179 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
118 119 120 121 122 123 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 118 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 |
#field_search_record_select_value(column) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 163 def field_search_record_select_value(column) begin value = field_search_params[column.name] unless value.blank? if column.[:multiple] column.association.klass.find value.collect!(&:to_i) else column.association.klass.find(value.to_i) end end rescue Exception => e logger.error Time.now.to_s + "Sorry, we are not that smart yet. Attempted to restore search values to search fields but instead got -- #{e.inspect} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}" raise e end end |
#override_search(form_ui) ⇒ Object
the naming convention for overriding search input types with helpers
218 219 220 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 218 def override_search(form_ui) "active_scaffold_search_#{form_ui}" end |
#override_search?(search_ui) ⇒ Boolean
213 214 215 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 213 def override_search?(search_ui) respond_to?(override_search(search_ui)) end |
#override_search_field(column) ⇒ Object
the naming convention for overriding form fields with helpers
209 210 211 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 209 def override_search_field(column) "#{column.name}_search_column" end |
#override_search_field?(column) ⇒ Boolean
Search column override signatures
204 205 206 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 204 def override_search_field?(column) respond_to?(override_search_field(column)) end |
#searched_by?(column) ⇒ Boolean
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 236 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
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 222 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 |