Module: ActiveScaffold::Helpers::ListColumnHelpers

Included in:
ViewHelpers
Defined in:
lib/active_scaffold/helpers/list_column_helpers.rb,
lib/active_scaffold/bridges/paperclip/lib/list_ui.rb,
lib/active_scaffold/bridges/carrierwave/lib/list_ui.rb,
lib/active_scaffold/bridges/file_column/lib/list_ui.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



58
59
60
61
62
63
64
65
66
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 58

def action_link_to_inline_form(column, record, associated)
  link = column.link.clone
  if column.polymorphic_association?
    polymorphic_controller = controller_path_for_activerecord(record.send(column.association.name).class)
    return link if polymorphic_controller.nil?
    link.controller = polymorphic_controller
  end
  configure_column_link(link, associated, column.actions_for_association_links)
end

#active_scaffold_column_carrierwave(column, record) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/active_scaffold/bridges/carrierwave/lib/list_ui.rb', line 4

def active_scaffold_column_carrierwave(column, record)
  carrierwave = record.send("#{column.name}")
  return nil unless !carrierwave.file.blank?
  thumbnail_style = ActiveScaffold::Bridges::Carrierwave::Lib::CarrierwaveBridgeHelpers.thumbnail_style
  content = if carrierwave.versions.keys.include?(thumbnail_style)
    image_tag(carrierwave.url(thumbnail_style), :border => 0).html_safe
  else
    record.send(record.send(:_mounter, column.name).send(:serialization_column))
  end
  link_to(content, carrierwave.url, :target => '_blank')
end

#active_scaffold_column_checkbox(column, record) ⇒ Object



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

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


10
11
12
13
14
# File 'lib/active_scaffold/bridges/file_column/lib/list_ui.rb', line 10

def active_scaffold_column_download_link(column, record, label = nil)
  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


5
6
7
8
# File 'lib/active_scaffold/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_paperclip(column, record) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/active_scaffold/bridges/paperclip/lib/list_ui.rb', line 4

def active_scaffold_column_paperclip(column, record)
  paperclip = record.send("#{column.name}")
  return nil unless paperclip.file?
  content = if paperclip.styles.include?(ActiveScaffold::Bridges::Paperclip::Lib::PaperclipBridgeHelpers.thumbnail_style)
    image_tag(paperclip.url(ActiveScaffold::Bridges::Paperclip::Lib::PaperclipBridgeHelpers.thumbnail_style), :border => 0)
  else
    paperclip.original_filename
  end
  link_to(content, paperclip.url, {'data-popup' => true, :target => '_blank'})
end

#active_scaffold_column_select(column, record) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 122

def active_scaffold_column_select(column, record)
  if column.association
    format_column_value(record, column)
  else
    value = record.send(column.name)

    text, val = column.options[:options].find {|text, val| (val.nil? ? text : val).to_s == value.to_s}
    value = active_scaffold_translated_option(column, text, val).first if text
    format_column_value(record, column, value)
  end
end

#active_scaffold_column_text(column, record) ⇒ Object

Overrides



118
119
120
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 118

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

#active_scaffold_column_thumbnail(column, record) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/active_scaffold/bridges/file_column/lib/list_ui.rb', line 16

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_inplace_edit(record, column, options = {}) ⇒ Object



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

def active_scaffold_inplace_edit(record, column, options = {})
  formatted_column = options[:formatted_column] || format_column_value(record, column)
  id_options = {:id => record.id.to_s, :action => 'update_column', :name => column.name.to_s}
  tag_options = {:id => element_cell_id(id_options), :class => "in_place_editor_field",
                 :title => as_(:click_to_edit), 'data-ie_id' => record.id.to_s}
  tag_options['data-ie_mode'] = :inline_checkbox if column.list_ui == :checkbox
  (:span, formatted_column, tag_options)
end

#cache_association(value, column) ⇒ Object



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

def cache_association(value, column)
  # loaded? and target seems to be gone in rails 3.1
  # 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, options = {}) ⇒ 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. if sanitize_value key is passed to this function sanitize will be used



107
108
109
110
111
112
113
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 107

def clean_column_value(v, options = {})
  if (options[:sanitize_value].present?)
    sanitize(v)
  else
    h(v)
  end
end

#column_controller_override(column) ⇒ Object

list_column override specific to this controller column combination



141
142
143
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 141

