Class: Lolita::Configuration::Column

Inherits:
Base show all
Defined in:
lib/lolita/configuration/column.rb

Overview

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

Constant Summary collapse

MAX_TEXT_SIZE =
20

Instance Attribute Summary collapse

Attributes inherited from Base

#component, #dbi

Instance Method Summary collapse

Methods included from Builder

#build, #builder, #builder=, #builder_default_name, #builder_default_options, #builder_default_state

Constructor Details

#initialize(dbi, *args, &block) ⇒ Column

Returns a new instance of Column.



32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#list_association_nameObject (readonly)

Returns the value of attribute list_association_name.



29
30
31
# File 'lib/lolita/configuration/column.rb', line 29

def list_association_name
  @list_association_name
end

Instance Method Details

#current_sort_state(params) ⇒ Object

Find if any of received sort options matches this column.



88
89
90
# 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) ⇒ Object



71
72
73
# File 'lib/lolita/configuration/column.rb', line 71

def formatted_value(record, view)
  formatter.with(value(record), record, view)
end

#formatter(value = nil, &block) ⇒ Object

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



130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object



143
144
145
146
147
148
149
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# 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_nameObject

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



120
121
122
# 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) ⇒ Object

Create array of sort information from params.



125
126
127
# File 'lib/lolita/configuration/column.rb', line 125

def sort_pairs(params)
  (params[:s] || '').split('|').map { |pair| pair.split(',') }
end

#sort_params(params) ⇒ Object

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/lolita/configuration/column.rb', line 83

def sortable?
  @sortable
end

#title(new_title = nil) ⇒ Object

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



76
77
78
79
80
81
# 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) ⇒ Object

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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