Sha256: a953c964459991d782f66a4e3e5c71f771670fff254c60919abff66af1ebfb20

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module TableCloth
  class Presenter
    attr_reader :view_context, :table_definition, :objects,
      :table

    def initialize(objects, table, view)
      @objects = objects
      @table_definition = table
      @view_context = view
      @table = table_definition.new(objects, view)
    end

    def render_table
      raise NoMethodError, "You must override the .render method"
    end

    def render_header
      raise NoMethodError, "You must override the .header method"
    end

    def render_rows
      raise NoMethodError, "You must override the .rows method"
    end

    def columns
      @columns ||= table.class.columns.map do |name, options|
        column = options[:class].new(name, options[:options])

        if ColumnJury.new(column, table).available?
          column
        else
          nil
        end
      end.compact
    end

    def column_names
      @column_names ||= columns.each_with_object([]) do |column, names|
        names << column.human_name(view_context)
      end
    end

    def row_values(object)
      columns.each_with_object([]) do |column, values|
        values << column.value(object, view_context, table)
      end
    end

    def rows
      objects.each_with_object([]) do |object, row|
        row << row_values(object)
      end
    end

    def wrapper_tag(type, value=nil, options={}, &block)
      options = tag_options(type, options)

      content = if block_given?
        v.content_tag(type, options, &block)
      else
        v.content_tag(type, value, options)
      end
    end

    private

    def tag_options(type, options={})
      options = options.dup
      if TableCloth.config.respond_to?(type)
        options.merge!(table.config.config_for(type))
        options.merge!(TableCloth.config.config_for(type))
      end

      options
    end

    def v
      view_context
    end

    def params
      v.params
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_cloth-0.3.1.alpha1 lib/table_cloth/presenter.rb