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

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>, Proc)

    Determines if the column can be sortable and specifies the ORM ordering method. Example: "created_at, id" for ActiveRecord, [:created_at, :id] for Mongoid. Can be a Proc accepting a scope argument and returning a scope with applied order

  • order_desc (String, Array<Symbol>, Proc)

    Specifies a descending order for the column. See order option for context. 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 (Symbol, Proc)

    conditions when a column is available, can be a method name or a proc with grid argument returning Boolean.

  • unless (Symbol, Proc)

    conditions when a column is not available, can be a method name or a proc with grid argument returning Boolean.

  • 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" }.



289
290
291
# File 'lib/datagrid/columns.rb', line 289

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



295
296
297
# File 'lib/datagrid/columns.rb', line 295

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



301
302
303
# File 'lib/datagrid/columns.rb', line 301

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)


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 }


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

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


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

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