class DiningTable::Presenters::HTMLPresenter

Attributes

base_tags_configuration[RW]
output[W]
row_config_block[RW]
table_config_block[RW]
table_tags_configuration[RW]
tags_configuration[RW]

Public Class Methods

new( options = {} ) click to toggle source
Calls superclass method
# File lib/dining-table/presenters/html_presenter.rb, line 12
def initialize( options = {} )
  super
  self.base_tags_configuration = HTMLPresenterConfiguration::TagsConfiguration.from_hash( default_options )
  base_tags_configuration.merge_hash( options )
  self.output = ''.html_safe
end

Public Instance Methods

end_body() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 42
def end_body
  add_tag(:end, :tbody)
end
end_table() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 31
def end_table
  add_tag(:end, :table)
  if options[:wrap]
    add_tag(:end, wrap_tag )
  end
end
identifier() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 19
def identifier
  :html
end
output() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 86
def output
  @output
end
render_header() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 57
def render_header
  set_up_row_configuration( :header, nil )
  add_tag(:start, :thead, tag_options(:thead))
  add_tag(:start, :tr, row_options)
  columns.each do |column|
    value = column.header
    configuration = cell_configuration( tags_configuration, column, :header, nil )
    render_header_cell( value, configuration )
  end
  add_tag(:end,   :tr)
  add_tag(:end,   :thead)
end
render_row( object ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 46
def render_row( object )
  set_up_row_configuration( table.index, object )
  add_tag(:start, :tr, row_options)
  columns.each do |column|
    value = column.value( object )
    configuration = cell_configuration( tags_configuration, column, table.index, object )
    render_cell( value, configuration )
  end
  add_tag(:end,   :tr)
end
row_config(&block) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 94
def row_config(&block)
  self.row_config_block = block
end
start_body() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 38
def start_body
  add_tag(:start, :tbody, tag_options(:tbody))
end
start_table() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 23
def start_table
  set_up_configuration
  if options[:wrap]
    add_tag(:start, wrap_tag, wrap_options )
  end
  add_tag(:start, :table, table_options )
end
table_config(&block) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 90
def table_config(&block)
  self.table_config_block = block
end

Private Instance Methods

add_tag(type, tag, options = {}) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 104
def add_tag(type, tag, options = {})
  string = send("#{ type.to_s }_tag", tag, options)
  output_ << string
end
cell_configuration( start_configuration, column, index, object ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 190
def cell_configuration( start_configuration, column, index, object )
  column_options = column_options_cache( column )
  return start_configuration if !column_options
  new_configuration = start_configuration.dup
  if column_options.is_a?(Hash)
    new_configuration.merge_hash( column_options )
  else # callable
    column_options.call( new_configuration, index, object )
    new_configuration
  end
end
column_options_cache( column ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 173
def column_options_cache( column )
  @column_options_cache ||= { }
  @column_options_cache[ column ] ||= begin
    column_options = column.options_for( identifier )
    if column_options.is_a?(Hash)
      if column_options[:th_options] || column_options[:td_options]
        warn "[DEPRECATION] dining-table: options \"th_options\" and \"td_options\" are deprecated, please use \"th\" and \"td\" instead. Example: \"{ td: { class: 'my_class' } }\"."
        column_options[:th] = column_options.delete(:th_options)
        column_options[:td] = column_options.delete(:td_options)
      end
      column_options[:tags] ? column_options : { :tags => column_options }
    elsif column_options.respond_to?(:call)
      column_options
    end
  end
end
end_tag(tag, options = {}) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 113
def end_tag(tag, options = {})
  "</#{ tag.to_s }>".html_safe
end
options_string(options) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 135
def options_string(options)
  return '' if options.nil?
  options.each_key.inject('') do |result, key|
    result += " #{key.to_s}=\"#{ options[key] }\"" if !options_to_skip.include?( key )
    result
  end
end
options_to_skip() click to toggle source

don't output these keys as html options in options_string

# File lib/dining-table/presenters/html_presenter.rb, line 144
def options_to_skip
  [ :wrap ]
end
output_() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 100
def output_
  @output
end
render_cell( string, configuration ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 117
def render_cell( string, configuration )
  render_general_cell( string, configuration, :td)
end
render_general_cell( string, configuration, cell_tag ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 129
def render_general_cell( string, configuration, cell_tag )
  add_tag(:start, cell_tag, tag_options(cell_tag, configuration) )
  output_ << string.to_s
  add_tag(:end,   cell_tag)
end
render_header_cell( string, configuration ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 121
def render_header_cell( string, configuration )
  render_general_cell( string, configuration, :th)
end
row_options() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 169
def row_options
  tag_options(:tr)
end
set_up_configuration() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 207
def set_up_configuration
  self.table_tags_configuration = base_tags_configuration.dup
  table_config_block.call( table_tags_configuration ) if table_config_block
  self.tags_configuration = table_tags_configuration.dup
end
set_up_row_configuration( index, object ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 213
def set_up_row_configuration( index, object )
  self.tags_configuration = table_tags_configuration.dup
  row_config_block.call( tags_configuration, index, object ) if row_config_block
end
start_tag(tag, options = {}) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 109
def start_tag(tag, options = {})
  "<#{ tag.to_s }#{ options_string(options) }>".html_safe
end
table_options() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 158
def table_options
  options_ = tag_options(:table)
  return options_ unless options_.empty?
  if options[:class]
    warn "[DEPRECATION] dining-table: option \"class\" is deprecated, please use \"tags: { table: { class: 'my_class' } }\" instead."
    { :class => options[:class] }
  else
    { }
  end
end
tag_options( tag, configuration = nil ) click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 202
def tag_options( tag, configuration = nil )
  configuration ||= tags_configuration
  configuration.send( tag ).to_h
end
wrap_options() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 152
def wrap_options
  options_ = options[:wrap].dup
  options_.delete(:tag)
  options_
end
wrap_tag() click to toggle source
# File lib/dining-table/presenters/html_presenter.rb, line 148
def wrap_tag
  options[:wrap][:tag]
end