Module: Datagrid::Columns::ClassMethods

Defined in:
lib/datagrid/columns.rb

Instance Method Summary collapse

Instance Method Details

#column(name, query = nil, **options, &block) ⇒ Datagrid::Columns::Column

Defines a new datagrid column

Parameters:

  • name (Symbol)

    column name

  • query (String, nil) (defaults to: nil)

    a string representing the query to select this column (supports only ActiveRecord)

  • block (Block)

    proc to calculate a column value

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • html (Boolean, String)

    Determines if the column should be present in the HTML table and how it is formatted.

  • order (String, Array<Symbol>)

    Determines if the column can be sortable and specifies the ORM ordering method. Example: "created_at, id" for ActiveRecord, [:created_at, :id] for Mongoid.

  • order_desc (String)

    Specifies a descending order for the column (used when :order cannot be easily reversed by the ORM).

  • order_by_value (Boolean, Proc)

    Enables Ruby-level ordering for the column. Warning: Sorting large datasets in Ruby is not recommended. If true, Datagrid orders by the column value. If a block is provided, Datagrid orders by the block's return value.

  • mandatory (Boolean)

    If true, the column will never be hidden by the #column_names selection.

  • before (Symbol)

    Places the column before the specified column when determining order.

  • after (Symbol)

    Places the column after the specified column when determining order.

  • if (Boolean, Proc)

    conditions when a column is available.

  • unless (Boolean, Proc)

    conditions when a column is not available.

  • preload (Symbol, Array<Symbol>)

    Specifies associations to preload for the column within the scope.

  • tag_options (Hash)

    Specifies HTML attributes for the <td> or <th> of the column. Example: { class: "content-align-right", "data-group": "statistics" }.

Returns:


287
288
289
# File 'lib/datagrid/columns.rb', line 287

def column(name, query = nil, **options, &block)
  define_column(columns_array, name, query, **options, &block)
end

#column_by_name(name) ⇒ Datagrid::Columns::Column?

Returns column definition with given name

Returns:


293
294
295
# File 'lib/datagrid/columns.rb', line 293

def column_by_name(name)
  find_column_by_name(columns_array, name)
end

#column_namesArray<Datagrid::Columns::Column>

Returns an array of all defined column names

Returns:


299
300
301
# File 'lib/datagrid/columns.rb', line 299

def column_names
  columns.map(&:name)
end

#columns(*column_names, data: false, html: false) ⇒ Array<Datagrid::Columns::Column>

Returns column definition objects.

Examples:

GridClass.columns(:id, :name)

Parameters:

  • data (Boolean) (defaults to: false)

    if true returns only columns with data representation. Default: false.

  • html (Boolean) (defaults to: false)

    if true returns only columns with html columns. Default: false.

  • column_names (Array<String>)

    list of column names if you want to limit data only to specified columns

Returns:


258
259
260
# File 'lib/datagrid/columns.rb', line 258

def columns(*column_names, data: false, html: false)
  filter_columns(columns_array, *column_names, data: data, html: html)
end

#decorate(model = nil, &block) ⇒ void

This method returns an undefined value.

Defines a model decorator that will be used to define a column value. All column blocks will be given a decorated version of the model.

Examples:

Wrapping a model with presenter

decorate { |user| UserPresenter.new(user) }

A shortcut for doing the same

decorate { UserPresenter }

340
341
342
343
344
345
346
347
348
349
350
# File 'lib/datagrid/columns.rb', line 340

def decorate(model = nil, &block)
  if !model && !block
    raise ArgumentError, "decorate needs either a block to define decoration or a model to decorate"
  end
  return self.decorator = block unless model
  return model unless decorator

  presenter = ::Datagrid::Utils.apply_args(model, &decorator)
  presenter = presenter.new(model) if presenter.is_a?(Class)
  block_given? ? yield(presenter) : presenter
end

#format(value, &block) ⇒ Datagrid::Columns::Column::ResponseFormat

Formats column value for HTML. Helps to distinguish formatting as plain data and HTML

Examples:

column(:name) do |model|
  format(model.name) do |value|
    tag.strong(value)
  end
end

Parameters:

  • value (Object)

    Value to be formatted

Returns:


318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/datagrid/columns.rb', line 318

def format(value, &block)
  if block_given?
    respond_to do |f|
      f.data { value }
      f.html do
        instance_exec(value, &block)
      end
    end
  else
    # Ruby Object#format exists.
    # We don't want to change the behaviour and overwrite it.
    super
  end
end