/README.md

https://github.com/christianblais/tableize · Markdown · 223 lines · 165 code · 58 blank · 0 comment · 0 complexity · 5e61f36685d93560db6f793eb0c03218 MD5 · raw file

  1. Tableize
  2. ========
  3. Installation
  4. ------------
  5. First, install the gem
  6. gem install tableize
  7. In an initializer, include the `Tableize` module
  8. ActionView::Base.send(:include, Tableize)
  9. Usage
  10. -----
  11. The simplest scenario
  12. table_for Post, @posts do |t|
  13. t.column "Title", :title
  14. t.column "Author", :author
  15. end
  16. Is the same as
  17. table_for Post, @posts do |t|
  18. t.columns :title, :author
  19. end
  20. Both examples generate the following html
  21. <table class="posts">
  22. <thead>
  23. <tr>
  24. <th class="title string">Title</th>
  25. <th class="author string">Author</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <tr class="post_1">
  30. <td>Hello World!</td>
  31. <td>Christian Blais</td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. ### Few defaults
  36. * Inherited Resources
  37. By default, `Tableize` will search for both `resource_class` and `collection` methods. So you don't have to specify them each time, unless
  38. of course you want to override the default behavior. That being said, the following code produces the exact same output as the basic example
  39. above.
  40. table_for do |t|
  41. t.column "Title", :title
  42. t.column "Author", :author
  43. end
  44. * I18n
  45. By default, `Tableize` will try to internationalise your attribute, so you don't have to name each of your column. So, assuming my
  46. model responds to `human_attribute_name`, the following example still produces the exact same output.
  47. table_for do |t|
  48. t.column :title
  49. t.column :author
  50. end
  51. * Columns
  52. `Tableize` responds to `defaults` method, which will create a column for each public attribute of your model. Use it like this:
  53. table_for do |t|
  54. t.defaults
  55. end
  56. * Multiple columns, one line
  57. table_for do |t|
  58. t.columns :title, :author
  59. end
  60. ### Advanced features
  61. * Lambdas
  62. You can use lambdas if you want to have a custom column.
  63. table_for do |t|
  64. t.column "Title" do |post|
  65. post.title + "!"
  66. end
  67. end
  68. You can also specify a method to be called before the yield.
  69. table_for do |t|
  70. t.column "Title", :title do |title|
  71. title + "!"
  72. end
  73. end
  74. * Extras
  75. Let's say `author` is a relation, and you want to show his first name and last name as two separate columns. The following would works:
  76. table_for do |t|
  77. t.column "First name", :author do |author|
  78. author.first_name
  79. end
  80. t.column "Last name", :author do |author|
  81. author.last_name
  82. end
  83. end
  84. But the above code triggers the `author` method twice. In order to avoid that, you could use the `extra` method, which will be executed only
  85. once per row, yielding the result alongside the resource in each block. You can use more than one extra without any problem.
  86. table_for do |t|
  87. t.extra do |post|
  88. post.author
  89. end
  90. t.column "First name" do |post, author|
  91. author.first_name
  92. end
  93. t.column "Last name" do |post, author|
  94. author.last_name
  95. end
  96. end
  97. * Hashes
  98. `Tableize` also accepts hashes instead of regular collections. Use it the same way.
  99. collection = [{:title => "Hello World", "author" => "Christian Blais"}]
  100. table_for Post, collection do |t|
  101. t.column :title
  102. t.column "author"
  103. end
  104. ### Configuration
  105. `Tableize` can me customized in an initializer.
  106. Tableize::Configuration.configure do |config|
  107. [...]
  108. end
  109. #### Options
  110. Here are the defaults used in the first example.
  111. * table_options
  112. This option let you set default values for all your tables. It must be a lambda that returns a hash. The lambda receive one argument, which is the resource class, when available.
  113. config.table_options = lambda do |resource_class|
  114. {
  115. :class => "#{resource_class.model_name.tableize}"
  116. }
  117. end
  118. * th_options
  119. This option let you set default values for all your th cells. It must be a lambda that returns a hash. The lambda receive two arguments; the field name and it's value type.
  120. config.th_options = lambda do |field, type|
  121. {
  122. :class => [field, type].compact.map{ |a| a.to_s.underscore }
  123. }
  124. end
  125. * tr_options
  126. This option let you set default values for all your rows. It must be a lambda that returns a hash. The lambda receive the resource as a single argument.
  127. config.tr_options = lambda do |resource|
  128. {
  129. :class => "#{resource.class.model_name.underscore}_#{resource.id}"
  130. }
  131. end
  132. #### Custom column
  133. Let's suppose you are doing an admin section and that you always need a column to display the resource's name with a link to that resource's show page.
  134. The following would work, but it's pretty long if you need to do that all the time.
  135. table_for do |t|
  136. t.column "Title" do |resource|
  137. resource.title
  138. end
  139. end
  140. What about your own column type? In the configuration file, add this
  141. config.custom_column :title, :html => {:class => "titleized"} do |column|
  142. column.th :class => "title_th" do |resource_class|
  143. resource_class.human_attribute_name(:title)
  144. end
  145. column.td :class => "title_td" do |resource|
  146. resource.title
  147. end
  148. end
  149. Having this in your configuration file, you can now do this
  150. table_for do |t|
  151. t.title
  152. end
  153. This will produce the following html
  154. <table class="posts titleized">
  155. <thead>
  156. <tr>
  157. <th class="title string title_th">Title</th>
  158. </tr>
  159. </thead>
  160. <tbody>
  161. <tr class="post_1">
  162. <td class="title_td">Hello World!</td>
  163. </tr>
  164. </tbody>
  165. </table>