Module: Netzke::Basepack::GridPanel::Columns
- Extended by:
- ActiveSupport::Concern, ActiveSupport::Memoizable
- Defined in:
- lib/netzke/basepack/grid_panel/columns.rb
Instance Method Summary collapse
- #append_meta_column(cols) ⇒ Object
-
#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. -
#columns_hash ⇒ Object
Columns as a hash, for easier access to a specific column.
-
#default_columns ⇒ Object
Columns that we fall back to when neither persistent columns, nor configured columns are present.
-
#initial_columns(with_excluded = false) ⇒ Object
Columns that represent a smart merge of default_columns and columns passed during the configuration.
-
#meta_data(r) ⇒ Object
Override it when you need extra meta data to be passed through the meta column.
-
#meta_default_data ⇒ Object
default_value for the meta column; used when a new record is being created in the grid.
-
#overridden_default_columns ⇒ Object
Columns that were overridden with :override_columns config option.
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 (cols) cols << {}.tap do |c| c.merge!( :name => "_meta", :meta => true, :getter => lambda do |r| (r) end ) c[:default_value] = if .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( = {}) [].tap do |cols| if loaded_columns = load_columns filter_out_excluded_columns(loaded_columns) unless [:with_excluded] cols.concat(reverse_merge_equally_named_columns(loaded_columns, initial_columns([:with_excluded]))) else cols.concat(initial_columns([:with_excluded])) end (cols) if [:with_meta] end end |
#columns_hash ⇒ Object
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_columns ⇒ Object
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_data ⇒ Object
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 get_default_association_values.present? ? { :association_values => get_default_association_values.literalize_keys } : {} end |
#overridden_default_columns ⇒ Object
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 |