module Datagrid::Columns::ClassMethods
Public Instance Methods
Defines 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 options [Hash<Symbol, Object>] hash of options @param block [Block] proc to calculate a column value @return [Datagrid::Columns::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 withcolumn_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
@see github.com/bogdan/datagrid/wiki/Columns
# File lib/datagrid/columns.rb, line 90 def column(name, query = nil, **options, &block) define_column(columns_array, name, query, **options, &block) end
Returns column definition with given name @return [Datagrid::Columns::Column, nil]
# File lib/datagrid/columns.rb, line 96 def column_by_name(name) find_column_by_name(columns_array, name) end
Returns an array of all defined column names @return [Array<Datagrid::Columns::Column>]
# File lib/datagrid/columns.rb, line 102 def column_names columns.map(&:name) 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)
# File lib/datagrid/columns.rb, line 57 def columns(*column_names, data: false, html: false) filter_columns(columns_array, *column_names, data: data, html: html) 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
decorate { |user| UserPresenter.new(user) } decorate { UserPresenter } # a shortcut
# File lib/datagrid/columns.rb, line 143 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
@!visibility private
# File lib/datagrid/columns.rb, line 175 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, default_column_options.merge(options), &block ) columns.insert(position, column) column end
@!visibility private
# File lib/datagrid/columns.rb, line 161 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
# File lib/datagrid/columns.rb, line 189 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
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| content_tag(:strong, value) end end
# File lib/datagrid/columns.rb, line 121 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
@!visibility private
# File lib/datagrid/columns.rb, line 155 def inherited(child_class) super(child_class) child_class.columns_array = self.columns_array.clone end
@!visibility private
# File lib/datagrid/columns.rb, line 107 def respond_to(&block) Datagrid::Columns::Column::ResponseFormat.new(&block) end