Module: ActiveScaffold::Helpers::ListColumnHelpers

Included in:
ViewHelpers
Defined in:
lib/six-updater-web/vendor/plugins/active_scaffold/lib/bridges/file_column/lib/list_ui.rb,
lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb

Overview

Helpers that assist with the rendering of a List Column

Instance Method Summary collapse

Instance Method Details

setup the action link to inline form



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 85

def action_link_to_inline_form(column, associated)
  link = column.link.clone
  if column_empty?(associated) # if association is empty, we only can link to create form
    if column.actions_for_association_links.include?(:new)
      link.action = 'new'
      link.crud_type = :create
    end
  elsif column.actions_for_association_links.include?(:edit)
    link.action = 'edit'
    link.crud_type = :update
  elsif column.actions_for_association_links.include?(:show)
    link.action = 'show'
    link.crud_type = :read
  end
  link
end

#active_scaffold_column_checkbox(column, record) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 120

def active_scaffold_column_checkbox(column, record)
  column_value = record.send(column.name)
  checked = column_value.class.to_s.include?('Class') ? column_value : column_value == 1
  if column.inplace_edit and record.authorized_for?(:action => :update, :column => column.name)
    id_options = {:id => record.id.to_s, :action => 'update_column', :name => column.name.to_s}
    tag_options = {:tag => "span", :id => element_cell_id(id_options), :class => "in_place_editor_field"}
    script = remote_function(:method => 'POST', :url => {:controller => params_for[:controller], :action => "update_column", :column => column.name, :id => record.id.to_s, :value => !column_value, :eid => params[:eid]})
    (:span, check_box_tag(tag_options[:id], 1, checked, {:onclick => script}) , tag_options)
  else
    check_box_tag(nil, 1, checked, :disabled => true)
  end
end

HOWTO <% column = active_scaffold_config.columns %> <% doc = Document.find(:first, :conditions => [“name = ?”, doc_name]) %> <%= active_scaffold_column_download_link(column, doc) %>



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/bridges/file_column/lib/list_ui.rb', line 14

def active_scaffold_column_download_link(column, record, label = nil)
  return nil if record.send(column.name).nil?
  label ||= as_(:download)
  # AST Begin - :secure_download
  if column.options[:secure_download]
    url_options = active_scaffold_column_download_link_url_options(column, record)
  else
    url_options = url_for_file_column(record, column.name.to_s)
  end
  # AST End
  link_to( label, url_options, :popup => true)
end

AST - active_scaffold_column_download_link_url_options



28
29
30
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/bridges/file_column/lib/list_ui.rb', line 28

def active_scaffold_column_download_link_url_options(column, record)
  {:controller => active_scaffold_config.secure_download_controller, :action => "show", :id => record.id, :download => url_for_file_column(record, column.name.to_s)}#.encrypt!(:symmetric, :key => active_scaffold_config.secure_download_key)}
end


5
6
7
8
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/bridges/file_column/lib/list_ui.rb', line 5

def active_scaffold_column_download_link_with_filename(column, record)
  return nil if record.send(column.name).nil?
  active_scaffold_column_download_link(column, record, File.basename(record.send(column.name)))
end

#active_scaffold_column_percentage(column, record) ⇒ Object



357
358
359
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 357

def active_scaffold_column_percentage(column, record)
  number_to_percentage(record.send(column.name).to_s, :precision => 1)
end

#active_scaffold_column_phone_number(column, record) ⇒ Object



379
380
381
382
383
384
385
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 379

def active_scaffold_column_phone_number(column, record)
  if record.respond_to?("#{column.name}_for_human")
    record.send("#{column.name}_for_human")
  else
    usa_number_to_phone(record.send(column.name).to_s)
  end
end

#active_scaffold_column_ssn(column, record) ⇒ Object



361
362
363
364
365
366
367
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 361

def active_scaffold_column_ssn(column, record)
  if record.respond_to?("#{column.name}_for_human")
    record.send("#{column.name}_for_human")
  else
    usa_number_to_ssn(record.send(column.name).to_s)
  end
end

#active_scaffold_column_text(column, record) ⇒ Object

Overrides



116
117
118
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 116

def active_scaffold_column_text(column, record)
  truncate(clean_column_value(record.send(column.name)), :length => column.options[:truncate] || 50)
end

#active_scaffold_column_thumbnail(column, record) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/bridges/file_column/lib/list_ui.rb', line 32

def active_scaffold_column_thumbnail(column, record)
  return nil if record.send(column.name).nil?
  link_to( 
    image_tag(url_for_file_column(record, column.name.to_s, "thumb"), :border => 0), 
    url_for_file_column(record, column.name.to_s), 
    :popup => true)
end

#active_scaffold_column_usa_money(column, record) ⇒ Object



369
370
371
372
373
374
375
376
377
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 369

