Class: TableFor::Base

Inherits:
WithTemplate::Base
  • Object
show all
Defined in:
lib/table_for/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
# File 'lib/table_for/base.rb', line 15

def initialize(view, options={})
  super(view, TableFor.config.merge(options))
end

Instance Attribute Details

#current_indexObject Also known as: current_row_index

Returns the value of attribute current_index.



11
12
13
# File 'lib/table_for/base.rb', line 11

def current_index
  @current_index
end

#current_recordObject Also known as: current_row

Returns the value of attribute current_record.



8
9
10
# File 'lib/table_for/base.rb', line 8

def current_record
  @current_record
end

Instance Method Details

#cell_content(record, column, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/table_for/base.rb', line 85

def cell_content(record, column, options={})
  if options[:formatter]
    if options[:formatter].is_a?(Proc)
      call_with_params(options[:formatter], record.send(column.name), options)
    else
      record.send(column.name).try(*options[:formatter])
    end
  elsif options[:data] || [:edit, :show, :delete].include?(column.name)
    call_with_params(options[:data], record)
  else
    record.send(column.name)
  end
end

#column(*args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/table_for/base.rb', line 27

def column(*args, &block)
  options = args.extract_options!
  queue(*args, options, &block)
  if options[:link_url] ||
     options[:link_action] ||
     options[:link_method] ||
     options[:link_confirm] ||
     options[:link]
    around(columns.last.name) do |content_block, record, column, options|
      options = options.merge(column.options)
      url = if options[:link_url]
        call_with_params(options[:link_url], record)
      else
        [
          options[:link_action],
          options[:link_namespace] || global_options[:link_namespace],
          record
        ].flatten.compact
      end
      html_options = {data: {}}
      html_options[:data][:method] = options[:link_method] if options[:link_method].present?
      html_options[:data][:confirm] = options[:link_confirm] if options[:link_confirm].present?
      html_options = html_options.deep_merge(options[:link_html] || {})
      view.link_to content_block.call, url, html_options
    end
  end
  nil
end


23
24
25
# File 'lib/table_for/base.rb', line 23

def footer(options={}, &block)
  define(:footer_content, options, &block)
end

#header(name, options = {}, &block) ⇒ Object



19
20
21
# File 'lib/table_for/base.rb', line 19

def header(name, options={}, &block)
  define("#{name.to_s}_header", options.reverse_merge(:header => true), &block)
end

#header_cell_content(column, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/table_for/base.rb', line 71

def header_cell_content(column, options={})
  unless options[:header] == false
    header_sort_link(column, options) do
      if options[:header]
        call_with_params options[:header], column
      elsif column.anonymous
        nil
      else
        I18n.t("#{translation_lookup_prefix}.#{column.name.to_s.underscore}", :default => column.name.to_s.titleize)
      end
    end
  end
end

#header_column_html(column, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/table_for/base.rb', line 56

def header_column_html(column, options={})
  header_column_html = call_each_hash_value_with_params(options[:header_column_html], column)
  if options[:sortable]
    order = options[:order] ? options[:order].to_s : column.name.to_s

    sort_modes = TableFor.config.sort_modes
    current_sort_mode = (view.params[:order] != order || view.params[:sort_mode].blank?) ? nil : view.params[:sort_mode]
    current_sort_mode = sort_modes[sort_modes.index(current_sort_mode.to_sym)] rescue nil if current_sort_mode
    sort_class = "sorting#{"_#{current_sort_mode}" if current_sort_mode}"

    header_column_html[:class] = (header_column_html[:class] ? "#{header_column_html[:class]} #{sort_class}" : sort_class)
  end
  header_column_html
end


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/table_for/base.rb', line 104

def header_sort_link(column, options={}, &block)
  if options[:sortable] && (options[:header] || !column.anonymous)
    order = options[:order] ? options[:order].to_s : column.name.to_s

    sort_modes = TableFor.config.sort_modes
    current_sort_mode = (view.params[:order] != order || view.params[:sort_mode].blank?) ? nil : view.params[:sort_mode]
    next_sort_mode_index = sort_modes.index(current_sort_mode.to_sym) + 1 rescue 0
    if next_sort_mode_index == sort_modes.length
      next_sort_mode = nil
    else
      next_sort_mode = sort_modes[next_sort_mode_index]
    end

    parameters = view.params.merge(:order => order, :sort_mode => next_sort_mode)
    if parameters.respond_to?(:to_unsafe_h)
      parameters = parameters.to_unsafe_h
    end
    parameters.delete(:action)
    parameters.delete(:controller)
    url = options[:sort_url] ? options[:sort_url] : ""
    view.link_to view.capture(self, &block), "#{url}?#{parameters.to_query}"
  else
    view.capture(self, &block)
  end
end

#set_current_record_and_index(record, index) ⇒ Object



99
100
101
102
# File 'lib/table_for/base.rb', line 99

def set_current_record_and_index(record, index)
  self.current_record = record
  self.current_index = index
end