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)

Instance Method Details

- (Object) active_scaffold_search_boolean(column, options) 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



131
132
133
134
135
136
137
138
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 131

def active_scaffold_search_boolean(column, options)
  select_options = []
  select_options << [as_(:_select_), nil]
  select_options << [as_(:true), true]
  select_options << [as_(:false), false]

  select_tag(options[:name], options_for_select(select_options, column.column.type_cast(options[:value])), :id => options[:id])
end

- (Object) active_scaffold_search_date(column, options)



235
236
237
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 235

def active_scaffold_search_date(column, options)
  active_scaffold_search_datetime(column, options.merge!(:discard_time => true))
end

- (Object) active_scaffold_search_datetime(column, options) Also known as: active_scaffold_search_timestamp



226
227
228
229
230
231
232
233
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 226

def active_scaffold_search_datetime(column, options)
  opt_value, from_value, to_value = field_search_params_range_values(column)
  options = column.options.merge(options)
  helper = "select_#{'date' unless options[:discard_date]}#{'time' unless options[:discard_time]}"
  
  send(helper, field_search_datetime_value(from_value), {:include_blank => true, :prefix => "#{options[:name]}[from]"}.merge(options)) << 
  ' - '.html_safe << send(helper, field_search_datetime_value(to_value), {:include_blank => true, :prefix => "#{options[:name]}[to]"}.merge(options))
end

- (Object) active_scaffold_search_for(column, options = nil)

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
48
49
50
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 7

def active_scaffold_search_for(column, options = nil)
  options ||= active_scaffold_search_options(column)
  record = options[:object]
  ActiveSupport::Deprecation.warn "Relying on @record is deprecated, include :object in options with record.", caller if record.nil? # TODO Remove when relying on @record is removed
  record ||= @record # TODO Remove when relying on @record is removed

  # first, check if the dev has created an override for this specific field for search
  if (method = override_search_field(column))
    send(method, record, options)

  # 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, options)

  # 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, options)

  # fourth, check if the dev has created an override for this specific field
  elsif (method = override_form_field(column))
    send(method, record, options)

  # fallback: we get to make the decision
  else
    if column.association or column.virtual?
      active_scaffold_search_text(column, options)

    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, options)
      # 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, options)
      # final ultimate fallback: use rails' generic input method
      else
        # for textual fields we pass different options
        text_types = [:text, :string, :integer, :float, :decimal]
        options = active_scaffold_input_text_options(options) if text_types.include?(column.column.type)
        text_field(:record, column.name, options.merge(column.options))
      end
    end
  end
end

- (Object) active_scaffold_search_multi_select(column, options)

Search input methods



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 71

def active_scaffold_search_multi_select(column, options)
  record = options.delete(:object)
  ActiveSupport::Deprecation.warn "Relying on @record is deprecated, include :object in options with record.", caller if record.nil? # TODO Remove when relying on @record is removed
  record ||= @record # TODO Remove when relying on @record is removed
  associated = options.delete :value
  associated = [associated].compact unless associated.is_a? Array
  associated.collect!(&:to_i)
  
  if column.association
    method = column.options[:label_method] || :to_label
    select_options = sorted_association_options_find(column.association, nil, record).collect {|r| [r.send(method), r.id]}
  else
    select_options = column.options[:options].collect do |text, value|
      active_scaffold_translated_option(column, text, value)
    end
  end
  return as_(:no_options) if select_options.empty?

  active_scaffold_checkbox_list(column, select_options, associated, options)
end

- (Object) active_scaffold_search_null(column, options)



142
143
144
145
146
147
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 142

def active_scaffold_search_null(column, options)
  select_options = []
  select_options << [as_(:_select_), nil]
  select_options.concat ActiveScaffold::Finder::NullComparators.collect {|comp| [as_(comp), comp]}
  select_tag(options[:name], options_for_select(select_options, options[:value]), :id => options[:id])
end

- (Object) active_scaffold_search_options(column)

the standard active scaffold options used for class, name and scope



53
54
55
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 53

def active_scaffold_search_options(column)
  { :name => "search[#{column.name}]", :class => "#{column.name}-input", :id => "search_#{column.name}", :value => field_search_params[column.name] }
end

- (Object) active_scaffold_search_range(column, options) Also known as: active_scaffold_search_integer, active_scaffold_search_decimal, active_scaffold_search_float, active_scaffold_search_string



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 189

