Class: DryCrud::Table::Builder
- Inherits:
-
Object
- Object
- DryCrud::Table::Builder
- Defined in:
- app/helpers/dry_crud/table/builder.rb
Overview
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
-
.table(entries, template, **options) {|t| ... } ⇒ Object
Convenience method to directly generate a table.
Instance Method Summary collapse
-
#align_class(attr) ⇒ Object
Returns css classes used for alignment of the cell data.
-
#attr(attr, header = nil, **html_options, &block) ⇒ Object
Define a column for the given attribute and an optional header.
-
#attr_header(attr) ⇒ Object
Creates a header string for the given attr.
-
#attrs(*attrs) ⇒ Object
Convenience method to add one or more attribute columns.
-
#col(header = '', **html_options, &block) ⇒ Object
Define a column for the table with the given header, the html_options used for each td and a block rendering the contents of a cell for the current entry.
-
#initialize(entries, template, **options) ⇒ Builder
constructor
A new instance of Builder.
-
#to_html ⇒ Object
Renders the table as HTML.
Methods included from Actions
#action_col, #attr_with_show_link, #destroy_action_col, #edit_action_col, #show_action_col, #table_action_link
Methods included from Sorting
#sort_header, #sortable_attr, #sortable_attrs
Constructor Details
#initialize(entries, template, **options) ⇒ Builder
Returns a new instance of Builder.
23 24 25 26 27 28 |
# File 'app/helpers/dry_crud/table/builder.rb', line 23 def initialize(entries, template, **) @entries = entries @template = template @options = @cols = [] end |
Instance Attribute Details
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
17 18 19 |
# File 'app/helpers/dry_crud/table/builder.rb', line 17 def cols @cols end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
17 18 19 |
# File 'app/helpers/dry_crud/table/builder.rb', line 17 def entries @entries end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'app/helpers/dry_crud/table/builder.rb', line 17 def @options end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
17 18 19 |
# File 'app/helpers/dry_crud/table/builder.rb', line 17 def template @template end |
Class Method Details
.table(entries, template, **options) {|t| ... } ⇒ Object
Convenience method to directly generate a table. Renders a row for each entry in entries. Takes a block that gets the table object as parameter for configuration. Returns the generated html for the table.
33 34 35 36 37 |
# File 'app/helpers/dry_crud/table/builder.rb', line 33 def self.table(entries, template, **) t = new(entries, template, **) yield t t.to_html end |
Instance Method Details
#align_class(attr) ⇒ Object
Returns css classes used for alignment of the cell data. Based on the column type of the attribute.
75 76 77 78 79 80 81 82 83 |
# File 'app/helpers/dry_crud/table/builder.rb', line 75 def align_class(attr) entry = entries.present? ? entry_class.new : nil case column_type(entry, attr) when :integer, :float, :decimal 'right' unless association(entry, attr, :belongs_to) when :boolean 'center' end end |
#attr(attr, header = nil, **html_options, &block) ⇒ Object
Define a column for the given attribute and an optional header. If no header is given, the attribute name is used. The cell will contain the formatted attribute value for the current entry.
58 59 60 61 62 63 |
# File 'app/helpers/dry_crud/table/builder.rb', line 58 def attr(attr, header = nil, **, &block) header ||= attr_header(attr) block ||= ->(e) { format_attr(e, attr) } add_css_class(, align_class(attr)) col(header, **, &block) end |
#attr_header(attr) ⇒ Object
Creates a header string for the given attr.
86 87 88 |
# File 'app/helpers/dry_crud/table/builder.rb', line 86 def attr_header(attr) (attr, entry_class) end |
#attrs(*attrs) ⇒ Object
Convenience method to add one or more attribute columns. The attribute name will become the header, the cells will contain the formatted attribute value for the current entry.
49 50 51 52 53 |
# File 'app/helpers/dry_crud/table/builder.rb', line 49 def attrs(*attrs) attrs.each do |a| attr(a) end end |
#col(header = '', **html_options, &block) ⇒ Object
Define a column for the table with the given header, the html_options used for each td and a block rendering the contents of a cell for the current entry. The columns appear in the order they are defined.
42 43 44 |
# File 'app/helpers/dry_crud/table/builder.rb', line 42 def col(header = '', **, &block) @cols << Col.new(header, , @template, block) end |
#to_html ⇒ Object
Renders the table as HTML.
66 67 68 69 70 71 |
# File 'app/helpers/dry_crud/table/builder.rb', line 66 def to_html tag.table(**) do tag.thead(html_header) + content_tag_nested(:tbody, entries) { |e| html_row(e) } end end |