module Daru::View::Display

Public Instance Methods

show_in_iruby(dom=SecureRandom.uuid) click to toggle source

@param dom [String] The ID of the DIV element that the DataTable

should be rendered in

@return [void] shows the datatable in IRuby notebook

# File lib/daru/data_tables/display/display.rb, line 96
def show_in_iruby(dom=SecureRandom.uuid)
  IRuby.html(to_html(dom))
end
show_script(dom=SecureRandom.uuid, options={}) click to toggle source

@param dom [String] The ID of the DIV element that the DataTable

should be rendered in

@param options [Hash] options provided @return [String] js script to render the table

# File lib/daru/data_tables/display/display.rb, line 59
def show_script(dom=SecureRandom.uuid, options={})
  script_tag = options.fetch(:script_tag) { true }
  if script_tag
    to_js(dom)
  else
    html = ''
    html << draw_js(dom)
    html
  end
end
to_html(id=nil, options={}) click to toggle source

If table_options is not present then it will assume that table tag is already present in the web page source, where we are pasting the html code. @param id [String] The ID of the DIV element that the DataTable

should be rendered in

@param options [Hash] options provided @return [String] Generates JavaScript and renders the table in the

final HTML output.
# File lib/daru/data_tables/display/display.rb, line 78
def to_html(id=nil, options={})
  # More things can be added into table_script.erb
  path = File.expand_path('../templates/table_script.erb', __dir__)
  template = File.read(path)
  id ||= SecureRandom.uuid # TODO: remove it or Use it for table tag.
  element_id ||= id
  table_script = show_script(element_id, script_tag: false)
  html_code = ERB.new(template).result(binding)
  # table_options is given. That means table html code is not present in
  # the webpage. So it must generate table code with given options.
  table_thead = extract_table
  draw_table_thead(element_id, html_code, table_thead)
  html_code
end
to_js(element_id) click to toggle source

@param element_id [String] The ID of the DIV element that the DataTable

should be rendered in

@return [String] returns the javascript of the DataTable

# File lib/daru/data_tables/display/display.rb, line 103
def to_js(element_id)
  js =  ''
  js << "\n<script type='text/javascript'>"
  js << draw_js(element_id)
  js << "\n</script>"
  js
end

Private Instance Methods

draw_table_thead(element_id, html_code, table_thead) click to toggle source
# File lib/daru/data_tables/display/display.rb, line 121
def draw_table_thead(element_id, html_code, table_thead)
  if html_options && html_options[:table_options]
    draw_table_thead_from_html_options(element_id, html_code, table_thead)
  # if user provided html_options but not provided html_options[:table_options]
  else
    html_code.concat(
      content_tag(
        'table', table_thead.html_safe, id: element_id, class: 'display'
      )
    )
  end
end
draw_table_thead_from_html_options(element_id, html_code, table_thead) click to toggle source
# File lib/daru/data_tables/display/display.rb, line 134
def draw_table_thead_from_html_options(element_id, html_code, table_thead)
  html_options[:table_options][:id] = element_id
  html_options[:table_options][:class] ||= 'display'
  table_thead = html_options[:table_options].delete(:table_thead) if
  html_options[:table_options][:table_thead]
  html_code.concat(
    content_tag('table', table_thead.html_safe, html_options[:table_options])
  )
end
extract_table() click to toggle source
# File lib/daru/data_tables/display/display.rb, line 113
def extract_table
  return data.to_html_thead unless data.is_a?(Array)

  path = File.expand_path('../templates/thead.erb', __dir__)
  template = File.read(path)
  ERB.new(template).result(binding)
end