Sha256: b9f8f10407e3111b7df437e71413eba696ec87b64b92da6b4731b14bccbb737f

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Tableficate
  class Column
    attr_reader :name, :header, :table, :header_attrs, :cell_attrs, :attrs

    def initialize(table, name, options = {}, &block)
      @table = table
      @name  = name
      @block = block

      @header       = options.delete(:header) || name.to_s.titleize
      @header_attrs = options.delete(:header_attrs) || {}

      @cell_attrs = options.delete(:cell_attrs) || {}

      @show_sort = options.delete(:show_sort) || false

      @attrs = options
    end

    def value(row)
      if @block
        output = @block.call(row)
        if output.is_a?(ActionView::OutputBuffer)
          ''
        else
          output = output.html_safe if output.respond_to? :html_safe
          output
        end
      else
        row.send(@name)
      end
    end

    def show_sort?
      @show_sort
    end

    def is_sorted?(dir = nil)
      is_sorted = @table.current_sort[:column] == self.name
      is_sorted = @table.current_sort[:dir] == dir if is_sorted and dir

      is_sorted
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tableficate-0.3.2 lib/tableficate/column.rb
tableficate-0.3.1 lib/tableficate/column.rb
tableficate-0.3.0 lib/tableficate/column.rb