class Admino::Table::Presenter

Attributes

collection_klass[R]
query[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/admino/table/presenter.rb, line 32
def initialize(*args)
  context = args.pop
  collection = args.shift

  @collection_klass = args.shift

  if !@collection_klass && !collection.empty?
    @collection_klass = collection.first.class
  end

  @query = args.shift

  super(collection, context)
end
tag_helper(name, tag, options = {}) click to toggle source
# File lib/admino/table/presenter.rb, line 11
def self.tag_helper(name, tag, options = {})
  options_method = :"#{name}_html_options"

  define_method :"#{name}_tag" do |*args, &block|
    options = args.extract_options!
    if respond_to?(options_method, true)
      default_options = send(options_method, *args)
      html_options = Showcase::Helpers::HtmlOptions.new(default_options)
      html_options.merge_attrs!(options)
      options = html_options.to_h
    end
    h.content_tag(tag, options, &block)
  end
end

Public Instance Methods

to_html(options = {}, &block) click to toggle source
# File lib/admino/table/presenter.rb, line 47
def to_html(options = {}, &block)

  if @collection_klass.nil?
    raise ArgumentError, 'collection is empty and no explicit class is specified'
  end

  table_tag(options) do
    thead_tag do
      thead_tr_tag do
        th_tags(&block)
      end
    end <<
    tbody_tag do
      tbody_tr_tags(&block)
    end
  end
end

Private Instance Methods

base_tbody_tr_html_options(resource, index) click to toggle source
# File lib/admino/table/presenter.rb, line 104
def base_tbody_tr_html_options(resource, index)
  options = {
    class: zebra_css_classes[index % zebra_css_classes.size]
  }

  if resource.respond_to?(:dom_id)
    options[:id] = resource.dom_id
  end

  options
end
collection() click to toggle source
# File lib/admino/table/presenter.rb, line 92
def collection
  object
end
head_row(collection_klass, query, view_context) click to toggle source
# File lib/admino/table/presenter.rb, line 96
def head_row(collection_klass, query, view_context)
  HeadRow.new(collection_klass, query, view_context)
end
resource_row(resource, view_context) click to toggle source
# File lib/admino/table/presenter.rb, line 100
def resource_row(resource, view_context)
  ResourceRow.new(resource, view_context)
end
tbody_tr_tags(&block) click to toggle source
# File lib/admino/table/presenter.rb, line 67
def tbody_tr_tags(&block)
  collection.each_with_index.map do |resource, index|
    html_options = base_tbody_tr_html_options(resource, index)
    tbody_tr_tag(resource, index, html_options) do
      td_tags(resource, &block)
    end
  end.join.html_safe
end
td_tags(resource, &block) click to toggle source
# File lib/admino/table/presenter.rb, line 84
def td_tags(resource, &block)
  row = resource_row(resource, view_context)
  if block_given?
    h.capture(row, resource, &block)
  end
  row.to_html
end
th_tags(&block) click to toggle source
# File lib/admino/table/presenter.rb, line 76
def th_tags(&block)
  row = head_row(collection_klass, query, view_context)
  if block_given?
    h.capture(row, nil, &block)
  end
  row.to_html
end
zebra_css_classes() click to toggle source
# File lib/admino/table/presenter.rb, line 116
def zebra_css_classes
  %w(is-even is-odd)
end