Skip to content

Instantly share code, notes, and snippets.

@buren
Created July 11, 2015 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buren/711c5497e2bb432b5491 to your computer and use it in GitHub Desktop.
Save buren/711c5497e2bb432b5491 to your computer and use it in GitHub Desktop.
ActiveTableView: Render "tables structure" for ActiveRecord objects
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