Class: Lolita::Configuration::Column
- 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
-
#list_association_name ⇒ Object
readonly
Returns the value of attribute list_association_name.
Attributes inherited from Base
Instance Method Summary collapse
-
#current_sort_state(params) ⇒ Object
Find if any of received sort options matches this column.
- #formatted_value(record, view) ⇒ Object
-
#formatter(value = nil, &block) ⇒ Object
Define format, for details see Lolita::Support::Formatter and Lolita::Support::Formater::Rails.
- #formatter=(value) ⇒ Object
-
#initialize(dbi, *args, &block) ⇒ Column
constructor
A new instance of Column.
- #list(*args, &block) ⇒ Object
- #set_attributes(*args) ⇒ Object
-
#sort_pairs(params) ⇒ Object
Create array of sort information from params.
-
#sort_params(params) ⇒ Object
Return string with sort options for column if column is sortable.
- #sortable? ⇒ Boolean
-
#title(new_title = nil) ⇒ Object
Set/Get title.
-
#value(record) ⇒ Object
Return value of column from given record.
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.
33 34 35 36 37 38 39 40 |
# File 'lib/lolita/configuration/column.rb', line 33 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_name ⇒ Object (readonly)
Returns the value of attribute list_association_name.
30 31 32 |
# File 'lib/lolita/configuration/column.rb', line 30 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.
89 90 91 |
# File 'lib/lolita/configuration/column.rb', line 89 def current_sort_state(params) @sortable && sort_pairs(params).detect{|pair| pair[0]==self.name.to_s} || [] end |
#formatted_value(record, view) ⇒ Object
72 73 74 |
# File 'lib/lolita/configuration/column.rb', line 72 def formatted_value(record,view) self.formatter.with(self.value(record), record, view) end |
#formatter(value = nil, &block) ⇒ Object
Define format, for details see Lolita::Support::Formatter and Lolita::Support::Formater::Rails
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/lolita/configuration/column.rb', line 119 def formatter(value = nil,&block) if block_given? @formatter = Lolita::Support::Formatter.new(value,&block) elsif value || !@formatter if value.kind_of?(Lolita::Support::Formatter) @formatter = value else @formatter = Lolita::Support::Formatter::Rails.new(value) end end @formatter end |
#formatter=(value) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/lolita/configuration/column.rb', line 132 def formatter=(value) if value.kind_of?(Lolita::Support::Formatter) @formatter = value else @formatter = Lolita::Support::Formatter::Rails.new(value) end end |
#list(*args, &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lolita/configuration/column.rb', line 42 def list *args, &block if args && args.any? || block_given? detect_association list_association = args[0] && @dbi.associations[args[0].to_s.to_sym] || self.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
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/lolita/configuration/column.rb', line 140 def set_attributes(*args) = args ? args. : {} 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 .each do |attr_name,value| send(:"#{attr_name}=",value) end end |
#sort_pairs(params) ⇒ Object
Create array of sort information from params.
114 115 116 |
# File 'lib/lolita/configuration/column.rb', line 114 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lolita/configuration/column.rb', line 94 def sort_params params if @sortable pairs = sort_pairs(params) found_pair = false pairs.each_with_index{|pair,index| if pair[0] == self.name.to_s pairs[index][1] = pair[1] == "asc" ? "desc" : "asc" found_pair = true end } unless found_pair pairs << [self.name.to_s,"asc"] end (pairs.map{ |pair| pair.join(",") }).join("|") else "" end end |
#sortable? ⇒ Boolean
84 85 86 |
# File 'lib/lolita/configuration/column.rb', line 84 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.
77 78 79 80 81 82 |
# File 'lib/lolita/configuration/column.rb', line 77 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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lolita/configuration/column.rb', line 57 def value(record) if self.association if self.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(self.name) end end |