Class: Fancygrid::Column

Inherits:
Node
  • Object
show all
Defined in:
lib/fancygrid/column.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #name, #parent, #resource_class, #root, #table_name

Instance Method Summary collapse

Methods inherited from Node

#attributes, #column, #columns, #columns_for, #name_chain, #root?

Constructor Details

#initialize(parent, name, options = {}) ⇒ Column

Returns a new instance of Column.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fancygrid/column.rb', line 38

def initialize(parent, name, options = {})
  super(parent, name, options)
  
  @position        = options.fetch(:position, 0)
  @width           = options.fetch(:width, nil)
  @hidden          = options.fetch(:hidden, false)
  self.visible     = options.fetch(:visible, self.visible)
  
  @searchable      = options.fetch(:searchable, false)
  @search_value    = options.fetch(:search_value, nil)
  @search_operator = options.fetch(:search_operator, nil)
  @search_options  = options.fetch(:search_options, nil)
  
  @selectable      = options.fetch(:selectable, false)
  @value_proc      = options.fetch(:value_proc, nil)
  
  @human_name      = options.fetch(:human_name, nil)

  @th_class        = options.fetch(:th_class, "")
  @td_class        = options.fetch(:td_class, "")
  
  @formatable      = self.root.respond_to?(self.formatter_method)
end

Instance Attribute Details

#formatableObject

If true, the root node has a formatter method for this column.



33
34
35
# File 'lib/fancygrid/column.rb', line 33

def formatable
  @formatable
end

#hiddenObject

If true, this column is not rendered.



12
13
14
# File 'lib/fancygrid/column.rb', line 12

def hidden
  @hidden
end

#positionObject

The column position in table.



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

def position
  @position
end

#search_operatorObject

The current search operator for this column.



22
23
24
# File 'lib/fancygrid/column.rb', line 22

def search_operator
  @search_operator
end

#search_optionsObject

The possible search options for this column.



25
26
27
# File 'lib/fancygrid/column.rb', line 25

def search_options
  @search_options
end

#search_valueObject

The current search value for this column.



19
20
21
# File 'lib/fancygrid/column.rb', line 19

def search_value
  @search_value
end

#searchableObject

If true, a search field is then rendered for this column.



16
17
18
# File 'lib/fancygrid/column.rb', line 16

def searchable
  @searchable
end

#selectableObject

If true, this column is treated as a database column. SQL queries may use the columns name for SELECT statements.



30
31
32
# File 'lib/fancygrid/column.rb', line 30

def selectable
  @selectable
end

#value_procObject

Value resolver proc for this column.



36
37
38
# File 'lib/fancygrid/column.rb', line 36

def value_proc
  @value_proc
end

#widthObject

The column width.



9
10
11
# File 'lib/fancygrid/column.rb', line 9

def width
  @width
end

Instance Method Details

#collect_columns(collection) ⇒ Object

Adds this column to the given collection



163
164
165
# File 'lib/fancygrid/column.rb', line 163

def collect_columns collection
  collection << self
end

#default_human_nameObject

Gets the default human name for this column



118
119
120
121
122
123
124
125
# File 'lib/fancygrid/column.rb', line 118

def default_human_name
  result = self.name.to_s.humanize
  if self.resource_class.respond_to? :human_attribute_name
    self.resource_class.human_attribute_name(self.name, :default => result)
  else
    result
  end
end

#fetch_value(record) ⇒ Object

Fetches a value from given record.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fancygrid/column.rb', line 135

def fetch_value record
  if self.value_proc
    return value_proc.call(record) 
  else
    chain = self.name_chain.split(".")
    chain.shift

    value = record
    while token = chain.shift
      value = (value.respond_to?(token) ? value.send(token) : nil)
      return nil if value.nil?
    end
    
    return value
  end
end

#fetch_value_and_format(record) ⇒ Object

Fetches a value from given record and tries to apply the format method



155
156
157
158
159
# File 'lib/fancygrid/column.rb', line 155

def fetch_value_and_format record
  value = fetch_value(record)
  value = self.root.send(self.formatter_method, value) if self.formatable
  return value
end

#formatter_methodObject

Gets the method name that is send to the root node to format the value of a column cell



85
86
87
# File 'lib/fancygrid/column.rb', line 85

def formatter_method
  @formatter_method or @formatter_method = "format_" + self.identifier.gsub(".", "_")
end

#human_nameObject

Gets the internationalized, human readable name for this column.



129
130
131
# File 'lib/fancygrid/column.rb', line 129

def human_name
  @human_name or @human_name = I18n.t(self.i18n_path, :default => default_human_name)
end

#i18n_pathObject

Gets the internationalization lookup path for this column.



112
113
114
# File 'lib/fancygrid/column.rb', line 112

def i18n_path
  @i18n_path or @i18n_path = [Fancygrid.i18n_scope, :tables, self.name_chain].join(".")
end

#identifierObject

Gets the column identifier. This is the #table_name and #name joined with a ‘.’ (dot)



92
93
94
# File 'lib/fancygrid/column.rb', line 92

def identifier
  @identifier or @identifier = [self.table_name, self.name].join('.')
end

#sort_orderObject

Gets the current sort order for this column.



78
79
80
# File 'lib/fancygrid/column.rb', line 78

def sort_order
  self.root.view_state.column_order(self)
end

#tag_classObject

Gets a whitespace separated string that is used as css class for the table column. The string contains the #table_name and the #name of this column.



100
101
102
# File 'lib/fancygrid/column.rb', line 100

def tag_class
  @tag_class or @tag_class = self.identifier.split(".").join(" ")
end

#td_class(record) ⇒ Object



107
108
# File 'lib/fancygrid/column.rb', line 107

def td_class(record)
end

#th_class(record) ⇒ Object



104
105
# File 'lib/fancygrid/column.rb', line 104

def th_class(record)
end

#visibleObject

Gets a value indicating whether this column is visible and should be rendered or not.



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

def visible
  !@hidden
end

#visible=(value) ⇒ Object

Sets a value indication whether this column is visible and should be rendered or not.



72
73
74
# File 'lib/fancygrid/column.rb', line 72

def visible=(value)
  @hidden = !value
end