Module: Admin::TableHelper

Defined in:
app/helpers/admin/table_helper.rb

Instance Method Summary collapse

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 (:td, links.join("").html_safe, class:"actions-td")

end

#actions_thObject

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)
   :div, class:"index-table-wrap" do
     :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 = {}, options = {}, &block)

  options = 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

  options[:class] = "index-td #{options[:class]}"

  if image = options.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 (:td, options) 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 a table.index-table. field_or_label may be any string, or a symbol naming a model column. sort may be true, 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 (:th, sort_link(@search, sort), class:"index-th")
  end

  if sort.eql?(true) && field_or_label.is_a?(Symbol)
    return (:th, sort_link(@search, field_or_label), class:"index-th")
  end

  return (:th, display_label, class:"index-th")

end

#show_table(content = nil, &block) ⇒ Object

Returns a <table class="show-table"> tag with the given content 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)
   :table, class:"show-table" do
    content || yield
  end
end

#show_thead_trObject

Returns the following <tr>, suitable for use in a table.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 a table.show-table. The given label is placed inside the first <td>, while the value is placed in the second <td>. Options are forwarded to content_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 the value.

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, options = nil)

  if label.is_a?(Symbol)
    options = value
    value = @resource.send(label)
    label = label.to_s.titleize
  end

  content = (:td, class:"show-td-field") do
     :span do
      label
    end
  end

  content << (:td, options) do
    value.to_s
  end

  (:tr) do
    content
  end

end