Module: ActiveScaffold::Helpers::ListColumnHelpers

Included in:
ViewHelpers
Defined in:
lib/active_scaffold/bridges/file_column/list_ui.rb,
lib/active_scaffold/bridges/dragonfly/list_ui.rb,
lib/active_scaffold/bridges/paperclip/list_ui.rb,
lib/active_scaffold/bridges/carrierwave/list_ui.rb,
lib/active_scaffold/helpers/list_column_helpers.rb,
lib/active_scaffold/bridges/active_storage/list_ui.rb

Overview

Helpers that assist with the rendering of a List Column

Constant Summary collapse

FORM_UI_WITH_OPTIONS =
%i[select radio].freeze
INPLACE_EDIT_PLURAL_FORM_UI =
%i[select record_select].freeze

Instance Method Summary collapse

Instance Method Details

#active_scaffold_column_active_storage_has_many(record, column, ui_options: column.options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_scaffold/bridges/active_storage/list_ui.rb', line 11

def active_scaffold_column_active_storage_has_many(record, column, ui_options: column.options)
  active_storage_files = record.send(column.name.to_s)
  return nil unless active_storage_files.attached?

  attachments = active_storage_files.attachments
  if attachments.size <= 3 # Lets display up to three links, otherwise just show the count.
    links = attachments.map { |attachment| link_for_attachment(attachment, column, ui_options: ui_options) }
    safe_join links, association_join_text(column)
  else
    pluralize attachments.size, column.name.to_s
  end
end

#active_scaffold_column_active_storage_has_one(record, column, ui_options: column.options) ⇒ Object



6
7
8
9
# File 'lib/active_scaffold/bridges/active_storage/list_ui.rb', line 6

def active_scaffold_column_active_storage_has_one(record, column, ui_options: column.options)
  attachment = record.send(column.name.to_s)
  attachment.attached? ? link_for_attachment(attachment, column, ui_options: ui_options) : nil
end

#active_scaffold_column_boolean(record, column, ui_options: column.options) ⇒ Object



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

def active_scaffold_column_boolean(record, column, ui_options: column.options)
  value = record.send(column.name)
  if value.nil? && ui_options[:include_blank]
    value = ui_options[:include_blank]
    value.is_a?(Symbol) ? as_(value) : value
  else
    format_column_value(record, column, value)
  end
end

#active_scaffold_column_carrierwave(record, column, ui_options: column.options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_scaffold/bridges/carrierwave/list_ui.rb', line 6

def active_scaffold_column_carrierwave(record, column, ui_options: column.options)
  carrierwave = record.send(column.name.to_s)
  return nil if carrierwave.file.blank?

  thumbnail_style = ActiveScaffold::Bridges::Carrierwave::CarrierwaveBridgeHelpers.thumbnail_style
  content =
    if carrierwave.versions.key?(thumbnail_style)
      image_tag(carrierwave.url(thumbnail_style), border: 0)
    else
      record.send(record.send(:_mounter, column.name).send(:serialization_column))
    end
  link_to(content, carrierwave.url, target: '_blank', rel: 'noopener noreferrer')
end

#active_scaffold_column_checkbox(record, column, ui_options: column.options) ⇒ Object



103
104
105
106
107
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 103

def active_scaffold_column_checkbox(record, column, ui_options: column.options)
  options = {disabled: true, id: nil, object: record}
  options.delete(:disabled) if inplace_edit?(record, column)
  check_box(:record, column.name, options)
end

#active_scaffold_column_checkboxes(record, column, ui_options: column.options) ⇒ Object Also known as: active_scaffold_column_select_multiple, active_scaffold_column_draggable



109
110
111
112
113
114
115
116
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 109

def active_scaffold_column_checkboxes(record, column, ui_options: column.options)
  return format_column_value(record, column) if column.association

  values = record.send(column.name)
  return if values.empty?

   :ul, safe_join(values.map { |v| (:li, convert_value_to_label(column, v)) })
end


13
14
15
16
17
18
# File 'lib/active_scaffold/bridges/file_column/list_ui.rb', line 13

def active_scaffold_column_download_link(record, column, label = nil, ui_options: column.options)
  return nil if record.send(column.name).nil?

  label ||= as_(:download)
  link_to(label, url_for_file_column(record, column.name.to_s), popup: true)
end


