Sha256: 34b9944a7a5dbbed08ce98c46bad6bb3c2c9cbf10fc43e0a6fd196565e32f5d6

Contents?: true

Size: 1.68 KB

Versions: 14

Compression:

Stored size: 1.68 KB

Contents

module Netzke
  class Column
    def self.default_columns_for_widget(widget)
      raise ArgumentError, "No data_class_name specified for widget #{widget.config[:name]}" if widget.config[:data_class_name].nil?

      # layout = NetzkeLayout.create(:widget_name => widget.id_name, :items_class => self.name, :user_id => NetzkeLayout.user_id)

      data_class = widget.config[:data_class_name].constantize

      exposed_columns = normalize_columns(data_class.exposed_columns) # columns exposed from the data class

      columns_from_config = widget.config[:columns] && normalize_columns(widget.config[:columns]) # columns specified in widget's config

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

      res = []
      for c in columns_for_create
        # finally reverse-merge them with the defaults from the data_class
        res << data_class.default_column_config(c)
      end
      
      res
    end
    
    protected

    # like this: [:col1, {:name => :col2}, :col3] => [{:name => :col1}, {:name => :col2}, {:name => :col3}]
    def self.normalize_columns(items)
      items.map{|c| c.is_a?(Symbol) ? {:name => c} : c}
    end
    
  end
end

Version data entries

14 entries across 14 versions & 4 rubygems

Version Path
skozlov-netzke-basepack-0.1.0.1 lib/netzke/column.rb
skozlov-netzke-basepack-0.1.0 lib/netzke/column.rb
skozlov-netzke-basepack-0.1.1.1 lib/netzke/column.rb
skozlov-netzke-basepack-0.1.1.2 lib/netzke/column.rb
skozlov-netzke-basepack-0.1.1 lib/netzke/column.rb
skozlov-netzke_basepack-0.1.0 lib/netzke/column.rb
skozlov-netzke_core-0.1.0 lib/netzke/column.rb
netzke-basepack-0.1.4.1 lib/netzke/column.rb
netzke-basepack-0.1.3.1 lib/netzke/column.rb
netzke-basepack-0.1.2.1 lib/netzke/column.rb
netzke-basepack-0.1.2 lib/netzke/column.rb
netzke-basepack-0.1.3 lib/netzke/column.rb
netzke-basepack-0.2.0.1 lib/netzke/column.rb
netzke-basepack-0.2.0 lib/netzke/column.rb