Class: Tabletastic::TableField

Inherits:
Object
  • Object
show all
Defined in:
lib/tabletastic/table_field.rb

Constant Summary collapse

@@association_methods =
%w[to_label display_name full_name name title username login value to_str to_s]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &proc) ⇒ TableField

Returns a new instance of TableField.



9
10
11
12
13
14
15
16
# File 'lib/tabletastic/table_field.rb', line 9

def initialize(*args, &proc)
  options = args.extract_options!
  method = args.first.to_sym
  @method_or_proc = block_given? ? proc : method
  @cell_html, @heading_html = options[:cell_html], options[:heading_html]
  @klass = options.delete(:klass)
  @heading = options.delete(:heading) || @klass.try(:human_attribute_name, method.to_s) || method.to_s.humanize
end

Instance Attribute Details

#cell_htmlObject

Returns the value of attribute cell_html.



7
8
9
# File 'lib/tabletastic/table_field.rb', line 7

def cell_html
  @cell_html
end

#headingObject

Returns the value of attribute heading.



7
8
9
# File 'lib/tabletastic/table_field.rb', line 7

def heading
  @heading
end

#heading_htmlObject

Returns the value of attribute heading_html.



7
8
9
# File 'lib/tabletastic/table_field.rb', line 7

def heading_html
  @heading_html
end

#method_or_procObject

Returns the value of attribute method_or_proc.



7
8
9
# File 'lib/tabletastic/table_field.rb', line 7

def method_or_proc
  @method_or_proc
end

Instance Method Details

#cell_data(record) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tabletastic/table_field.rb', line 18

def cell_data(record)
  # Get the attribute or association in question
  result = send_or_call(record, method_or_proc)
  # If we already have a string, just return it
  return result if result.is_a?(String)

  # If we don't have a string, its likely an association
  # Try to detect which method to use for stringifying the attribute
  to_string = detect_string_method(result)
  result.send(to_string) if to_string
end