module Datagrid::Columns::ClassMethods
Public Instance Methods
Source
# File lib/datagrid/columns.rb, line 287 def column(name, query = nil, **options, &block) define_column(columns_array, name, query, **options, &block) end
Defines a new datagrid column @param name [Symbol] column name @param query [String, nil] a string representing the query to select this column (supports only ActiveRecord) @param block [Block] proc to calculate a column value @option options [Boolean, String] html Determines if the column should be present
in the HTML table and how it is formatted.
@option options [String, Array<Symbol>] order Determines if the column can be sortable and
specifies the ORM ordering method. Example: `"created_at, id"` for ActiveRecord, `[:created_at, :id]` for Mongoid.
@option options [String] order_desc Specifies a descending order for the column
(used when `:order` cannot be easily reversed by the ORM).
@option options [Boolean, Proc] order_by_value 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.
@option options [Boolean] mandatory If ‘true`, the column will never be hidden by the `#column_names` selection. @option options [Symbol] before Places the column before the specified column when determining order. @option options [Symbol] after Places the column after the specified column when determining order. @option options [Boolean, Proc] if conditions when a column is available. @option options [Boolean, Proc] unless conditions when a column is not available. @option options [Symbol, Array<Symbol>] preload Specifies associations
to preload for the column within the scope.
@option options [Hash] tag_options Specifies HTML attributes for the ‘<td>` or `<th>` of the column.
Example: `{ class: "content-align-right", "data-group": "statistics" }`.
@return [Datagrid::Columns::Column]
Source
# File lib/datagrid/columns.rb, line 293 def column_by_name(name) find_column_by_name(columns_array, name) end
Returns column definition with given name @return [Datagrid::Columns::Column, nil]
Source
# File lib/datagrid/columns.rb, line 299 def column_names columns.map(&:name) end
Returns an array of all defined column names @return [Array<Datagrid::Columns::Column>]
Source
# 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
@param data [Boolean] if true returns only columns with data representation. Default: false. @param html [Boolean] if true returns only columns with html columns. Default: false. @param column_names
[Array<String>] list of column names if you want to limit data only to specified columns @return [Array<Datagrid::Columns::Column>] column definition objects @example
GridClass.columns(:id, :name)
Source
# 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
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. @return [void] @example Wrapping a model with presenter
decorate { |user| UserPresenter.new(user) }
@example A shortcut for doing the same
decorate { UserPresenter }
Source
# File lib/datagrid/columns.rb, line 374 def define_column(columns, name, query = nil, **options, &block) check_scope_defined!("Scope should be defined before columns") block ||= lambda do |model| model.public_send(name) end position = Datagrid::Utils.extract_position_from_options(columns, options) column = Datagrid::Columns::Column.new( self, name, query, options, &block ) columns.insert(position, column) column end
@!visibility private
Source
# File lib/datagrid/columns.rb, line 359 def filter_columns(columns_array, *names, data: false, html: false) names.compact! if names.size >= 1 && names.all? { |n| n.is_a?(Datagrid::Columns::Column) && n.grid_class == self.class } return names end names.map!(&:to_sym) columns_array.select do |column| (!data || column.data?) && (!html || column.html?) && (column.mandatory? || names.empty? || names.include?(column.name)) end end
@!visibility private
Source
# File lib/datagrid/columns.rb, line 389 def find_column_by_name(columns, name) return name if name.is_a?(Datagrid::Columns::Column) columns.find do |col| col.name.to_sym == name.to_sym end end
@!visibility private
Source
# 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
Formats column value for HTML. Helps to distinguish formatting as plain data and HTML @param value [Object] Value to be formatted @return [Datagrid::Columns::Column::ResponseFormat] Format object @example
column(:name) do |model| format(model.name) do |value| tag.strong(value) end end
Source
# File lib/datagrid/columns.rb, line 353 def inherited(child_class) super child_class.columns_array = columns_array.clone end
@!visibility private
Source
# File lib/datagrid/columns.rb, line 304 def respond_to(&block) Datagrid::Columns::Column::ResponseFormat.new(&block) end
@!visibility private