7
8
9
10
11
# File 'lib/active_scaffold/bridges/file_column/list_ui.rb', line 7

def active_scaffold_column_download_link_with_filename(record, column, ui_options: column.options)
  return nil if record.send(column.name).nil?

  active_scaffold_column_download_link(record, column, File.basename(record.send(column.name)))
end

#active_scaffold_column_dragonfly(record, column, ui_options: column.options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_scaffold/bridges/dragonfly/list_ui.rb', line 6

def active_scaffold_column_dragonfly(record, column, ui_options: column.options)
  attachment = record.send(column.name.to_s)
  return nil if attachment.blank?

  content =
    if attachment.image?
      image_tag(attachment.thumb(ui_options[:thumb] || ActiveScaffold::Bridges::Dragonfly::DragonflyBridgeHelpers.thumbnail_style).url, border: 0)
    else
      attachment.name
    end
  link_to(content, dragonfly_url_for_attachment(attachment, record, column, ui_options: ui_options), target: '_blank', rel: 'noopener noreferrer')
end

#active_scaffold_column_fulltext(record, column, ui_options: column.options) ⇒ Object



94
95
96
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 94

def active_scaffold_column_fulltext(record, column, ui_options: column.options)
  clean_column_value(record.send(column.name))
end

#active_scaffold_column_marked(record, column, ui_options: column.options) ⇒ Object



98
99
100
101
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 98

def active_scaffold_column_marked(record, column, ui_options: column.options)
  options = {id: nil, object: record}
  (:span, check_box(:record, column.name, options), class: 'in_place_editor_field', data: {ie_id: record.to_param})
end

#active_scaffold_column_month(record, column, ui_options: column.options) ⇒ Object



148
149
150
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 148

def active_scaffold_column_month(record, column, ui_options: column.options)
  l record.send(column.name), format: :year_month
end

#active_scaffold_column_paperclip(record, column, ui_options: column.options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_scaffold/bridges/paperclip/list_ui.rb', line 6

def active_scaffold_column_paperclip(record, column, ui_options: column.options)
  paperclip = record.send(column.name.to_s)
  return nil unless paperclip.file?

  content =
    if paperclip.styles.include?(ActiveScaffold::Bridges::Paperclip::PaperclipBridgeHelpers.thumbnail_style)
      image_tag(paperclip.url(ActiveScaffold::Bridges::Paperclip::PaperclipBridgeHelpers.thumbnail_style), border: 0, alt: nil)
    else
      paperclip.original_filename
    end
  link_to(content, paperclip.url, target: '_blank', rel: 'noopener')
end

#active_scaffold_column_percentage(record, column, ui_options: column.options) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 140

def active_scaffold_column_percentage(record, column, ui_options: column.options)
  options = ui_options[:slider] || {}
  options = options.merge(min: record.send(options[:min_method])) if options[:min_method]
  options = options.merge(max: record.send(options[:max_method])) if options[:max_method]
  value = record.send(options[:value_method]) if options[:value_method]
  as_slider options.merge(value: value || record.send(column.name))
end

#active_scaffold_column_select(record, column, ui_options: column.options) ⇒ Object Also known as: active_scaffold_column_radio



120
121
122
123
124
125
126
127
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 120

def active_scaffold_column_select(record, column, ui_options: column.options)
  unless column.association
    return active_scaffold_column_select_multiple(record, column, ui_options: ui_options) if ui_options[:multiple]

    value = convert_value_to_label(column, record.send(column.name))
  end
  format_column_value(record, column, value)
end

#active_scaffold_column_telephone(record, column, ui_options: column.options) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 163

def active_scaffold_column_telephone(record, column, ui_options: column.options)
  phone = record.send column.name
  return if phone.blank?

  phone = number_to_phone(phone) unless ui_options[:format] == false
  tel_to phone
end

#active_scaffold_column_text(record, column, ui_options: column.options) ⇒ Object

Overrides



89
90
91
92
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 89

def active_scaffold_column_text(record, column, ui_options: column.options)
  # `to_s` is necessary to convert objects in serialized columns to string before truncation.
  clean_column_value(truncate(record.send(column.name).to_s, length: ui_options[:truncate] || 50))
end

#active_scaffold_column_thumbnail(record, column, ui_options: column.options) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/active_scaffold/bridges/file_column/list_ui.rb', line 20

def active_scaffold_column_thumbnail(record, column, ui_options: column.options)
  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_week(record, column, ui_options: column.options) ⇒ Object



152
153
154
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 152

def active_scaffold_column_week(record, column, ui_options: column.options)
  l record.send(column.name), format: :week
end

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



385
386
387
388
389
390
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 385

def active_scaffold_inplace_edit(record, column, options = {})
  formatted_column = options[:formatted_column] || format_column_value(record, column)
  @_inplace_edit_handle ||= (:span, as_(:inplace_edit_handle), class: 'handle')
  span = (:span, formatted_column, active_scaffold_inplace_edit_tag_options(record, column))
  @_inplace_edit_handle + span
end

#active_scaffold_inplace_edit_tag_options(record, column) ⇒ Object



374
375
376
377
378
379
380
381
382
383
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 374

def active_scaffold_inplace_edit_tag_options(record, column)
  @_inplace_edit_title ||= as_(:click_to_edit)
  cell_id = ActiveScaffold::Registry.cache :inplace_edit_id, column.cache_key do
    element_cell_id(id: '--ID--', action: 'update_column', name: column.name.to_s)
  end
  tag_options = {id: cell_id.sub('--ID--', record.id.to_s), class: 'in_place_editor_field',
                 title: @_inplace_edit_title, data: {ie_id: record.to_param}}
  tag_options[:data][:ie_update] = column.inplace_edit if column.inplace_edit != true
  tag_options
end

#all_marked?Boolean

MARK

Returns:

  • (Boolean)


436
437
438
439
440
441
442
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 436

def all_marked?
  if active_scaffold_config.mark.mark_all_mode == :page
    @page.items.all? { |record| marked_records.key?(record.id) }
  else
    marked_records.length >= @page.pager.count.to_i
  end
end

#association_join_text(column = nil) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 279

def association_join_text(column = nil)
  column_value = column&.association_join_text
  return column_value if column_value
  return @_association_join_text if defined? @_association_join_text

  @_association_join_text = active_scaffold_config.list.association_join_text
end

#cache_association(association, column, size) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 338

def cache_association(association, column, size)
  associated_limit = column.associated_limit
  # we are not using eager loading, cache firsts records in order not to query the database for whole association in a future
  if associated_limit.nil?
    logger.warn "ActiveScaffold: Enable eager loading for #{column.name} association to reduce SQL queries"
  elsif associated_limit.positive?
    # load at least one record more, is needed to display '...'
    association.target = association.reader.limit(associated_limit + 1).select(column.select_associated_columns || "#{association.klass.quoted_table_name}.*").to_a
  elsif @cache_associations
    # set array with at least one element if size > 0, so blank? or present? works, saving [nil] may cause exceptions
    association.target =
      if size.to_i.zero?
        []
      else
        ActiveScaffold::Registry.cache(:cached_empty_association, association.klass) { [association.klass.new] }
      end
  end
end

#clean_column_value(value) ⇒ 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).



82
83
84
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 82

def clean_column_value(value)
  h(value)
end

#column_association_size(record, column, value) ⇒ Object



231
232
233
234
235
236
237
238
239
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 231

def column_association_size(record, column, value)
  cached_counts = @counts&.dig(column.name)
  if cached_counts
    key = column.association.primary_key if count_on_association_class?(column)
    cached_counts[record.send(key || :id)] || 0
  else
    value.size
  end
end

#column_calculation(column, id_condition: true) ⇒ Object

CALCULATIONS



497
498
499
500
501
502
503
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 497

def column_calculation(column, id_condition: true)
  if column.calculate.instance_of? Proc
    column.calculate.call(@records)
  elsif column.calculate.in? %i[count sum average minimum maximum]
    calculate_query(id_condition: id_condition).calculate(column.calculate, column.grouped_select)
  end
end

#column_heading_attributes(column, sorting, sort_direction) ⇒ Object

COLUMN HEADINGS



454
455
456
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 454

def column_heading_attributes(column, sorting, sort_direction)
  {id: active_scaffold_column_header_id(column), class: column_heading_class(column, sorting), title: strip_tags(column.description).presence}
end

#column_heading_label(column) ⇒ Object



491
492
493
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 491

def column_heading_label(column)
  column.label
end

#column_heading_value(column, sorting, sort_direction) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 471

