Class: TableSortable::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/table_sortable/column.rb,
lib/table_sortable/column/filter.rb,
lib/table_sortable/column/sorter.rb

Defined Under Namespace

Classes: Filter, Sorter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(col_name, *options) ⇒ Column

Returns a new instance of Column.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/table_sortable/column.rb', line 6

def initialize(col_name, *options)

  options = options.extract_options!
  value = options[:value] || col_name
  content = options[:content] || value
  translation_key = options[:translation_key]
  template_path = options[:template_path]
  label = options[:label] || (options[:label] == false ? '' : I18n.translate("table_sortable.#{"#{translation_key}." if translation_key }#{col_name.to_s}", :default => col_name.to_s).titleize)
  placeholder = options[:placeholder] || (options[:placeholder] == false ? nil : label)
  template = options[:template] || col_name
  column_options = options[:options] || {}
  controller = options[:controller]

  @name = col_name
  @value = value.respond_to?(:call) ? value : -> (record) { record.send(value) }
  @content = content.respond_to?(:call) ? content : -> (record) { record.send(content) }
  @label = label
  @placeholder = placeholder
  @template = template
  @template_path = template_path

  view_path = @template_path || (defined?(Rails) ? File.join(Rails.root, 'app', 'views', "#{controller.controller_path}/table_sortable/") : '')

  view_filename = "_#{@template}_column.html"
  view = Dir.glob(File.join(view_path, "#{view_filename}.*"))
  @column_partial = view.any? ? File.join(view_path, "#{view_filename}") : false

  view_filename = "_#{@template}_header.html"
  view = Dir.glob(File.join(view_path, "#{view_filename}.*"))
  @header_partial = view.any? ? File.join(view_path, "#{view_filename}") : false

  @options = column_options
  @filter = TableSortable::Column::Filter.new(options.merge(:column => self) )
  @sorter = TableSortable::Column::Sorter.new(options.merge(:column => self) )

end

Instance Attribute Details

#column_partialObject (readonly)

Returns the value of attribute column_partial.



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

def column_partial
  @column_partial
end

#content(record) ⇒ Object (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#header_partialObject (readonly)

Returns the value of attribute header_partial.



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

def header_partial
  @header_partial
end

#labelObject (readonly)

Returns the value of attribute label.



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

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#placeholderObject (readonly)

Returns the value of attribute placeholder.



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

def placeholder
  @placeholder
end

#sorterObject (readonly)

Returns the value of attribute sorter.



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

def sorter
  @sorter
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



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

def template_path
  @template_path
end

#translation_keyObject (readonly)

Returns the value of attribute translation_key.



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

def translation_key
  @translation_key
end

Instance Method Details

#value(record) ⇒ Object



43
44
45
# File 'lib/table_sortable/column.rb', line 43

def value(record)
  record.instance_eval(&@value) unless @value.nil?
end