Class: AdminAssistant::Column::View

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, action_view, admin_assistant, opts = {}) ⇒ View

Returns a new instance of View.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/admin_assistant/column.rb', line 325

def initialize(column, action_view, admin_assistant, opts = {})
  @column, @action_view, @opts = column, action_view, opts
  @model_class = admin_assistant.model_class
  base_setting = admin_assistant[name]
  @boolean_labels = base_setting.boolean_labels
  @strftime_format = base_setting.strftime_format
  fem_name = name + '_exists?'
  if @action_view.respond_to?(fem_name)
    @file_exists_method = @action_view.method(fem_name)
  end
  @label = base_setting.label
  @polymorphic_types = base_setting.polymorphic_types
  if respond_to?(:set_instance_variables_from_options)
    set_instance_variables_from_options(admin_assistant, opts)
  end
end

Instance Attribute Details

#sort_orderObject (readonly)

Returns the value of attribute sort_order.



323
324
325
# File 'lib/admin_assistant/column.rb', line 323

def sort_order
  @sort_order
end

Instance Method Details

#check_box_and_hidden_tags(input_name, value) ⇒ Object



342
343
344
345
# File 'lib/admin_assistant/column.rb', line 342

def check_box_and_hidden_tags(input_name, value)
  @action_view.send(:hidden_field_tag, input_name, '0', :id => "#{input_name}_hidden") +
      @action_view.send(:check_box_tag, input_name, '1', value)
end

#controllerObject



347
348
349
# File 'lib/admin_assistant/column.rb', line 347

def controller
  @action_view.controller
end

#custom_template_file_path(slug) ⇒ Object



351
352
353
354
355
356
# File 'lib/admin_assistant/column.rb', line 351

def custom_template_file_path(slug)
  File.join(
    Rails.root, 'app/views', controller.controller_path, 
    "#{slug}.html.erb"
  )
end

#file_option_for_custom_template_render(slug) ⇒ Object



358
359
360
# File 'lib/admin_assistant/column.rb', line 358

def file_option_for_custom_template_render(slug)
  custom_template_file_path slug
end

#labelObject



362
363
364
365
366
367
368
369
370
# File 'lib/admin_assistant/column.rb', line 362

def label
  if @label
    @label
  elsif @column.name.to_s == 'id'
    'ID'
  else
    @column.name.to_s.capitalize.gsub(/_/, ' ') 
  end
end

#nameObject



372
373
374
# File 'lib/admin_assistant/column.rb', line 372

def name
  @column.name
end

#paperclip?Boolean

Returns:

  • (Boolean)


376
377
378
# File 'lib/admin_assistant/column.rb', line 376

def paperclip?
  @column.is_a?(PaperclipColumn)
end

#sort_possible?(total_entries) ⇒ Boolean

Returns:

  • (Boolean)


380
381
382
383
# File 'lib/admin_assistant/column.rb', line 380

def sort_possible?(total_entries)
  @column.is_a?(ActiveRecordColumn) ||
      (@column.is_a?(BelongsToColumn) && total_entries < 100_000)
end

#string(record) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/admin_assistant/column.rb', line 385

def string(record)
  string_method = "#{@column.name}_string"
  if @action_view.respond_to?(string_method)
    @action_view.send string_method, record
  else
    value = value(record)
    if @boolean_labels
      value ? @boolean_labels.first : @boolean_labels.last
    elsif value.respond_to?(:strftime) && @strftime_format
      value.strftime @strftime_format
    else
      value.to_s
    end
  end
end

#value(record) ⇒ Object



401
402
403
404
405
406
407
408
# File 'lib/admin_assistant/column.rb', line 401

def value(record)
  value_method = "#{name}_value"
  if @action_view.respond_to?(value_method)
    @action_view.send value_method, record
  else
    record.send(name) if record.respond_to?(name)
  end
end