def column_heading_value(column, sorting, sort_direction)
  if column.name == :as_marked
    mark_column_heading
  elsif column.sortable?
    options = {id: nil, class: 'as_sort',
               'data-page-history' => controller_id,
               remote: true, method: :get}
    url_options = {action: :index, page: 1, sort: column.name, sort_direction: sort_direction}
    # :id needed because rails reuse it even if it was deleted from params (like do_refresh_list does)
    url_options[:id] = nil if @remove_id_from_list_links
    url_options = params_for(url_options)
    if !active_scaffold_config. && respond_to?(:search_params) && search_params.present?
      url_options[:search] = search_params
    end
    link_to column_heading_label(column), url_options, options
  else
    (:p, column_heading_label(column))
  end
end

#column_override(column) ⇒ Object Also known as: column_override?



171
172
173
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 171

def column_override(column)
  override_helper column, 'column'
end

#column_wrap_tagObject



69
70
71
72
73
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 69

def column_wrap_tag
  return @_column_wrap_tag if defined? @_column_wrap_tag

  @_column_wrap_tag = (active_scaffold_config.list.wrap_tag if active_scaffold_config.actions.include?(:list))
end

#convert_value_to_label(column, value, options = nil) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 201

def convert_value_to_label(column, value, options = nil)
  options ||= (column.form_ui_options || column.options)&.dig(:options)
  return value if options.blank?

  text, val = options.find { |t, v| (v.nil? ? t : v).to_s == value.to_s }
  text ? active_scaffold_translated_option(column, text, val).first : value
end

#dragonfly_url_for_attachment(attachment, record, column, ui_options: column.options) ⇒ Object



19
20
21
22
# File 'lib/active_scaffold/bridges/dragonfly/list_ui.rb', line 19

def dragonfly_url_for_attachment(attachment, record, column, ui_options: column.options)
  url_method = ui_options[:private_store] ? :url : :remote_url
  attachment.send(url_method)
end

#format_association_value(value, column, size) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 312

def format_association_value(value, column, size)
  method = (column.list_ui_options || column.options)[:label_method] || :to_label
  value =
    if column.association.collection?
      format_collection_association_value(value, column, method, size)
    elsif value
      format_singular_association_value(value, column, method)
    end
  format_value value, nil, column
end

#format_collection_association_value(value, column, label_method, size) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 287

def format_collection_association_value(value, column, label_method, size)
  associated_limit = column.associated_limit
  if associated_limit.nil?
    firsts = value.collect(&label_method)
    safe_join firsts, association_join_text(column)
  elsif associated_limit.zero?
    size if column.associated_number?
  else
    firsts = value.loaded? ? value[0, associated_limit] : value.limit(associated_limit)
    firsts = firsts.map(&label_method)
    firsts << '' if value.size > associated_limit
    text = safe_join firsts, association_join_text(column)
    text << " (#{size})" if column.associated_number? && associated_limit && value.size > associated_limit
    text
  end
end

#format_column_calculation(column, calculation) ⇒ Object



512
513
514
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 512

def format_column_calculation(column, calculation)
  "#{"#{as_(column.calculate)}: " unless column.calculate.is_a? Proc}#{format_column_value nil, column, calculation}"
end

#format_column_value(record, column, value = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 210

def format_column_value(record, column, value = nil)
  value ||= read_value_from_record(record, column) unless record.nil?
  if grouped_search? && column == search_group_column && (search_group_function || search_group_column.group_by)
    format_grouped_search_column(value, column.options)
  elsif column.association.nil?
    if value.is_a? Numeric
      format_number_value(value, column.options)
    else
      format_value(value, column.options, column)
    end
  else
    if column.association.collection?
      associated_size = column_association_size(record, column, value) if column.associated_number? # get count before cache association
      if column.association.respond_to_target? && !value.loaded?
        cache_association(record.association(column.name), column, associated_size)
      end
    end
    format_association_value(value, column, associated_size)
  end
end

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 260

def format_grouped_search_column(value, options = {})
  case search_group_function
  when 'year_month'
    year, month = value.to_s.scan(/(\d*)(\d{2})/)[0]
    I18n.l(Date.new(year.to_i, month.to_i, 1), format: options[:group_format] || search_group_function.to_sym)
  when 'year_quarter'
    year, quarter = value.to_s.scan(/(\d*)(\d)/)[0]
    I18n.t(options[:group_format] || search_group_function, scope: 'date.formats', year: year, quarter: quarter)
  when 'quarter'
    I18n.t(options[:group_format] || search_group_function, scope: 'date.formats', num: value)
  when 'month'
    I18n.l(Date.new(Time.zone.today.year, value, 1), format: options[:group_format] || search_group_function.to_sym)
  when 'year'
    value.to_i
  else
    value
  end