def column_controller_override(column)
  "#{clean_class_name(column.active_record_class.name)}_#{clean_column_name(column.name)}_column"
end

#column_controller_override?(column) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 145

def column_controller_override?(column)
  respond_to?(column_controller_override(column))
end

#column_heading_value(column, sorting, sort_direction) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 354

def column_heading_value(column, sorting, sort_direction)
  if column.sortable?
    options = {:id => nil, :class => "as_sort",
               'data-page-history' => controller_id,
               :remote => true, :method => :get}
    url_options = params_for(:action => :index, :page => 1,
                     :sort => column.name, :sort_direction => sort_direction)
    link_to column.label, url_options, options
  else
    if column.name != :marked
      (:p, column.label)
    else
      mark_column_heading
    end
  end
end

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 87

def column_link_authorized?(link, column, record, associated)
  if column.association
    associated_for_authorized = if associated.nil? || (associated.respond_to?(:empty?) && associated.empty?)
      column.association.klass
    elsif [:has_many, :has_and_belongs_to_many].include? column.association.macro
      associated.first
    else
      associated
    end
    authorized = associated_for_authorized.authorized_for?(:crud_type => link.crud_type)
    authorized = authorized and record.authorized_for?(:crud_type => :update, :column => column.name) if link.crud_type == :create
    authorized
  else
    record.authorized_for?(:crud_type => link.crud_type)
  end
end

#column_override(column) ⇒ Object

list_column overide for a specific column in all controllers



150
151
152
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 150

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)


154
155
156
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 154

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


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 68

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

#format_association_value(value, column, size) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 203

def format_association_value(value, column, size)
  case column.association.macro
    when :has_one, :belongs_to
      if column.polymorphic_association?
        format_value("#{value.class.model_name.human}: #{value.to_label}")
      else
        format_value(value.to_label)
      end
    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, value = nil) ⇒ Object

Formatting



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 170

def format_column_value(record, column, value = nil)
  value ||= record.send(column.name) unless record.nil?
  if value && column.association # cache association size before calling column_empty?
    associated_size = value.size if column.plural_association? 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



271
272
273
274
275
276
277
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 271

def format_inplace_edit_column(record,column)
  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



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

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



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

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

#generate_list_column_helper_code(column) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 384

def generate_list_column_helper_code(column)
  method_name = column_controller_override(column)
  if column_override? column
    "def #{method_name}(column, record)\n  #{column_override(column)}(column, record)\nend"
  elsif column.list_ui and override_column_ui?(column.list_ui)
    "def #{method_name}(column, record)\n  #{override_column_ui(column.list_ui)}(column, record)\nend"
  elsif column.column and override_column_ui?(column.column.type)
    "def #{method_name}(column, record)\n  #{override_column_ui(column.column.type)}(column, record)\nend"
  #associations and virtual columns will use default list_column helper methods..
  elsif column.association || column.column.nil?
    "def #{method_name}(record, column)\n get_column_value(column, record)\nend"
  else
    "def #{method_name}(column, record)\n value ||= record.send(column.name)\n value.nil? ? h(active_scaffold_config.list.empty_field_text) : h(#{list_column_helper_code_by_column_type(column)})\nend"
  end
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
# File '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), column, record)
    # 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 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;'.html_safe 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)


259
260
261
262
263
264
265
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 259

def inplace_edit?(record, column)
  if column.inplace_edit
    editable = controller.send(:update_authorized?, record) if controller.respond_to?(:update_authorized?, true)
    editable = record.authorized_for?(:crud_type => :update, :column => column.name) if editable.nil? || editable == true
    editable
  end
end

#inplace_edit_cloning?(column) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 267

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



288
289
290
291
292
293
294
295
296
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 288

def inplace_edit_control(column)
  if inplace_edit?(active_scaffold_config.model, column) and inplace_edit_cloning?(column)
    @record = new_model
    column = column.clone
    column.options = column.options.clone
    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



298
299
300
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 298

def inplace_edit_control_css_class
  "as_inplace_pattern"
end

#inplace_edit_tag_attributes(column) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 302

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

  if column.list_ui == :checkbox
    tag_options['data-ie_mode'] = :inline_checkbox
  elsif inplace_edit_cloning?(column)
    tag_options['data-ie_mode'] = :clone
  elsif column.inplace_edit == :ajax
    url = url_for(:controller => params_for[:controller], :action => 'render_field', :id => '__id__', :column => column.name, :update_column => column.name, :in_place_editing => true, :escape => false)
    plural = column.plural_association? && !override_form_field?(column) && [:select, :record_select].include?(column.form_ui)
    tag_options['data-ie_render_url'] = url
    tag_options['data-ie_mode'] = :ajax
    tag_options['data-ie_plural'] = plural
  end
  tag_options
