Normalized columns for the grid, e.g.:
# File lib/netzke/basepack/grid_panel/columns.rb, line 57
57: def columns(only_included = true)
58: @columns ||= begin
59: if cols = load_columns
60: filter_out_excluded_columns(cols) if only_included
61: reverse_merge_equally_named_columns(cols, initial_columns)
62: cols
63: else
64: initial_columns(only_included)
65: end
66: end
67: end
Columns as a hash, for easier access to a specific column
# File lib/netzke/basepack/grid_panel/columns.rb, line 70
70: def columns_hash
71: @columns_hash ||= columns.inject({}){|r,c| r.merge(c[:name].to_sym => c)}
72: end
Columns that we fall back to when neither persistent columns, nor configured columns are present. If there’s a model-level field configuration, it’s being used. Otherwise the defaults straight from the ActiveRecord model (“netzke_attributes“). Override this method if you want to provide a fix set of columns in your subclass.
# File lib/netzke/basepack/grid_panel/columns.rb, line 78
78: def default_columns
79: @default_columns ||= load_model_level_attrs || data_class.netzke_attributes
80: end
Columns that represent a smart merge of default_columns and columns passed during the configuration.
# File lib/netzke/basepack/grid_panel/columns.rb, line 83
83: def initial_columns(only_included = true)
84: # Normalize here, as from the config we can get symbols (names) instead of hashes
85: columns_from_config = config[:columns] && normalize_attrs(config[:columns])
86:
87:
88: if columns_from_config
89: # automatically add a column that reflects the primary key (unless specified in the config)
90: columns_from_config.insert(0, {:name => data_class.primary_key}) unless columns_from_config.any?{ |c| c[:name] == data_class.primary_key }
91:
92: # reverse-merge each column hash from config with each column hash from exposed_attributes
93: # (columns from config have higher priority)
94: for c in columns_from_config
95: corresponding_default_column = default_columns.find{ |k| k[:name] == c[:name] }
96: c.reverse_merge!(corresponding_default_column) if corresponding_default_column
97: end
98: columns_for_create = columns_from_config
99: else
100: # we didn't have columns configured in component's config, so, use the columns from the data class
101: columns_for_create = default_columns
102: end
103:
104: filter_out_excluded_columns(columns_for_create) if only_included
105:
106: # Make the column config complete with the defaults
107: columns_for_create.each do |c|
108: detect_association(c)
109: set_default_header(c)
110: set_default_editor(c)
111: set_default_width(c)
112: set_default_hidden(c)
113: set_default_editable(c)
114: set_default_sortable(c)
115: set_default_filterable(c)
116: end
117:
118: columns_for_create
119: end
Returns a hash that maps a column type to the editor xtype. Override if you want different editors.
# File lib/netzke/basepack/grid_panel/columns.rb, line 188
188: def attr_type_to_editor_map
189: {
190: :integer => :numberfield,
191: :boolean => :checkbox,
192: :date => :datefield,
193: :datetime => :xdatetime,
194: :text => :textarea,
195: :string => :textfield
196: }
197: end
Default fields that will be displayed in the Add/Edit/Search forms When overriding this method, keep in mind that the fields inside the layout must be expanded (each field represented by a hash, not just a symbol)
# File lib/netzke/basepack/grid_panel/columns.rb, line 220
220: def default_fields_for_forms
221: form_klass = "Netzke::ModelExtensions::#{config[:model]}ForFormPanel".constantize rescue nil
222: form_klass ||= original_data_class
223:
224: # Select only those fields that are known to the form_klass
225: selected_columns = columns.select do |c|
226: form_klass.column_names.include?(c[:name]) ||
227: form_klass.instance_methods.include?("#{c[:name]}=") ||
228: association_attr?(c[:name])
229: end
230:
231: selected_columns.map do |c|
232: field_config = {:name => c[:name]}
233:
234: # scopes for combobox options
235: field_config[:scopes] = c[:editor].is_a?(Hash) && c[:editor][:scopes]
236:
237: field_config
238: end
239: end
default_fields_for_forms extended with default values (for new-record form)
# File lib/netzke/basepack/grid_panel/columns.rb, line 242
242: def default_fields_for_forms_with_default_values
243: res = default_fields_for_forms.dup
244: each_attr_in(res) do |a|
245: attr_name = a[:name].to_sym
246: a[:value] = a[:default_value] || columns_hash[attr_name].try(:fetch, :default_value, nil) || data_class.netzke_attribute_hash[attr_name].try(:fetch, :default_value, nil)
247: end
248: res
249: end
Detects an association column and sets up the proper editor.
# File lib/netzke/basepack/grid_panel/columns.rb, line 200
200: def detect_association(c)
201: # double-underscore notation? surely an association column
202: if c[:name].index('__')
203: assoc_name, assoc_method = c[:name].split('__')
204: if assoc_method && assoc = data_class.reflect_on_association(assoc_name.to_sym)
205: assoc_column = assoc.klass.columns_hash[assoc_method]
206: assoc_method_type = assoc_column.try(:type)
207:
208: # if association column is boolean, display a checkbox (or alike), otherwise - a combobox (or alike)
209: if c[:nested_attribute]
210: c[:editor] ||= editor_for_attr_type(assoc_method_type)
211: else
212: c[:editor] ||= assoc_method_type == :boolean ? editor_for_attr_type(:boolean) : editor_for_association
213: end
214: end
215: end
216: end
Recursively traversess items (an array) and yields each found field (a hash with :name set)
# File lib/netzke/basepack/grid_panel/columns.rb, line 252
252: def each_attr_in(items)
253: items.each do |item|
254: if item.is_a?(Hash)
255: each_attr_in(item[:items]) if item[:items].is_a?(Array)
256: yield(item) if item[:name]
257: end
258: end
259: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 183
183: def editor_for_association
184: :combobox
185: end
Returns editor’s xtype for a column type
# File lib/netzke/basepack/grid_panel/columns.rb, line 179
179: def editor_for_attr_type(type)
180: attr_type_to_editor_map[type] || :textfield
181: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 123
123: def filter_out_excluded_columns(cols)
124: cols.reject!{ |c| c[:included] == false }
125: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 132
132: def load_columns
133: # NetzkeFieldList.read_list(global_id) if persistent_config_enabled?
134: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 136
136: def load_model_level_attrs
137: # NetzkeModelAttrList.read_list(data_class.name) if persistent_config_enabled?
138: end
Receives 2 arrays of columns. Merges the missing config from the source into dest, matching columns by name
# File lib/netzke/basepack/grid_panel/columns.rb, line 262
262: def reverse_merge_equally_named_columns(dest, source)
263: dest.each{ |dc| dc.reverse_merge!(source.detect{ |sc| sc[:name] == dc[:name] } || {}) }
264: end
Stores modified columns in persistent storage
# File lib/netzke/basepack/grid_panel/columns.rb, line 128
128: def save_columns!
129: # NetzkeFieldList.update_list_for_current_authority(global_id, columns(false), original_data_class.name) if persistent_config_enabled?
130: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 158
158: def set_default_editable(c)
159: not_editable_if = primary_key_attr?(c)
160: not_editable_if ||= c[:virtual]
161: not_editable_if ||= c.delete(:read_only)
162:
163: editable_if = data_class.column_names.include?(c[:name])
164: editable_if ||= data_class.instance_methods.map(&:to_s).include?("#{c[:name]}=")
165: editable_if ||= association_attr?(c[:name])
166:
167: c[:editable] = editable_if && !not_editable_if if c[:editable].nil?
168: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 144
144: def set_default_editor(c)
145: c[:editor] ||= editor_for_attr_type(c[:attr_type])
146: c[:editor] = {:xtype => c[:editor]} if c[:editor].is_a?(Symbol)
147: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 174
174: def set_default_filterable(c)
175: c[:filterable] = !c[:virtual] if c[:filterable].nil?
176: end
# File lib/netzke/basepack/grid_panel/columns.rb, line 140
140: def set_default_header(c)
141: c[:label] ||= data_class.human_attribute_name(c[:name])
142: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.