Class: Lolita::Configuration::Column

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

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.



9
10
11
12
13
14
15
16
# File 'lib/lolita/configuration/column.rb', line 9

def initialize(dbi,*args,&block)
  set_and_validate_dbi(dbi)
  self.set_attributes(*args)
  self.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.



6
7
8
# File 'lib/lolita/configuration/column.rb', line 6

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.



65
66
67
# File 'lib/lolita/configuration/column.rb', line 65

def current_sort_state(params)
  @sortable && sort_pairs(params).detect{|pair| pair[0]==self.name.to_s} || []
end

#formatted_value(record, view) ⇒ Object



48
49
50
# File 'lib/lolita/configuration/column.rb', line 48

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

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

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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lolita/configuration/column.rb', line 95

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



108
109
110
111
112
113
114
# File 'lib/lolita/configuration/column.rb', line 108

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



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lolita/configuration/column.rb', line 18

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)
    raise 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



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lolita/configuration/column.rb', line 116

def set_attributes(*args)
  options = args ? args.extract_options! : {}
  if args[0].respond_to?(:field)
    [:name,:type].each do |attr|
      self.send(:"#{attr}=",args[0].send(attr))
    end
  elsif args[0]
    self.name = args[0]
  end
  options.each do |attr_name,value|
    self.send(:"#{attr_name}=",value)
  end
end

#sort_pairs(params) ⇒ Object

Create array of sort information from params.



90
91
92
# File 'lib/lolita/configuration/column.rb', line 90

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lolita/configuration/column.rb', line 70

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/lolita/configuration/column.rb', line 60

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.



53
54
55
56
57
58
# File 'lib/lolita/configuration/column.rb', line 53

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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lolita/configuration/column.rb', line 33

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