Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
= table_helper
+table_helper+ adds a helper method for generating HTML tables from collections.
== Resources
API
Bugs
Development
Source
== Description
Tables of summary data for ActiveRecord models are often formatted in the same way by creating a header indicating the attribute and a body containing the data from each record in separate rows. table_helper makes it easier to create these types of tables by DRYing much of the html being generated.
== Usage
=== Basic Example
<%= collection_table Person.find(:all) %>
...is compiled to (formatted here for the sake of sanity):
First Name | Last Name | Company | Role |
---|---|---|---|
John | Doe | 1 | President |
Jane | Doe | 1 | Vice-President |
=== Advanced Example
<%= collection_table(@posts, :id => 'posts', :class => 'summary') do |t| t.header :title t.header :category t.header :author t.header :publish_date, 'Date<br >Published' t.header :num_comments, '# Comments' t.header :num_trackbacks, '# Trackbacks'
t.rows.alternate = :odd
t.rows.each do |row, post, index|
# Notice there's no need to explicitly define the title
row.category post.category.name
row.author post.author.name
row.publish_date time_ago_in_words(post.published_at)
row.num_comments post.comments.empty? ? '-' : post.comments.size
row.num_trackbacks post.trackbacks.empty? ? '-' : post.trackbacks.size
end
t.footer :num_comments, @posts.inject(0) {|sum, post| sum += post.comments.size}
t.footer :num_trackbacks, @posts.inject(0) {|sum, post| sum += post.trackbacks.size}
end
%>
...is compiled to (formatted here for the sake of sanity):
Title | Category | Author | Date Published | # Comments | # Trackbacks |
---|---|---|---|---|---|
Open-source projects: The good, the bad, and the ugly | General | John Doe | 23 days | - | - |
5 reasons you should care about Rails | Rails | John Q. PUblic | 21 days | - | - |
Deprecation: Stop digging yourself a hole | Rails | Jane Doe | 17 days | - | - |
Jumpstart your Rails career at RailsConf 2007 | Conferences | Jane Doe | 4 days | - | - |
Getting some REST | Rails | John Doe | about 18 hours | - | - |
0 | 0 |
=== Caveat Emptor
See the API for more information on syntax, options, and examples. You should only use table_helper if it fits the needs of your application. Remember one of the key principles of Rails, KISS (Keep It Simple Stupid). table_helper works really well when you need to quickly output several of these types of summary tables. If this is not the case, you may want to stick to using actual html.
== Testing
Before you can run any tests, the following gem must be installed:
To run against a specific version of Rails:
rake test RAILS_FRAMEWORK_ROOT=/path/to/rails
== Dependencies
FAQs
Unknown package
We found that table_helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.