Class: Netzke::Basepack::ColumnConfig

Inherits:
AttrConfig
  • Object
show all
Defined in:
lib/netzke/basepack/column_config.rb

Overview

Takes care of automatic column configuration in Grid::Base

Constant Summary

COMMON_DEFAULTS =

These config options can be ommitted from config, as they are assumed by default at the JS side

{
  virtual: false,
  sortable: true,
  filterable: true,
  width: 100,
  hidden: false,
  assoc: false
}

Instance Method Summary collapse

Methods inherited from AttrConfig

#association?, #initialize, #primary?, #set_read_only

Constructor Details

This class inherits a constructor from Netzke::Basepack::AttrConfig

Instance Method Details

#editor_for_type(type) ⇒ Object

Column editor config for attribute type.



94
95
96
# File 'lib/netzke/basepack/column_config.rb', line 94

def editor_for_type(type)
  {xtype: type_to_editor_xtype(type)}
end

#merge_attribute(attr) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/netzke/basepack/column_config.rb', line 15

def merge_attribute(attr)
  self.merge!(attr)

  self.text = delete(:label) if self.has_key?(:label)

  self.merge!(delete(:column_config)) if self.has_key?(:column_config)

  self.delete(:field_config) if self.has_key?(:field_config)
end

#remove_defaultsObject

Returns self

Returns:

  • self



121
122
123
124
# File 'lib/netzke/basepack/column_config.rb', line 121

def remove_defaults
  COMMON_DEFAULTS.each_pair {|k,v| self.delete(k) if v == self[k]}
  self
end

#set_default_association_editorObject

Detects an association column and sets up the proper editor.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/netzke/basepack/column_config.rb', line 80

def set_default_association_editor
  assoc, assoc_method = name.split('__')

  assoc_method_type = @model_adapter.get_assoc_property_type assoc, assoc_method

  # if association column is boolean, display a checkbox (or alike), otherwise - a combobox (or alike)
  if nested_attribute
    self.editor = editor_for_type(assoc_method_type)
  else
    self.editor = assoc_method_type == :boolean ? editor_for_type(:boolean) : {xtype: :netzkeremotecombo}
  end
end

#set_defaultsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/netzke/basepack/column_config.rb', line 25

def set_defaults
  super

  self.type ||= @model_adapter.attr_type(name)

  set_xtype if xtype.nil?

  self.virtual = @model_adapter.virtual_attribute?(self)

  self.text ||= label || default_label

  set_editor

  set_width if width.nil?

  self.hidden = primary? if hidden.nil?

  self.sortable = !virtual || !sorting_scope.nil? if sortable.nil?

  self.filterable = !virtual || !filter_with.nil? if filterable.nil?

  self.assoc = association? # used at the JS side

  remove_defaults # options that are implied by Ext JS by default, thus don't have to be passed
end

#set_editorObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/netzke/basepack/column_config.rb', line 64

def set_editor
  # if shouldn't be editable, don't set any default editor
  return if read_only

  passed_editor = editor

  if association?
    set_default_association_editor
  else
    self.editor = editor_for_type(type)
  end

  self.editor.merge!(passed_editor) if passed_editor
end

#set_widthObject



115
116
117
# File 'lib/netzke/basepack/column_config.rb', line 115

def set_width
  self.width = 150 if type == :datetime
end

#set_xtypeObject



51
52
53
54
55
56
# File 'lib/netzke/basepack/column_config.rb', line 51

def set_xtype
  # if user set those manually, we don't mess with column xtype
  return if renderer || editor
  xtype = xtype_for_type(type)
  self.xtype = xtype unless xtype.nil?
end

#type_to_editor_xtype(type) ⇒ Object



98
99
100
# File 'lib/netzke/basepack/column_config.rb', line 98

def type_to_editor_xtype(type)
  type_to_editor_xtype_map[type] || :textfield
end

#type_to_editor_xtype_mapObject

Hash that maps a column type to the editor xtype. Override if you want different editors.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/netzke/basepack/column_config.rb', line 103

def type_to_editor_xtype_map
  {
    integer: :numberfield,
    decimal: :numberfield,
    boolean: :checkbox,
    date: :datefield,
    datetime: :xdatetime,
    text: :textarea,
    string: :textfield
  }
end

#xtype_for_type(type) ⇒ Object



58
59
60
61
62
# File 'lib/netzke/basepack/column_config.rb', line 58

def xtype_for_type(type)
  { :boolean  => :checkcolumn,
    :date     => :datecolumn
  }[type]
end