def active_scaffold_column_usa_money(column, record)
  value = record.send(column.name)
  formatted_value = number_to_currency(value.abs.to_s) if value
  if value and value < 0.0
    "(#{formatted_value})"
  else
    formatted_value
end
end

#active_scaffold_column_usa_zip_code(column, record) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 387

def active_scaffold_column_usa_zip_code(column, record)
  if record.respond_to?("#{column.name}_for_human")
    record.send("#{column.name}_for_human")
  else
    usa_number_to_zip(record.send(column.name).to_s)
  end
end

#active_scaffold_in_place_editor(field_id, options = {}) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 306

def active_scaffold_in_place_editor(field_id, options = {})
  function =  "new ActiveScaffold.InPlaceEditor("
  function << "'#{field_id}', "
  function << "'#{url_for(options[:url])}'"
    
  js_options = {}
    
  if protect_against_forgery?
    options[:with] ||= "Form.serialize(form)"
    options[:with] += " + '&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')"
  end
    
  js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text]
  js_options['okText'] = %('#{options[:save_text]}') if options[:save_text]
  js_options['okControl'] = %('#{options[:save_control_type]}') if options[:save_control_type]
  js_options['cancelControl'] = %('#{options[:cancel_control_type]}') if options[:cancel_control_type]
  js_options['loadingText'] = %('#{options[:loading_text]}') if options[:loading_text]
  js_options['savingText'] = %('#{options[:saving_text]}') if options[:saving_text]
  js_options['rows'] = options[:rows] if options[:rows]
  js_options['cols'] = options[:cols] if options[:cols]
  js_options['size'] = options[:size] if options[:size]
  js_options['externalControl'] = "'#{options[:external_control]}'" if options[:external_control]
  js_options['externalControlOnly'] = "true" if options[:external_control_only]
  js_options['submitOnBlur'] = "'#{options[:submit_on_blur]}'" if options[:submit_on_blur]
  js_options['loadTextURL'] = "'#{url_for(options[:load_text_url])}'" if options[:load_text_url]        
  js_options['ajaxOptions'] = options[:ajax_options] if options[:ajax_options]
  js_options['htmlResponse'] = !options[:script] if options[:script]
  js_options['callback']   = "function(form) { return #{options[:with]} }" if options[:with]
  js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text]
  js_options['textBetweenControls'] = %('#{options[:text_between_controls]}') if options[:text_between_controls]
  js_options['highlightcolor'] = %('#{options[:highlight_color]}') if options[:highlight_color]
  js_options['highlightendcolor'] = %('#{options[:highlight_end_color]}') if options[:highlight_end_color]
  js_options['onFailure'] = "function(element, transport) { #{options[:failure]} }" if options[:failure]
  js_options['onComplete'] = "function(transport, element) { #{options[:complete]} }" if options[:complete]
  js_options['onEnterEditMode'] = "function(element) { #{options[:enter_editing]} }" if options[:enter_editing]
  js_options['onLeaveEditMode'] = "function(element) { #{options[:exit_editing]} }" if options[:exit_editing]
  js_options['onFormCustomization'] = "function(element, form) { #{options[:form_customization]} }" if options[:form_customization]
  js_options['inplacePatternSelector'] = %('#{options[:inplace_pattern_selector]}') if options[:inplace_pattern_selector]
  js_options['nodeIdSuffix'] = %('#{options[:node_id_suffix]}') if options[:node_id_suffix]
  function << (', ' + options_for_javascript(js_options)) unless js_options.empty?
  
  function << ')'
    
  javascript_tag(function)
end

#active_scaffold_inplace_edit(record, column, options = {}) ⇒ Object

AST - options param



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 257

def active_scaffold_inplace_edit(record, column, options = {})
  formatted_column = options[:formatted_column] || format_inplace_edit_column(record, column)
# AST End
  id_options = {:id => record.id.to_s, :action => 'update_column', :name => column.name.to_s}
  tag_options = {:tag => "span", :id => element_cell_id(id_options), :class => "in_place_editor_field"}
  in_place_editor_options = {
    :url => {:controller => params_for[:controller], :action => "update_column", :column => column.name, :id => record.id.to_s},
    :with => params[:eid] ? "Form.serialize(form) + '&eid=#{params[:eid]}'" : nil,
    :click_to_edit_text => as_(:click_to_edit),
    :cancel_text => as_(:cancel),
    :loading_text => as_(:loading),
    :save_text => as_(:update),
    :saving_text => as_(:saving),
    :ajax_options => "{method: 'post'}",
    :script => true
  }

  if inplace_edit_cloning?(column)
    in_place_editor_options.merge!(
      :inplace_pattern_selector => "##{active_scaffold_column_header_id(column)} .#{inplace_edit_control_css_class}",
      :node_id_suffix => record.id.to_s,
      :form_customization => 'element.clonePatternField();'
    )
  elsif column.inplace_edit == :ajax
    url = url_for(:action => 'render_field', :id => record.id, :column => column.name, :update_column => column.name, :in_place_editing => true, :escape => false)
    in_place_editor_options[:form_customization] = "element.setFieldFromAjax('#{escape_javascript(url)}');"
  elsif column.column.try(:type) == :text
    in_place_editor_options[:rows] = column.options[:rows] || 5
  end

  in_place_editor_options.merge!(column.options)
  (:span, formatted_column, tag_options) + active_scaffold_in_place_editor(tag_options[:id], in_place_editor_options)
