Module: Netzke::Basepack::Columns

Extended by:
ActiveSupport::Concern
Included in:
Grid::Base, Tree::Base
Defined in:
lib/netzke/basepack/columns.rb

Overview

Takes care of grid column configuration, as well as the grid's default form fields Grid::Base extends common Ext JS column options with the following ones:

sorting_scope

A Proc object used for sorting by the column. This can be useful for sorting by a virtual column. The Proc object will get the relation as the first parameter, and the sorting direction as the second. Example:

columns => [{ name: "complete_user_name", sorting_scope: lambda {|rel, dir| order("users.first_name #{dir.to_s}, users.last_name #{dir.to_s}") }, ...]
filter_with

A Proc object that receives the relation, the value to filter by and the operator. This allows for more flexible handling of basic filters and enables filtering of virtual columns. Example:

columns => [{ name: "complete_user_name", filter_with: lambda{|rel, value, op| rel.where("first_name like ? or last_name like ?", "%#{value}%", "%#{value}%" ) } }, ...]
filterable

Set to false to disable filtering on this column

editor

A hash that will override the automatic editor configuration. For example, for one-to-many association column you may set it to {min_chars: 1}, which will be passed to the combobox and make it query its remote data after entering 1 character (instead of default 4).

Configuring default filters on grid columns

Default Filters can either be configured on the grid itself

def configure(c)
  super
  c.default_filters = [{name: "Mark"}, {age: {gt: 10}}]
end

or as a component configuration

component :tasks |c|
  c.klass = TaskGrid
  c.default_filters = [{due_date: {before: Time.now}}]
end

Defined Under Namespace

Modules: ClassMethods

Constant Summary

COLUMN_METHOD_NAME =
"%s_column"

Instance Method Summary collapse

Instance Method Details

#append_association_values_column(cols) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/netzke/basepack/columns.rb', line 122

def append_association_values_column(cols)
  cols << Netzke::Basepack::AttrConfig.new("association_values", model_adapter).tap do |c|
    c.meta = true
    c.getter = lambda do |r|
      model_adapter.assoc_values(r, final_columns_hash).netzke_literalize_keys
    end
    defaults = association_value_defaults(cols).netzke_literalize_keys
    c.default_value = defaults if defaults.present?
  end
end

#attributes_for_searchObject

ATM the same attributes are used as in forms



148
149
150
151
152
153
154
155
156
# File 'lib/netzke/basepack/columns.rb', line 148

def attributes_for_search
  non_meta_columns.map do |c|
    {name: c.name, text: c.text, type: c.type}.tap do |a|
      if c[:assoc]
        a[:text].sub!("  ", " ")
      end
    end
  end
end

#build_column_config(name) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/netzke/basepack/columns.rb', line 95

def build_column_config(name)
  Netzke::Basepack::ColumnConfig.new(name, model_adapter).tap do |c|
    attribute_config = attribute_overrides[c.name.to_sym]
    c.merge_attribute(attribute_config) if attribute_config
    augment_column_config(c)
  end
end

#columnsObject

Returns the list of (non-normalized) columns to be used. By default returns the list of model column names and declared columns. Can be overridden.



73
74
75
# File 'lib/netzke/basepack/columns.rb', line 73

def columns
  config.columns || attributes
end

#default_form_itemsObject

Default form items (non-normalized) that will be displayed in the Add/Edit forms



143
144
145
# File 'lib/netzke/basepack/columns.rb', line 143

def default_form_items
  attributes
end

#final_columnsObject

An array of complete columns configs ready to be passed to the JS side.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/netzke/basepack/columns.rb', line 78

def final_columns
  @final_columns ||= [].tap do |cols|
    has_primary_column = false

    columns.each do |name|
      c = build_column_config(name)
      next if c.excluded

      has_primary_column ||= c.primary?
      cols << c
    end

    insert_primary_column(cols) unless has_primary_column
    append_association_values_column(cols)
  end
end

#final_columns_hashObject

Columns as a hash, for easier access to a specific column



109
110
111
# File 'lib/netzke/basepack/columns.rb', line 109

def final_columns_hash
  @_final_columns_hash ||= final_columns.inject({}){|r,c| r.merge(c[:name].to_sym => c)}
end

#form_itemsObject

Form items that will be used by the Add/Edit forms. May be overridden.



159
160
161
# File 'lib/netzke/basepack/columns.rb', line 159

def form_items
  config.form_items || default_form_items
end

#insert_primary_column(cols) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/netzke/basepack/columns.rb', line 133

def insert_primary_column(cols)
  primary_key = model_adapter.primary_key
  raise "Model #{model_adapter.model.name} does not have a primary column" if primary_key.blank?
  c = Netzke::Basepack::ColumnConfig.new(model_adapter.primary_key, model_adapter)
  c.merge_attribute(attribute_overrides[c.name.to_sym]) if attribute_overrides.has_key?(c.name.to_sym)
  augment_column_config(c)
  cols.insert(0, c)
end

#js_columnsObject

Columns that have to be used by the JS side of the grid



114
115
116
117
118
119
120
# File 'lib/netzke/basepack/columns.rb', line 114

def js_columns
  final_columns.each do |c|
    # we are removing the editor on this last step, so that the editor config is still being passed from the
    # column config to the form editor; refactor!
    c.delete(:editor) unless config.edits_inline
  end
end

#non_meta_columnsObject

Array of complete config hashes for non-meta columns



104
105
106
# File 'lib/netzke/basepack/columns.rb', line 104

def non_meta_columns
  @non_meta_columns ||= final_columns.reject{|c| c[:meta]}
end