Sha256: 1905893362c741e25ee95ae248144a6bdd726b98240b5a81f71345e015570a26

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

module Voom
  module Presenters
    module Helpers
      module Rails
        module ModelTable
          # Build a a table from a Rails model
          def table_for(query_,
                        empty_icon: 'far fa-folder-open fa-rotate-180',
                        except: [:id, :created_at, :updated_at],
                        timezone: nil, # The timezone to covnert _at and _on values
                        selectable: false, &block)
            row1 = query_.first
            if row1
              __columns__ = row1.class.columns
              table selectable: selectable do
                _table_header_(__columns__, except)

                query_.each do |item_|
                  row do
                    _table_row_(__columns__, block, except, item_, timezone: timezone)
                  end
                end
              end
            else
              icon empty_icon, size: '6rem' if empty_icon
              body "You have no #{query_.arel_table.name.pluralize.humanize}"
            end
          end


          private
          def _table_row_(__columns__, block, except, item_, timezone: nil)
            __columns__.each do |col|
              next if except.include?(col.name.to_sym)
                value = format_value(col.name,item_.send(col.name.to_sym), timezone: timezone)
              column value do
                self.instance_exec(item_, col.name.to_sym, &block) if block
              end
            end
          end

          def format_value(col_name, value, timezone:)
            return format_time_long(value, timezone: timezone) if timezone &&
                col_name.ends_with?('_at') || col_name.ends_with?('_on')
            value
          end

          def _table_header_(__columns__, except)
            header do
              __columns__.each do |col|
                column col.name.titleize unless except.include?(col.name.to_sym)
              end
            end
          end
        end

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
voom-presenters-2.1.2 lib/voom/presenters/helpers/rails/model_table.rb
voom-presenters-2.1.0 lib/voom/presenters/helpers/rails/model_table.rb
voom-presenters-2.0.3 lib/voom/presenters/helpers/rails/model_table.rb
voom-presenters-2.0.2 lib/voom/presenters/helpers/rails/model_table.rb
voom-presenters-2.0.1 lib/voom/presenters/helpers/rails/model_table.rb
voom-presenters-2.0.0 lib/voom/presenters/helpers/rails/model_table.rb
voom-presenters-0.2.0 lib/voom/presenters/helpers/rails/model_table.rb