Module: Admin::TableHelper
- Defined in:
- app/helpers/admin/table_helper.rb
Instance Method Summary collapse
-
#actions_td(resource) ⇒ Object
Returns a
<td>
tag, suitable for use inside atable.index-table
. -
#actions_th ⇒ Object
Returns an
index_th
with label"Actions"
that is not sortable. -
#index_table(content = nil, &block) ⇒ Object
Returns a
<table class="index-table">
tag with the appropriate wrapper and the givencontent
or block content inside it. -
#index_td(resource, method_or_content = {}, options = {}, &block) ⇒ Object
Returns a
<td>
tag, suitable for use inside atable.index-table
. -
#index_th(field_or_label, sort: true) ⇒ Object
Returns a
<th>
tag, suitable for use inside atable.index-table
. -
#show_table(content = nil, &block) ⇒ Object
Returns a
<table class="show-table">
tag with the givencontent
or block content inside it. -
#show_thead_tr ⇒ Object
Returns the following
<tr>
, suitable for use in atable.show-table
:. -
#show_tr(label, value = nil, options = nil) ⇒ Object
Returns a
<tr>
with two<td>
s suitable for use in atable.show-table
.
Instance Method Details
#actions_td(resource) ⇒ Object
Returns a <td>
tag, suitable for use inside a table.index-table
.
The tag contains buttons to edit, inspect, and delete the given resource
.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/helpers/admin/table_helper.rb', line 160 def actions_td(resource) links = [] if @managed_class.allows?(:edit) links << link_to("Edit", url_for(action:"edit", id:resource.id, q:ransack_params, p:page_param), class:"button -small -edit") end if @managed_class.allows?(:show) links << link_to("Inspect", url_for(action:"show", id:resource.id, q:ransack_params, p:page_param), class:"button -small -inspect") end if @managed_class.allows?(:destroy) links << link_to("Delete", url_for(action:"destroy", id:resource.id, q:ransack_params, p:page_param), class: "button -small -delete", method: :delete, :'data-confirm' => deletion_warning(resource)) end return content_tag(:td, links.join("").html_safe, class:"actions-td") end |
#actions_th ⇒ Object
Returns an index_th
with label "Actions"
that is not sortable.
154 155 156 |
# File 'app/helpers/admin/table_helper.rb', line 154 def actions_th index_th("Actions", sort:false) end |
#index_table(content = nil, &block) ⇒ Object
Returns a <table class="index-table">
tag with the appropriate wrapper
and the given content
or block content inside it.
5 6 7 8 9 10 11 |
# File 'app/helpers/admin/table_helper.rb', line 5 def index_table(content = nil, &block) content_tag :div, class:"index-table-wrap" do content_tag :table, class:"index-table" do content || yield end end end |
#index_td(resource, method_or_content = {}, options = {}, &block) ⇒ Object
Returns a <td>
tag, suitable for use inside a table.index-table
.
If method_or_content
is a symbol, it will call that method on the
given resource
to obtain the content of the <td>
. Otherwise
it expects method_or_content
or a passed block to provide suitable string.
Special Options
:image
- A URL to a square image to use in the, floating to the left of the content. The image should be a square at least 14×14px in size. Other options are forwarded to
content_tag
for the<td>
.121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
# File 'app/helpers/admin/table_helper.rb', line 121 def index_td(resource, method_or_content = {}, = {}, &block) = method_or_content if block_given? if block_given? content = capture(resource, &block) elsif method_or_content.is_a?(Symbol) content = resource.send(method_or_content) else content = method_or_content end [:class] = "index-td #{[:class]}" if image = .delete(:image) image = image_tag(image, size:"18x18", alt:"") end if @managed_class.allows? :edit link = url_for(action:"edit", id:resource.id, q:ransack_params, p:page_param) elsif @managed_class.allows? :show link = url_for(action:"show", id:resource.id, q:ransack_params, p:page_param) else link = "#" # Guh, painted ourseves into a corner here end return content_tag(:td, ) do link_to("#{image}#{content}".html_safe, link) end end
#index_th(field_or_label, sort: true) ⇒ Object
Returns a
<th>
tag, suitable for use inside atable.index-table
.field_or_label
may be any string, or a symbol naming a model column.sort
may betrue
,false
, or a symbol. See the signtures below.If the column is sortable, the
<th>
will contain a Ransack sort link that allows the end-user to organize the table by that column.Signatures
# Create a header that sorts a named column index_th(:title, sort:true) # Create a header that sorts a column, with custom label index_th("Strange Title", sort: :title) # Create a header that can't be sorted index_th("Strange Title", sort:false)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
# File 'app/helpers/admin/table_helper.rb', line 86 def index_th(field_or_label, sort:true) case field_or_label when :id display_label = "ID" when Symbol display_label = field_or_label.to_s.humanize.titleize else display_label = field_or_label end if sort.is_a?(Symbol) return content_tag(:th, sort_link(@search, sort), class:"index-th") end if sort.eql?(true) && field_or_label.is_a?(Symbol) return content_tag(:th, sort_link(@search, field_or_label), class:"index-th") end return content_tag(:th, display_label, class:"index-th") end
#show_table(content = nil, &block) ⇒ Object
Returns a
<table class="show-table">
tag with the givencontent
or block content inside it.15 16 17 18 19
# File 'app/helpers/admin/table_helper.rb', line 15 def show_table(content = nil, &block) content_tag :table, class:"show-table" do content || yield end end
#show_thead_tr ⇒ Object
Returns the following
<tr>
, suitable for use in atable.show-table
:<tr> <th>Field</th> <th>Details</th> </tr>
27 28 29
# File 'app/helpers/admin/table_helper.rb', line 27 def show_thead_tr %{<tr><th>Field</th><th>Details</th></tr>}.html_safe end
#show_tr(label, value = nil, options = nil) ⇒ Object
Returns a
<tr>
with two<td>
s suitable for use in atable.show-table
. The givenlabel
is placed inside the first<td>
, while thevalue
is placed in the second<td>
. Options are forwarded tocontent_tag
for the second<td>
.If
label
is a symbol, it is assumed to be a method on a variable named@resource
in the current template, and the<tr>
is constructed automatically for you by converting the symbol to a human-readable label and calling the named method on @resource to get thevalue
.Signatures
# Set the values yourself, and a class on the second `<td>` show_tr "Slug", resource.slug, class:"monospace" # Attempt to auto-fill the row based on a method name show_tr :slug
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# File 'app/helpers/admin/table_helper.rb', line 47 def show_tr(label, value = nil, = nil) if label.is_a?(Symbol) = value value = @resource.send(label) label = label.to_s.titleize end content = content_tag(:td, class:"show-td-field") do content_tag :span do label end end content << content_tag(:td, ) do value.to_s end content_tag(:tr) do content end end