def active_scaffold_search_range(column, options)
  opt_value, from_value, to_value = field_search_params_range_values(column)

  select_options = active_scaffold_search_range_comparator_options(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.options) if from_value.is_a?(Numeric)
  to_value = format_number_value(to_value, column.options) if to_value.is_a?(Numeric)
  html = select_tag("#{options[:name]}[opt]",
        options_for_select(select_options, opt_value),
        :id => "#{options[:id]}_opt",
        :class => "as_search_range_option")
  html << ("span", :id => "#{options[:id]}_numeric", :style => ActiveScaffold::Finder::NullComparators.include?(opt_value) ? "display: none" : nil) do
    text_field_tag("#{options[:name]}[from]", from_value, active_scaffold_input_text_options(:id => options[:id], :size => text_field_size)) <<
    (:span, (' - ' + text_field_tag("#{options[:name]}[to]", to_value,
        active_scaffold_input_text_options(:id => "#{options[:id]}_to", :size => text_field_size))).html_safe,
        :id => "#{options[:id]}_between", :class => "as_search_range_between", :style => (opt_value == 'BETWEEN') ? nil : "display: none")
  end
   :span, html, :class => 'search_range'
end

- (Object) active_scaffold_search_range_comparator_options(column)



169
170
171
172
173
174
175
176
177
178
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 169

def active_scaffold_search_range_comparator_options(column)
  select_options = ActiveScaffold::Finder::NumericComparators.collect {|comp| [as_(comp.downcase.to_sym), comp]}
  if active_scaffold_search_range_string?(column)
    select_options.unshift *ActiveScaffold::Finder::StringComparators.collect {|title, comp| [as_(title), comp]}
  end
  if include_null_comparators? column
    select_options += ActiveScaffold::Finder::NullComparators.collect {|comp| [as_(comp), comp]}
  end
  select_options
end

- (Boolean) active_scaffold_search_range_string?(column)

Returns:

  • (Boolean)


156
157
158
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 156

def active_scaffold_search_range_string?(column)
  (column.column && column.column.text?) || column.search_ui == :string
end

- (Object) active_scaffold_search_select(column, html_options, options = {})



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 92

def active_scaffold_search_select(column, html_options, options = {})
  record = html_options.delete(:object)
  ActiveSupport::Deprecation.warn "Relying on @record is deprecated, include :object in html_options with record.", caller if record.nil? # TODO Remove when relying on @record is removed
  record ||= @record # TODO Remove when relying on @record is removed
  associated = html_options.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
    select_options = sorted_association_options_find(column.association, false, record)
  else
    method = column.name
    select_options = column.options[:options].collect do |text, value|
      active_scaffold_translated_option(column, text, value)
    end
  end

  options = options.merge(:selected => associated).merge column.options
  html_options.merge! column.options[:html_options] || {}
  if html_options[:multiple]
    html_options[:name] += '[]'
  else
    options[:include_blank] ||= as_(:_select_) 
  end
  
  if optgroup = options.delete(:optgroup)
    select(:record, method, active_scaffold_grouped_options(column, select_options, optgroup), options, html_options)
  elsif column.association
    collection_select(:record, method, select_options, :id, column.options[:label_method] || :to_label, options, html_options)
  else
    select(:record, method, select_options, options, html_options)
  end
end

- (Object) active_scaffold_search_text(column, options)



125
126
127
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 125

def active_scaffold_search_text(column, options)
  text_field :record, column.name, active_scaffold_input_text_options(options)
end

- (Object) active_scaffold_search_time(column, options)



238
239
240
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 238

def active_scaffold_search_time(column, options)
  active_scaffold_search_datetime(column, options.merge!(:discard_date => true))
end

- (Object) field_search_datetime_value(value)



222
223
224
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 222

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

- (Object) field_search_params_range_values(column)



149
150
151
152
153
154
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 149

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

- (Boolean) include_null_comparators?(column)

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 160

def include_null_comparators?(column)
  return column.options[:null_comparators] if column.options.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

- (Object) override_search(form_ui)

the naming convention for overriding search input types with helpers



256
257
258
259
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 256

def override_search(form_ui)
  method = "active_scaffold_search_#{form_ui}"
  method if respond_to? method
end

- (Object) override_search_field(column)



251
252
253
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 251

def override_search_field(column)
  override_helper column, 'search_column'
end

- (Object) search_attribute(column, record)



57
58
59
60
61
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 57

def search_attribute(column, record)
  column_options = active_scaffold_search_options(column).merge(:object => record)
  field = active_scaffold_search_for column, column_options
  %|<dl><dt>#{label_tag search_label_for(column, column_options), search_column_label(column, record)}</dt><dd>#{field}</dd></dl>|.html_safe
end

- (Object) search_column_label(column, record)

Search column override signatures



247
248
249
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 247

def search_column_label(column, record)
  column.label
end

- (Object) search_label_for(column, options)



63
64
65
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 63

def search_label_for(column, options)
  options[:id] unless [:range, :integer, :decimal, :float, :string, :date_picker, :datetime_picker, :calendar_date_select].include? column.search_ui
end

- (Boolean) searched_by?(column)

Returns:

  • (Boolean)


275
276
277
278
279
280
281
282
283
284
285
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 275

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

- (Object) visibles_and_hiddens(search_config)



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/active_scaffold/helpers/search_column_helpers.rb', line 261

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