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

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

Instance Method Summary collapse

Instance Method Details

#append_meta_column(cols) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 76

def append_meta_column(cols)
  cols << {}.tap do |c|
    c.merge!(
      :name => "_meta",
      :meta => true,
      :getter => lambda do |r|
        (r)
      end
    )
    c[:default_value] = meta_default_data if meta_default_data.present?
  end
end

#columns(options = {}) ⇒ Object

Normalized columns for the grid, e.g.:

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

Possible options:

  • with_excluded - when set to true, also excluded columns will be returned (handy for dynamic column configuration)

  • with_meta - when set to true, the meta column will be included as the last column



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 61

def columns(options = {})
  [].tap do |cols|
    if loaded_columns = load_columns
      filter_out_excluded_columns(loaded_columns) unless options[:with_excluded]
      cols.concat(reverse_merge_equally_named_columns(loaded_columns, initial_columns(options[:with_excluded])))
    else
      cols.concat(initial_columns(options[:with_excluded]))
    end

    append_meta_column(cols) if options[:with_meta]
  end
end

#columns_hashObject

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



100
101
102
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 100

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

#default_columnsObject

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.



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

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

#initial_columns(with_excluded = false) ⇒ Object

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 126

def initial_columns(with_excluded = false)
  # 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.to_s}) 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 = overridden_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 = overridden_default_columns
  end

  filter_out_excluded_columns(columns_for_create) unless with_excluded

  # Make the column config complete with the defaults.
  # Note: dup is needed to avoid modifying original hashes.
  columns_for_create.map { |c| c.dup.tap { |c| augment_column_config c } }
end

#meta_data(r) ⇒ Object

Override it when you need extra meta data to be passed through the meta column



95
96
97
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 95

def (r)
  { :association_values => get_association_values(r).literalize_keys }
end

#meta_default_dataObject

default_value for the meta column; used when a new record is being created in the grid



90
91
92
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 90

def meta_default_data
  get_default_association_values.present? ? { :association_values => get_default_association_values.literalize_keys } : {}
end

#overridden_default_columnsObject

Columns that were overridden with :override_columns config option.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/netzke/basepack/grid_panel/columns.rb', line 113

def overridden_default_columns
  if config[:override_columns].present?
    result = []
    default_columns.each do |col|
      result << col.merge(config[:override_columns][col[:name].to_sym] || {})
    end
    result
  else
    default_columns
  end
end