Sha256: c7b7f6ffcfa4f79328d9073fb934d09e832f2662801ac7b0f75305fe9dda14ea

Contents?: true

Size: 1.95 KB

Versions: 12

Compression:

Stored size: 1.95 KB

Contents

module Coprl
  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

12 entries across 12 versions & 1 rubygems

Version Path
coprl-3.0.0.beta.12 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.11 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.10 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.9 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.8 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.7 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.6 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.5 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.4 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.3 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.2 lib/coprl/presenters/helpers/rails/model_table.rb
coprl-3.0.0.beta.1 lib/coprl/presenters/helpers/rails/model_table.rb