end

#list_column_helper_code_by_column_type(column) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 400

def list_column_helper_code_by_column_type(column)
  case column.column.type
  when :integer
    "#{numeric_to_string_method_code(column.options[:format], true)}"
  when :float, :decimal
    "#{numeric_to_string_method_code(column.options[:format], false)}"
  when :boolean
    "as_(value.to_s.to_sym)"
  when :datetime, :timestamp, :time, :date
    "l(value, :format => :#{column.options[:format] || :default})"
  else
    "value.to_s"
  end
end

#mark_column_headingObject



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 327

def mark_column_heading
  if active_scaffold_config.mark.mark_all_mode == :page then
    all_marked = true
    @records.each do |record|
      all_marked = false if !marked_records.entries.include?(record.id)
    end
  else
    # if relation does not respond to kaminari total_count...
    # kaminari paging is nt active
    if @records.respond_to?(:total_count)
      all_marked = (marked_records.length >= @records.total_count)
    else
      all_marked = (marked_records.length >= @records.count)
    end
  end
  tag_options = {:id => "#{controller_id}_mark_heading", :class => "mark_heading in_place_editor_field"}
  tag_options['data-ie_url'] = url_for({:controller => params_for[:controller], :action => 'mark_all', :eid => params[:eid]})
  (:span, check_box_tag("#{controller_id}_mark_heading_span_input", !all_marked, all_marked), tag_options)
end

#numeric_to_string_method_code(format, is_integer) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 415

def numeric_to_string_method_code(format, is_integer)
  case format
  when :size
    "number_to_human_size(value, column.options[:i18n_options] || {})"
  when :percentage
    "number_to_percentage(value, column.options[:i18n_options] || {})"
  when :currency
    "number_to_currency(value, options[:i18n_options] || {})"
  when :i18n_number
    "number_with_#{is_integer ? 'delimiter' : 'precision'}(value, column.options[:i18n_options] || {})"
  else
    "value"
  end
end

#override_column_ui(list_ui) ⇒ Object

the naming convention for overriding column types with helpers



163
164
165
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 163

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

#override_column_ui?(list_ui) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#render_column_heading(column, sorting, sort_direction) ⇒ Object



347
348
349
350
351
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 347

def render_column_heading(column, sorting, sort_direction)
  tag_options = {:id => active_scaffold_column_header_id(column), :class => column_heading_class(column, sorting), :title => column.description}
  tag_options.merge!(inplace_edit_tag_attributes(column)) if column.inplace_edit
  (: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 &nbsp; 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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 33

def render_list_column(text, column, record)
  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)

    # setup automatic link
    if column.autolink? && column.singular_association? # link to inline form
      link = action_link_to_inline_form(column, record, associated)
      return text if link.crud_type.nil?
      url_options[:link] = as_(:create_new) if link.crud_type == :create
    end

    if column_link_authorized?(link, column, record, associated)
      render_action_link(link, url_options, record)
    else
      "<a class='disabled'>#{text}</a>".html_safe
    end
  else
    text = active_scaffold_inplace_edit(record, column, {:formatted_column => text}) if inplace_edit?(record, column)
    text
  end
end

#render_nested_view(action_links, url_options, record) ⇒ Object



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

def render_nested_view(action_links, url_options, record)
  rendered = []
  link_nested_controllers = []
  action_links.member.each do |link|
    if link.nested_link? && link.column && @nested_auto_open[link.column.name] && @records.length <= @nested_auto_open[link.column.name] && controller.respond_to?(:render_component_into_view, true)
      link_url_options = {:embedded => true, :format => :js}.merge(action_link_url_options(link, url_options, record, options = {:reuse_eid => true}))
      link_nested_controllers << link.controller.to_s if link.controller
      rendered << (controller.send(:render_component_into_view, link_url_options))
    end
  end
  (:tr, (:td, rendered.join(' ').html_safe), :class => "inline-adapter-autoopen", 'data-actionlink-controllers' => link_nested_controllers.join('::').html_safe, 'data-as_load'=>"tr");
end