Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Created April 22, 2013 22:19
Show Gist options
  • Select an option

  • Save gogogarrett/5439062 to your computer and use it in GitHub Desktop.

Select an option

Save gogogarrett/5439062 to your computer and use it in GitHub Desktop.
%label Search
%input#filter
%hr
%table.footable{"data-filter" => "#filter"}
%thead
%tr
- if block
%th
- headers.each_with_index do |header, index|
%th
= header.to_s.humanize
- if actions
%th
Actions
%tbody
- collection.each do |obj|
%tr[obj]
- if block
%td
= block.call(obj)
- headers.each_with_index do |header, index|
%td
= cell_value(obj, header, index)
- if actions
%td
=raw action_links_for(obj).join(" ")
%hr
= form_tag url: school_class_add_multiple_path, method: :post do
= table_grid_for @students, headers: [:first_name, :last_name, :email] do |student|
- check_box_tag "student_ids[]", student.id
%br
= submit_tag "Add Students", class: 'btn btn-primary'
%h1 All students
= table_grid_for(@students, headers: [:first_name, :last_name, :email, :grade])
module TableGridHelper
def table_grid_for(collection, options={}, &block)
collection = collection.to_a.flatten
headers = options.fetch(:headers)
include_action = options.fetch(:actions, true)
render partial: "shared/grid", locals: {
collection: collection,
headers: headers,
actions: include_action,
block: block
}
end
def cell_value(obj, header, index)
index.zero? ? link_to(obj.send(header), obj) : obj.send(header)
end
def action_links_for(obj)
[
link_to("Edit", [:edit, obj], class: 'btn btn-primary'),
link_to("Delete", obj, method: :delete, class: 'btn btn-primary')
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment