Created
July 11, 2015 00:44
-
-
Save buren/711c5497e2bb432b5491 to your computer and use it in GitHub Desktop.
ActiveTableView: Render "tables structure" for ActiveRecord objects
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
class ActiveTableView | |
def initialize(opts) | |
@col_names = opts.fetch(:cols) | |
@records = opts.fetch(:records) | |
@model_name = rows.table_name.singularize | |
end | |
def headers | |
@col_names.map { |name| t_col(name) } | |
end | |
def rows | |
@records | |
end | |
def cols(record) | |
@col_names.map { |name| record.public_send(name) } | |
end | |
def t_col(col_name) | |
I18n.t("activerecord.attributes.#{@model_name}.#{col_name}", default: col_name.titleize) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment