Netzke::Basepack::FormPanel::Fields

Because FormPanel allows for arbitrary layout of fields, we need to have all fields configured in one place (the fields method), and then have references to those fields from items.

Public Instance Methods

fields() click to toggle source

Hash of fully configured fields, that are referenced in the items. E.g.:

  {
    :role__name => {:xtype => 'combobox', :disabled => true, :value => "admin"},
    :created_at => {:xtype => 'datetime', :disabled => true, :value => "2010-10-10 10:10"}
  }
    # File lib/netzke/basepack/form_panel/fields.rb, line 29
29:         def fields
30:           @fields ||= begin
31:             if static_layout?
32:               # extract incomplete field configs from +config+
33:               flds = fields_from_config
34:               # and merged them with fields from the model
35:               deep_merge_existing_fields(flds, fields_from_model)
36:             else
37:               # extract flds configs from the model
38:               flds = fields_from_model
39:             end
40:             flds
41:           end
42:         end
fields_array_from_model() click to toggle source

The array of fields as specified on the model level (using netzke_attribute and alike)

    # File lib/netzke/basepack/form_panel/fields.rb, line 45
45:         def fields_array_from_model
46:           data_class && data_class.netzke_attributes
47:         end
fields_from_config() click to toggle source

Hash of normalized field configs extracted from :items, e.g.:

    {:role__name => {:xtype => "combobox"}, :password => {:xtype => "passwordfield"}}
    # File lib/netzke/basepack/form_panel/fields.rb, line 57
57:         def fields_from_config
58:           items if @fields_from_config.nil? # by calling +items+ we initiate building of @fields_from_config
59:           @fields_from_config ||= {}
60:         end
fields_from_model() click to toggle source

Hash of fields as specified on the model level

    # File lib/netzke/basepack/form_panel/fields.rb, line 50
50:         def fields_from_model
51:           @fields_from_model ||= fields_array_from_model && fields_array_from_model.inject({}){ |hsh, f| hsh.merge(f[:name].to_sym => f) }
52:         end
items() click to toggle source

Items with normalized fields (i.e. containing all the necessary attributes needed by Ext.form.FormPanel to render a field)

    # File lib/netzke/basepack/form_panel/fields.rb, line 9
 9:         def items
10:           @form_panel_items ||= begin
11:             res = normalize_fields(super || data_class && data_class.netzke_attributes || []) # netzke_attributes as default items
12: 
13:             # if primary key isn't there, insert it as first
14:             if data_class && res.first && res.first[:name] != [data_class.primary_key]
15:               primary_key_item = normalize_field(data_class.primary_key.to_sym)
16:               @fields_from_config[data_class.primary_key.to_sym] = primary_key_item
17:               res.insert(0, primary_key_item)
18:             end
19: 
20:             res
21:           end
22:         end

Private Instance Methods

attr_type_to_xtype_map() click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 190
190:           def attr_type_to_xtype_map
191:             {
192:               :integer => :numberfield,
193:               :boolean => :checkbox,
194:               :date => :datefield,
195:               :datetime => :xdatetime,
196:               :text => :textarea,
197:               :json => :jsonfield,
198:               :string => :textfield
199:             }
200:           end
deep_merge_existing_fields(dest, src) click to toggle source

Deeply merges only those key/values at the top level that are already there

     # File lib/netzke/basepack/form_panel/fields.rb, line 171
171:           def deep_merge_existing_fields(dest, src)
172:             dest.each_pair do |k,v|
173:               v.deep_merge!(src[k] || {})
174:             end
175:           end
detect_association_with_method(c) click to toggle source

Sets the proper xtype of an asociation field

     # File lib/netzke/basepack/form_panel/fields.rb, line 120
120:           def detect_association_with_method(c)
121:             if c[:name].index('__')
122:               assoc_name, method = c[:name].split('__').map(&:to_sym)
123:               if method && assoc = data_class.reflect_on_association(assoc_name)
124:                 assoc_column = assoc.klass.columns_hash[method.to_s]
125:                 assoc_method_type = assoc_column.try(:type)
126:                 if assoc_method_type
127:                   if c[:nested_attribute]
128:                     c[:xtype] ||= xtype_for_attr_type(assoc_method_type)
129:                   else
130:                     c[:xtype] ||= assoc_method_type == :boolean ? xtype_for_attr_type(assoc_method_type) : xtype_for_association
131:                   end
132:                 end
133:               end
134:             end
135:           end
is_field_config?(item) click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 155
155:           def is_field_config?(item)
156:             item.is_a?(String) || item.is_a?(Symbol) || item[:name] # && !is_component_config?(item)
157:           end
load_model_level_attrs() click to toggle source
    # File lib/netzke/basepack/form_panel/fields.rb, line 80
80:           def load_model_level_attrs
81:             # NetzkeModelAttrList.read_list(data_class.name) if persistent_config_enabled? && data_class
82:           end
load_persistent_fields() click to toggle source
    # File lib/netzke/basepack/form_panel/fields.rb, line 76
76:           def load_persistent_fields
77:             # NetzkeFieldList.read_list(global_id) if persistent_config_enabled?
78:           end
normalize_field(field) click to toggle source

This is where we expand our basic field config with all the defaults

     # File lib/netzke/basepack/form_panel/fields.rb, line 85
 85:           def normalize_field(field)
 86:             # field can only be a string, a symbol, or a hash
 87:             if field.is_a?(Hash)
 88:               field = field.dup # we don't want to modify original hash
 89:               return field if field[:no_binding] # stop here if no normalization is needed
 90:               field[:name] = field[:name].to_s if field[:name] # all names should be strings
 91:             else
 92:               field = {:name => field.to_s}
 93:             end
 94: 
 95:             field_from_model = fields_from_model && fields_from_model[field[:name].to_sym]
 96: 
 97:             field_from_model && field.merge!(field_from_model)
 98: 
 99:             detect_association_with_method(field) # xtype for an association field
100:             set_default_field_label(field)
101:             set_default_field_xtype(field) if field[:xtype].nil?
102:             set_default_field_value(field) if self.record
103:             set_default_read_only(field)
104: 
105:             field[:display_mode] = config[:display_mode] if config[:display_mode]
106: 
107:             # provide our special combobox with our id
108:             field[:parent_id] = self.global_id if field[:xtype] == :combobox
109: 
110:             field[:hidden] = field[:hide_label] = true if field[:hidden].nil? && primary_key_attr?(field)
111: 
112:             # checkbox setup
113:             field[:checked] = field[:value] if field[:attr_type] == :boolean
114:             field[:input_value] = true if field[:attr_type] == :boolean
115: 
116:             field
117:           end
normalize_fields(items) click to toggle source

RECURSIVELY extracts fields configuration from :items

     # File lib/netzke/basepack/form_panel/fields.rb, line 138
138:           def normalize_fields(items)
139:             @fields_from_config ||= {}
140:             items.map do |item|
141:               # at this moment, item is a hash or a symbol
142:               if is_field_config?(item)
143:                 item = normalize_field(item)
144:                 @fields_from_config[item[:name].to_sym] = item
145:                 item #.reject{ |k,v| k == :name } # do we really need to remove the :name key?
146:               elsif item.is_a?(Hash)
147:                 item = item.dup # we don't want to modify original hash
148:                 item[:items].is_a?(Array) ? item.merge(:items => normalize_fields(item[:items])) : item
149:               else
150:                 item
151:               end
152:             end
153:           end
set_default_field_label(c) click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 159
159:           def set_default_field_label(c)
160:             # multiple spaces (in case of association attrs) get replaced with one
161:             c[:field_label] ||= data_class ? data_class.human_attribute_name(c[:name]) : c[:name].humanize
162:             c[:field_label].gsub!(/\s+/, " ")
163:           end
set_default_field_value(field) click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 165
165:           def set_default_field_value(field)
166:             value = record.value_for_attribute(field)
167:             field[:value] ||= value unless value.nil?
168:           end
set_default_field_xtype(field) click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 177
177:           def set_default_field_xtype(field)
178:             field[:xtype] = xtype_for_attr_type(field[:attr_type]) unless xtype_for_attr_type(field[:attr_type]).nil?
179:           end
set_default_read_only(field) click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 181
181:           def set_default_read_only(field)
182:             enabled_if = !data_class || data_class.column_names.include?(field[:name])
183:             enabled_if ||= data_class.instance_methods.map(&:to_s).include?("#{field[:name]}=")
184:             enabled_if ||= record && record.respond_to?("#{field[:name]}=")
185:             enabled_if ||= association_attr?(field[:name])
186: 
187:             field[:read_only] = !enabled_if if field[:read_only].nil?
188:           end
static_layout?() click to toggle source

Are we provided with a static field layout?

     # File lib/netzke/basepack/form_panel/fields.rb, line 211
211:           def static_layout?
212:             !!config[:items]
213:           end
xtype_for_association() click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 206
206:           def xtype_for_association
207:             :combobox
208:           end
xtype_for_attr_type(type) click to toggle source
     # File lib/netzke/basepack/form_panel/fields.rb, line 202
202:           def xtype_for_attr_type(type)
203:             attr_type_to_xtype_map[type] || :textfield
204:           end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.