end

#cache_association(value, column) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 223

def cache_association(value, column)
  # we are not using eager loading, cache firsts records in order not to query the database in a future
  unless value.loaded?
    # load at least one record, is needed for column_empty? and checking permissions
    if column.associated_limit.nil?
      Rails.logger.warn "ActiveScaffold: Enable eager loading for #{column.name} association to reduce SQL queries"
    else
      value.target = value.find(:all, :limit => column.associated_limit + 1, :select => column.select_columns)
    end
  end
end

#clean_column_value(v) ⇒ Object

There are two basic ways to clean a column’s value: h() and sanitize(). The latter is useful when the column contains valid html data, and you want to just disable any scripting. People can always use field overrides to clean data one way or the other, but having this override lets people decide which way it should happen by default.

Why is it not a configuration option? Because it seems like a somewhat rare request. But it could eventually be an option in config.list (and config.show, I guess).



109
110
111
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 109

def clean_column_value(v)
  h(v)
end

#column_override(column) ⇒ Object



133
134
135
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 133

def column_override(column)
  "#{column.name.to_s.gsub('?', '')}_column" # parse out any question marks (see issue 227)
end

#column_override?(column) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 137

def column_override?(column)
  respond_to?(column_override(column))
end

#format_association_value(value, column, size) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 188

def format_association_value(value, column, size)
  case column.association.macro
    when :has_one, :belongs_to
      format_value(value.to_label)
    when :has_many, :has_and_belongs_to_many
      if column.associated_limit.nil?
        firsts = value.collect { |v| v.to_label }
      else
        firsts = value.first(column.associated_limit)
        firsts.collect! { |v| v.to_label }
        firsts[column.associated_limit] = '' if value.size > column.associated_limit
      end
      if column.associated_limit == 0
        size if column.associated_number?
      else
        joined_associated = format_value(firsts.join(', '))
        joined_associated << " (#{size})" if column.associated_number? and column.associated_limit and value.size > column.associated_limit
        joined_associated
      end
  end
end

#format_column_value(record, column) ⇒ Object

Formatting



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 154

def format_column_value(record, column)
  value = record.send(column.name)
  if value && column.association # cache association size before calling column_empty?
    # AST - Had problems with has_many through when the relationship was empty - AR is creating  WHERE ((`trade_days`.id = )). Tried value.empty? but that caused the same problem so we are using (value.length > 0)
    associated_size = value.size if column.plural_association? and (value.length > 0) and column.associated_number? # get count before cache association
    cache_association(value, column)
  end
  if column.association.nil? or column_empty?(value)
    if value.is_a? Numeric
      format_number_value(value, column.options)
    else
      format_value(value, column.options)
    end
  else
    format_association_value(value, column, associated_size)
  end
end

#format_inplace_edit_column(record, column) ⇒ Object



247
248
249
250
251
252
253
254
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 247

def format_inplace_edit_column(record,column)
  value = record.send(column.name)
  if column.list_ui == :checkbox
    active_scaffold_column_checkbox(column, record)
  else
    format_column_value(record, column)
  end
end

#format_number_value(value, options = {}) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 172

def format_number_value(value, options = {})
  value = case options[:format]
    when :size
      number_to_human_size(value, options[:i18n_options] || {})
    when :percentage
      number_to_percentage(value, options[:i18n_options] || {})
    when :currency
      number_to_currency(value, options[:i18n_options] || {})
    when :i18n_number
      send("number_with_#{value.is_a?(Integer) ? 'delimiter' : 'precision'}", value, options[:i18n_options] || {})
    else
      value
  end
  clean_column_value(value)
end

#format_value(column_value, options = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 210

def format_value(column_value, options = {})
  value = if column_empty?(column_value)
    active_scaffold_config.list.empty_field_text
  elsif column_value.is_a?(Time) || column_value.is_a?(Date)
    l(column_value, :format => options[:format] || :default)
  elsif [FalseClass, TrueClass].include?(column_value.class)
    as_(column_value.to_s.to_sym)
  else
    column_value.to_s
  end
  clean_column_value(value)
end

#get_column_value(record, column) ⇒ Object



6
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
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 6

