Class: Admino::Table::Presenter

Inherits:
Showcase::Presenter
  • Object
show all
Defined in:
lib/admino/table/presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Presenter

Returns a new instance of Presenter.



32
33
34
35
36
37
38
39
40
# File 'lib/admino/table/presenter.rb', line 32

def initialize(*args)
  context = args.pop
  collection = args.shift

  @collection_klass = args.shift
  @query = args.shift

  super(collection, context)
end

Instance Attribute Details

#collection_klassObject (readonly)

Returns the value of attribute collection_klass.



8
9
10
# File 'lib/admino/table/presenter.rb', line 8

def collection_klass
  @collection_klass
end

#queryObject (readonly)

Returns the value of attribute query.



9
10
11
# File 'lib/admino/table/presenter.rb', line 9

def query
  @query
end

Class Method Details

.tag_helper(name, tag, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/admino/table/presenter.rb', line 11

def self.tag_helper(name, tag, options = {})
  default_options_method = :"#{name}_html_options"

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

Instance Method Details

#to_html(options = {}, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/admino/table/presenter.rb', line 42

def to_html(options = {}, &block)
  table_tag(options) do
    thead_tag do
      thead_tr_tag do
        row = head_row(collection_klass, query, view_context)
        h.capture(row, nil, &block) if block_given?
        row.to_html
      end
    end <<
    tbody_tag do
      collection.each_with_index.map do |resource, index|
        tr_html_options = {
          class: zebra_css_classes[index % zebra_css_classes.size],
          id: resource.dom_id
        }
        tbody_tr_tag(resource, index, tr_html_options) do
          row = resource_row(resource, view_context)
          h.capture(row, resource, &block) if block_given?
          row.to_html
        end
      end.join.html_safe
    end
  end
end