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 new datagrid column

Available options:

  • html - determines if current column should be present in html table and how is it formatted

  • order - determines if this column could be sortable and how. The value of order is explicitly passed to ORM ordering method. Ex: "created_at, id" for ActiveRecord, [:created_at, :id] for Mongoid

  • order_desc - determines a descending order for given column (only in case when :order can not be easily reversed by ORM)

  • order_by_value - used in case it is easier to perform ordering at ruby level not on database level. Warning: using ruby to order large datasets is very unrecommended. If set to true - datagrid will use column value to order by this column If block is given - datagrid will use value returned from block

  • mandatory - if true, column will never be hidden with #column_names selection

  • url - a proc with one argument, pass this option to easily convert the value into an URL

  • before - determines the position of this column, by adding it before the column passed here

  • after - determines the position of this column, by adding it after the column passed here

  • if - the column is shown if the reult of calling this argument is true

  • unless - the column is shown unless the reult of calling this argument is true

  • preload - spefies which associations of the scope should be preloaded for this column

Parameters:

  • name (Symbol)

    column name

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

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

  • options (Hash<Symbol, Object>)

    hash of options

  • block (Block)

    proc to calculate a column value

Returns:

See Also:



91
92
93
# File 'lib/datagrid/columns.rb', line 91

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:



97
98
99
# File 'lib/datagrid/columns.rb', line 97

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:



103
104
105
# File 'lib/datagrid/columns.rb', line 103

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:



58
59
60
# File 'lib/datagrid/columns.rb', line 58

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:

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

decorate { UserPresenter } # a shortcut


144
145
146
147
148
149
150
151
152
153
# File 'lib/datagrid/columns.rb', line 144

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.is_a?(Class) ?  presenter.new(model) : presenter
  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|
    (:strong, value)
  end
end

Parameters:

  • value (Object)

    Value to be formatted

Returns:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/datagrid/columns.rb', line 122

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