def get_column_value(record, column)
  begin
  # check for an override helper
  value = if column_override? column
    # we only pass the record as the argument. we previously also passed the formatted_value,
    # but mike perham pointed out that prohibited the usage of overrides to improve on the
    # performance of our default formatting. see issue #138.
    send(column_override(column), record, column)
  # second, check if the dev has specified a valid list_ui for this column
  elsif column.list_ui and override_column_ui?(column.list_ui)
    send(override_column_ui(column.list_ui), column, record)

  elsif inplace_edit?(record, column)
    active_scaffold_inplace_edit(record, column)
  elsif column.column and override_column_ui?(column.column.type)
    send(override_column_ui(column.column.type), column, record)
  else
    format_column_value(record, column)
  end

  value = '&nbsp;' if value.nil? or (value.respond_to?(:empty?) and value.empty?) # fix for IE 6
  return value
  rescue Exception => e
    logger.error Time.now.to_s + "#{e.inspect} -- on the ActiveScaffold column = :#{column.name} in #{@controller.class}"
    raise e
  end
end

#inplace_edit?(record, column) ⇒ Boolean

Inline Edit =

Returns:

  • (Boolean)


239
240
241
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 239

def inplace_edit?(record, column)
  column.inplace_edit and record.authorized_for?(:action => :update, :column => column.name)
end

#inplace_edit_cloning?(column) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 243

def inplace_edit_cloning?(column)
   column.inplace_edit != :ajax and (override_form_field?(column) or column.form_ui or (column.column and override_input?(column.column.type)))
end

#inplace_edit_control(column) ⇒ Object



291
292
293
294
295
296
297
298
299
300
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 291

def inplace_edit_control(column)
  if inplace_edit?(active_scaffold_config.model, column) and inplace_edit_cloning?(column)
    @record = active_scaffold_config.model.new
    column = column.clone
    column.options = column.options.clone
    column.options.delete(:update_column)
    column.form_ui = :select if (column.association && column.form_ui.nil?)
    (:div, active_scaffold_input_for(column), {:style => "display:none;", :class => inplace_edit_control_css_class})
  end
end

#inplace_edit_control_css_classObject



302
303
304
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 302

def inplace_edit_control_css_class
  "as_inplace_pattern"
end

#nested_label(association) ⇒ Object

AST Begin



353
354
355
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 353

def nested_label(association)
  as_(:nested_for_model, :nested_model => active_scaffold_config_for(association.klass).label(:count => 2), :parent_model => format_value(@record.to_label))
end

#override_column_ui(list_ui) ⇒ Object

the naming convention for overriding column types with helpers



146
147
148
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 146

def override_column_ui(list_ui)
  "active_scaffold_column_#{list_ui}"
end

#override_column_ui?(list_ui) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 141

def override_column_ui?(list_ui)
  respond_to?(override_column_ui(list_ui))
end

#render_list_column(text, column, record) ⇒ Object

TODO: move empty_field_text and &nbsp; logic in here? TODO: move active_scaffold_inplace_edit in here? TODO: we need to distinguish between the automatic links we create and the ones that the dev specified. some logic may not apply if the dev specified the link.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/list_column_helpers.rb', line 37

def render_list_column(text, column, record)
  # AST Begin
  make_available_method = "#{column.name}_make_available?"
  return active_scaffold_config.list.empty_field_text if record.respond_to?(make_available_method) and !record.send(make_available_method)
  # AST End
  if column.link
    link = column.link
    associated = record.send(column.association.name) if column.association
    url_options = params_for(:action => nil, :id => record.id, :link => text)
    url_options[:parent_controller] = params[:controller] if link.controller and link.controller.to_s != params[:controller]
    url_options[:id] = associated.id if associated and link.controller and link.controller.to_s != params[:controller]

    # setup automatic link
    if column.autolink? # link to nested scaffold or inline form
      link = action_link_to_inline_form(column, associated) if link.crud_type.nil? # automatic link to inline form (singular association)
      return text if link.crud_type.nil?
      if link.crud_type == :create
        url_options[:link] = as_(:create_new)
        url_options[:parent_id] = record.id
        url_options[:parent_column] = column.association.reverse
        url_options[:parent_model] = record.class.name # needed for polymorphic associations
        url_options.delete :id
      end
    end

    # check authorization
    if column.association
      associated_for_authorized = if associated.nil? || (associated.respond_to?(:empty?) && associated.empty?)
        column.association.klass
      elsif column.plural_association?
        associated.first
      else
        associated
      end
      authorized = associated_for_authorized.authorized_for?(:action => link.crud_type)
      authorized = authorized and record.authorized_for?(:action => :update, :column => column.name) if link.crud_type == :create
    else
      authorized = record.authorized_for?(:action => link.crud_type)
    end
    return "<a class='disabled'>#{text}</a>" unless authorized

    render_action_link(link, url_options)
  else
    text
  end
end