Last active
August 29, 2015 14:24
-
-
Save mnishiguchi/1a8593b674bcaff670d6 to your computer and use it in GitHub Desktop.
Rails - content_tagでビューヘルパーを自作する。 ref: http://qiita.com/mnishiguchi/items/d7a225e89806666e48fc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
# kv_names: 配列 [key名, value名] | |
# collection: hash { key1: value1, key2: value2, ... } | |
def kv_table_for(kv_names, collection={}) | |
thead = content_tag(:thead) do | |
content_tag(:tr) do | |
tags = kv_names.map { |column| content_tag(:th, column, class: "text-center") } | |
safe_join tags | |
end | |
end | |
tbody = content_tag(:tbody) do | |
tags = collection.map do |k, v| | |
content_tag(:tr, class: "text-center") do | |
concat content_tag(:td, k) | |
concat content_tag(:td, v) | |
end | |
end | |
safe_join tags | |
end | |
content_tag(:div, class: "table-responsive") do | |
content_tag(:table, class: "table table-bordered table-hover table-striped") do | |
thead + tbody | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= kv_table_for %w(category volume), @items.group(:category).sum(:volume) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment