Module: Netzke::Basepack::GridPanel::Columns

Extended by:
ActiveSupport::Concern
Defined in:
lib/netzke/basepack/grid_panel/columns.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Object) columns(only_included = true)

Normalized columns for the grid, e.g.:

=> :id, :hidden => true, ..., => :name, :editable => false, ..., ...


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 57

def columns(only_included = true)
  @columns ||= begin
    if cols = load_columns
      filter_out_excluded_columns(cols) if only_included
      reverse_merge_equally_named_columns(cols, initial_columns)
      cols
    else
      initial_columns(only_included)
    end
  end
end

- (Object) columns_hash

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



70
71
72
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 70

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

- (Object) default_columns

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.



78
79
80
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 78

def default_columns
  @default_columns ||= load_model_level_attrs || data_class.netzke_attributes
end

- (Object) initial_columns(only_included = true)

Columns that represent a smart merge of default_columns and columns passed during the configuration.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 83

def initial_columns(only_included = true)
  # Normalize here, as from the config we can get symbols (names) instead of hashes
  columns_from_config = config[:columns] && normalize_attrs(config[:columns])


  if columns_from_config
    # automatically add a column that reflects the primary key (unless specified in the config)
    columns_from_config.insert(0, {:name => data_class.primary_key}) unless columns_from_config.any?{ |c| c[:name] == data_class.primary_key }

    # reverse-merge each column hash from config with each column hash from exposed_attributes
    # (columns from config have higher priority)
    for c in columns_from_config
      corresponding_default_column = default_columns.find{ |k| k[:name] == c[:name] }
      c.reverse_merge!(corresponding_default_column) if corresponding_default_column
    end
    columns_for_create = columns_from_config
  else
    # we didn't have columns configured in component's config, so, use the columns from the data class
    columns_for_create = default_columns
  end

  filter_out_excluded_columns(columns_for_create) if only_included

  # Make the column config complete with the defaults
  columns_for_create.each do |c|
    detect_association(c)
    set_default_virtual(c)
    set_default_header(c)
    set_default_editor(c)
    set_default_width(c)
    set_default_hidden(c)
    set_default_editable(c)
    set_default_sortable(c)
    set_default_filterable(c)
  end

  columns_for_create
end