lib/action_table/view.rb in action_table-0.3.0 vs lib/action_table/view.rb in action_table-0.4.0

- old
+ new

@@ -1,33 +1,28 @@ # frozen_string_literal: true require 'set' -require 'action_table/bootstrap_styles' module ActionTable class View include ActionView::Helpers::UrlHelper include ActionTable.config.rails_host_app.routes.url_helpers - attr_reader :model_name, :rows, :styles + attr_reader :rows def initialize( columns:, records:, paginate: false, links: ActionTable.config.links, - actions: ActionTable.config.actions, - styles: ActionTable.config.styles + actions: ActionTable.config.actions ) - @columns = columns.map(&:to_s) + @columns = Array(columns).map(&:to_s) @rows = records - @table_name = records.table_name - @model_name = @table_name.singularize @paginate = paginate @links = Set.new(Array(links).map(&:to_s)).reject(&:empty?) @actions = Array(actions).map(&:to_s) - @styles = BootstrapStyles.new(styles) end def headers @headers ||= @columns.map { |name| t_col(name) } end @@ -63,30 +58,49 @@ "#{model_name}_page" end # This must be defined for record_path to work def controller - @table_name + table_name end # Contstruct path to record, i.e programs_path(record) def record_path(record, action: nil) action = nil if action.to_s == 'show' path = [action, model_name, 'path'].compact.join('_') public_send(path, record) end + private + def t_actions @actions.map { |name| t_action(name) } end def t_action(name) name = name.to_s I18n.t("actions.#{name}", default: name.titleize) end def t_col(col_name) - t_key = "activerecord.attributes.#{@model_name}.#{col_name}" - I18n.t(t_key, default: col_name.titleize) + if model_name? + t_key = "activerecord.attributes.#{model_name}.#{col_name}" + return I18n.t(t_key, default: col_name.titleize) + end + + col_name.humanize.titleize end + + def table_name + rows.table_name + end + + def model_name + table_name.singularize + end + + def table_name? + rows.respond_to?(:table_name) + end + alias_method :model_name?, :table_name? end end