Module: DataTablesController

Defined in:
lib/data_tables.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



4
5
6
# File 'lib/data_tables.rb', line 4

def self.included(cls)
  cls.extend(ClassMethods)
end

Instance Method Details

#datatable_source(name) ⇒ Object



447
448
449
# File 'lib/data_tables.rb', line 447

def datatable_source(name)
  {:action => name, :attrs => method("datatable_#{name}_columns".to_sym).call}
end

#datatables_instance_get_value(instance, column) ⇒ Object

gets the value for a column and row



403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/data_tables.rb', line 403

def datatables_instance_get_value(instance, column)
  if column[:special]
    get_instance_special_value(instance, column[:special])
  elsif column[:attribute]
    begin
    get_instance_value(instance.send("#{column[:attribute]}"))
    rescue ArgumentError => error
      handle_argument_error(error, instance, column)
    end
  else
    return "value not found"
  end
end

#get_instance_special_value(instance, special) ⇒ Object



417
418
419
420
421
422
423
424
# File 'lib/data_tables.rb', line 417

def get_instance_special_value(instance, special)
  if special[:method]
    return method(special[:method].to_sym).call(instance)
  elsif special[:eval]
    proc = lambda { obj = instance; binding }
    return Kernel.eval(special[:eval], proc.call)
  end
end

#get_instance_value(value) ⇒ Object



426
427
428
429
430
431
432
433
# File 'lib/data_tables.rb', line 426

def get_instance_value(value)
  if !value.blank? || value == false
    trans = I18n.t(value.to_s.to_sym, :default => value.to_s)
    return trans.class == String ? trans : value.to_s
  else
    return ''
  end
end

#handle_argument_error(error, instance, column) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
# File 'lib/data_tables.rb', line 435

def handle_argument_error(error, instance, column)
  if error.message.include? "UTF-8"
    invalid_sequence = instance.send("#{column[:attribute]}").bytes.to_a
    logger.warn("[datatables] Error: #{instance.class.name} for "+
                "id #{instance.id}, column #{column[:attribute]}: " +
                "Invalid UTF8 sequence is [#{invalid_sequence.join(", ")}]")
    return ''
  else
    raise
  end
end