end

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 241

def format_number_value(value, options = {})
  if value
    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
  end
  clean_column_value(value)
end

#format_singular_association_value(value, column, label_method) ⇒ Object



304
305
306
307
308
309
310
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 304

def format_singular_association_value(value, column, label_method)
  if column.association.polymorphic?
    "#{value.class.model_name.human}: #{value.send(label_method)}"
  else
    value.send(label_method)
  end
end

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



323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 323

def format_value(column_value, options = {}, column = nil)
  options ||= column&.options
  value =
    if column_empty?(column_value)
      empty_field_text(column)
    elsif column_value.is_a?(Time) || column_value.is_a?(Date)
      l(column_value, format: options&.dig(:format) || :default)
    elsif !!column_value == column_value # rubocop:disable Style/DoubleNegation -- fast check for boolean
      as_(column_value.to_s.to_sym)
    else
      column_value.to_s
    end
  clean_column_value(value)
end

#get_column_method(record, column) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 30

def get_column_method(record, column)
  # check for an override helper
  ActiveScaffold::Registry.cache :column_methods, column.cache_key do
    if (method = 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.
      method
    # second, check if the dev has specified a valid list_ui for this column
    elsif column.list_ui && (method = override_column_ui(column.list_ui))
      [method, true]
    elsif column.column && (method = override_column_ui(column.column_type)) # rubocop:disable Lint/DuplicateBranch
      method
    else
      :format_column_value
    end
  end
end

#get_column_value(record, column) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 11

def get_column_value(record, column)
  record = record.send(column.delegated_association.name) if column.delegated_association
  if record
    method, list_ui = get_column_method(record, column)
    value =
      if list_ui
        send(method, record, column, ui_options: column.list_ui_options || column.options)
      else
        send(method, record, column)
      end
  else
    value = nil
  end
  value = '&nbsp;'.html_safe if value.nil? || value.blank? # fix for IE 6
  value
rescue StandardError => e
  handle_exception_on_column(e, column, record)
end

#inplace_edit?(record, column) ⇒ Boolean

==========

Inline Edit =

==========

Returns:

  • (Boolean)


361
362
363
364
365
366
367
368
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 361

def inplace_edit?(record, column)
  return false unless column.inplace_edit
  if controller.respond_to?(:update_authorized?, true)
    return Array(controller.send(:update_authorized?, record, column.name))[0]
  end

  record.authorized_for?(crud_type: :update, column: column.name)
end

#inplace_edit_cloning?(column) ⇒ Boolean

Returns:

  • (Boolean)


370
371
372
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 370

def inplace_edit_cloning?(column)
  column.inplace_edit != :ajax && (override_form_field?(column) || column.form_ui || (column.column && override_input?(column.column_type)))
end

#inplace_edit_control(column) ⇒ Object



392
393
394
395
396
397
398
399
400
401
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 392

def inplace_edit_control(column)
  return unless inplace_edit?(active_scaffold_config.model, column) && inplace_edit_cloning?(column)

  column.form_ui = :select if column.association && column.form_ui.nil?
  options = active_scaffold_input_options(column).merge(object: column.active_record_class.new)
  options[:class] = "#{options[:class]} inplace_field"
  options[:'data-id'] = options[:id]
  options[:id] = nil
  (:div, active_scaffold_input_for(column, nil, options), style: 'display:none;', class: inplace_edit_control_css_class)
end

#inplace_edit_control_css_classObject



403
404
405
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 403

def inplace_edit_control_css_class
  'as_inplace_pattern'
end

#inplace_edit_data(column) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 408

def inplace_edit_data(column)
  data = {}
  data[:ie_url] = url_for(params_for(action: 'update_column', column: column.name, id: '__id__'))
  data[:ie_cancel_text] = column.options[:cancel_text] || as_(:cancel)
  data[:ie_loading_text] = column.options[:loading_text] || as_(:loading)
  data[:ie_save_text] = column.options[:save_text] || as_(:update)
  data[:ie_saving_text] = column.options[:saving_text] || as_(:saving)
  data[:ie_rows] = column.options[:rows] || 5 if column.column&.type == :text
  data[:ie_cols] = column.options[:cols] if column.options[:cols]
  data[:ie_size] = column.options[:size] if column.options[:size]
  data[:ie_use_html] = column.options[:use_html] if column.options[:use_html]

  if column.list_ui == :checkbox
    data[:ie_mode] = :inline_checkbox
  elsif inplace_edit_cloning?(column)
    data[:ie_mode] = :clone
  elsif column.inplace_edit == :ajax
    url = url_for(params_for(controller: params_for[:controller], action: 'render_field', id: '__id__', update_column: column.name))
    plural = column.association&.collection? && !override_form_field?(column) && INPLACE_EDIT_PLURAL_FORM_UI.include?(column.form_ui)
    data[:ie_render_url] = url
    data[:ie_mode] = :ajax
    data[:ie_plural] = plural
  end
  data
end

#list_record_viewObject



7
8
9
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 7

def list_record_view
  'list_record'
end

#mark_column_headingObject



444
445
446
447
448
449
450
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 444

def mark_column_heading
  tag_options = {
    id: "#{controller_id}_mark_heading",
    class: 'mark_heading in_place_editor_field'
  }
  (:span, check_box_tag("#{controller_id}_mark_heading_span_input", '1', all_marked?), tag_options)
end

#override_column_ui(list_ui) ⇒ Object Also known as: override_column_ui?

the naming convention for overriding column types with helpers



177
178
179
180
181
182
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 177

def override_column_ui(list_ui)
  ActiveScaffold::Registry.cache :column_ui_overrides, list_ui do
    method = "active_scaffold_column_#{list_ui}"
    method if respond_to? method
  end
end

#read_value_from_record(record, column, join_text = ' - ') ⇒ Object

Formatting



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 189

def read_value_from_record(record, column, join_text = ' - ')
  if grouped_search? && column == search_group_column
    value =
      if search_group_function
        record[column.name]
      elsif search_group_column.group_by
        safe_join column.group_by.map.with_index { |_, index| record["#{column.name}_#{index}"] }, join_text
      end
  end
  value || record.send(column.name)
end

#render_column_calculation(column, id_condition: true) ⇒ Object



505
506
507
508
509
510
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 505

def render_column_calculation(column, id_condition: true)
  calculation = column_calculation(column, id_condition: id_condition)
  override_formatter = "render_#{column.name}_#{column.calculate.is_a?(Proc) ? :calculate : column.calculate}"
  calculation = send(override_formatter, calculation) if respond_to? override_formatter
  format_column_calculation(column, calculation)
end

#render_column_heading(column, sorting, sort_direction) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 458

def render_column_heading(column, sorting, sort_direction)
  tag_options = column_heading_attributes(column, sorting, sort_direction)
  if column.name == :as_marked
    tag_options[:data] = {
      ie_mode: :inline_checkbox,
      ie_url: url_for(params_for(action: 'mark', id: '__id__'))
    }
  elsif column.inplace_edit
    tag_options[:data] = inplace_edit_data(column)
  end
  (:th, column_heading_value(column, sorting, sort_direction) + inplace_edit_control(column), tag_options)
end

#render_list_column(text, column, record) ⇒ Object

TODO: move empty_field_text and   logic 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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 51

def render_list_column(text, column, record)
  if column.link && !skip_action_link?(column.link, record)
    link = column.link
    associated = record.send(column.association.name) if column.association
    authorized = link.action.nil?
    authorized, reason = column_link_authorized?(link, column, record, associated) unless authorized
    render_action_link(link, record, link: text, authorized: authorized, not_authorized_reason: reason)
  elsif inplace_edit?(record, column)
    active_scaffold_inplace_edit(record, column, formatted_column: text)
  elsif column_wrap_tag
     column_wrap_tag, text
  else
    text
  end
rescue StandardError => e
  handle_exception_on_column(e, column, record)
end

#tel_to(text) ⇒ Object



156
157
158
159
160
161
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 156

def tel_to(text)
  text = text.to_s
  groups = text.scan(/(?:^\+)?\d+/)
  extension = groups.pop if text.match?(/\s*[^\d\s]+\s*\d+$/)
  link_to text, "tel:#{[groups.join('-'), extension].compact.join(',')}"
end