class Lolita::Configuration::Column

column in a list

Examples

lolita do

list do
  column :title, sortable: true
  column :full_name, sortable: 'first_name'
  column :is_public do
    formatter do |value, record, view|
      value ? 'Yes' : 'No'
    end
  end

  column do
    title "ID"
    name :id
    formatter do|value, record, view|
      view.link_to(value, view.url_for(action: 'edit', id: value))
    end
  end

  column :updated_at, title: 'Date', formatter: '%d.%m.%Y., %H:%M'
end

end

Constants

MAX_TEXT_SIZE

Attributes

list_association_name[R]

Public Class Methods

new(dbi, *args, &block) click to toggle source
# File lib/lolita/configuration/column.rb, line 32
def initialize(dbi, *args, &block)
  set_and_validate_dbi(dbi)
  set_attributes(*args)
  instance_eval(&block) if block_given?
  validate
  normalize_attributes
  detect_association
end

Public Instance Methods

current_sort_state(params) click to toggle source

Find if any of received sort options matches this column.

# File lib/lolita/configuration/column.rb, line 88
def current_sort_state(params)
  @sortable && sort_pairs(params).find { |pair| pair[0] == sort_by_name } || []
end
formatted_value(record, view) click to toggle source
# File lib/lolita/configuration/column.rb, line 71
def formatted_value(record, view)
  formatter.with(value(record), record, view)
end
formatter(value = nil, &block) click to toggle source

Define format, for details see Lolita::Support::Formatter and Lolita::Support::Formater::Rails

# File lib/lolita/configuration/column.rb, line 130
def formatter(value = nil, &block)
  if block_given?
    @formatter = Lolita::Support::Formatter.new(value, &block)
  elsif value || !@formatter
    if value.is_a?(Lolita::Support::Formatter)
      @formatter = value
    else
      @formatter = Lolita::Support::Formatter::Rails.new(value)
    end
  end
  @formatter
end
formatter=(value) click to toggle source
# File lib/lolita/configuration/column.rb, line 143
def formatter=(value)
  if value.is_a?(Lolita::Support::Formatter)
    @formatter = value
  else
    @formatter = Lolita::Support::Formatter::Rails.new(value)
  end
end
list(*args, &block) click to toggle source
# File lib/lolita/configuration/column.rb, line 41
def list(*args, &block)
  if args && args.any? || block_given?
    detect_association
    list_association = args[0] && @dbi.associations[args[0].to_s.to_sym] || association
    list_dbi = list_association && Lolita::DBI::Base.create(list_association.klass)
    fail Lolita::UnknownDBPError.new("DBI is not specified for list in column #{self}") unless list_dbi
    @list_association_name = list_association.name
    Lolita::LazyLoader.lazy_load(self, :@list, Lolita::Configuration::NestedList, list_dbi, self, association_name: list_association.name, &block)
  else
    @list
  end
end
set_attributes(*args) click to toggle source
# File lib/lolita/configuration/column.rb, line 151
def set_attributes(*args)
  options = args ? args.extract_options! : {}
  if args[0].respond_to?(:field)
    [:name, :type].each do |attr|
      send(:"#{attr}=", args[0].send(attr))
    end
  elsif args[0]
    self.name = args[0]
  end
  options.each do |attr_name, value|
    send(:"#{attr_name}=", value)
  end
end
sort_by_name() click to toggle source

returns value to sort by in default it will be column name, but you can specify it in field configuration

Examples

list do

field :name, sortable: 'some_table.first_name'

end

# File lib/lolita/configuration/column.rb, line 120
def sort_by_name
  @sortable.is_a?(TrueClass) ? name.to_s : @sortable.to_s
end
sort_pairs(params) click to toggle source

Create array of sort information from params.

# File lib/lolita/configuration/column.rb, line 125
def sort_pairs(params)
  (params[:s] || '').split('|').map { |pair| pair.split(',') }
end
sort_params(params) click to toggle source

Return string with sort options for column if column is sortable.

# File lib/lolita/configuration/column.rb, line 93
def sort_params(params)
  if @sortable
    pairs = sort_pairs(params)
    found_pair = false
    pairs.each_with_index do|pair, index|
      if pair[0] == sort_by_name
        pairs[index][1] = pair[1] == 'asc' ? 'desc' : 'asc'
        found_pair = true
      end
    end
    unless found_pair
      pairs << [sort_by_name, 'asc']
    end
    (pairs.map { |pair| pair.join(',') }).join('|')
  else
    ''
  end
end
sortable?() click to toggle source
# File lib/lolita/configuration/column.rb, line 83
def sortable?
  @sortable
end
title(new_title = nil) click to toggle source

Set/Get title. Getter return title what was set or ask for human_attribute_name to model.

# File lib/lolita/configuration/column.rb, line 76
def title(new_title = nil)
  if new_title
    @title = new_title
  end
  Lolita::Utils.dynamic_string(@title, default: @name && @dbi.klass.human_attribute_name(@name))
end
value(record) click to toggle source

Return value of column from given record. When record matches foreign key patter, then foreign key is used. In other cases it just ask for attribute with same name as column.

# File lib/lolita/configuration/column.rb, line 56
def value(record)
  if association
    if association.macro == :one &&  dbi.klass.respond_to?(:human_attribute_name)
      dbi.klass.human_attribute_name(association.name)
      # dbi.record(record.send(association.name)).title
    elsif dbi.klass.respond_to?(:human_attribute_name)
      "#{dbi.klass.human_attribute_name(association.name)} (#{record.send(association.name).count})"
    else
      "#{association.name} (#{record.send(association.name).count})"
    end
  else
    record.send(name)
  end
end

Private Instance Methods

detect_association() click to toggle source
# File lib/lolita/configuration/column.rb, line 167
def detect_association
  @association ||= dbi.associations[self.name.to_s]
end
normalize_attributes() click to toggle source
# File lib/lolita/configuration/column.rb, line 171
def normalize_attributes
  @name = @name.to_sym
end
validate() click to toggle source
# File lib/lolita/configuration/column.rb, line 175
def validate
  fail Lolita::UnknownDBIError.new("DBI is not specified for column #{self}") unless dbi
  fail ArgumentError.new('Column must have name.') unless name
end