Sha256: fd554d1283368bdafde470ace98856037e2a6ded95584ca25ac774dc0ef8aefd

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

require "netzke/active_record/data_accessor"

module Netzke
  # This module is included into such data-driven widgets as GridPanel, FormPanel, etc. It provides for
  # flexible pre-configuration of (virtual) attributes.
  #
  # TODO: show examples of how to create the helpers
  module DataAccessor
    
    # This method should be called from the constructor of the widget. It dynamically includes:
    # 1) helpers into the data model for this widget; those may contain instance methods used as virtual attributes
    # 2) generic (used by all "data accessor" widgets) extensions into the data model for this widget
    def apply_helpers
      # Generic extensions to the data model
      if data_class # because some widgets, like FormPanel, may have it optional
        data_class.send(:include, Netzke::ActiveRecord::DataAccessor) if !data_class.include?(Netzke::ActiveRecord::DataAccessor)
      end
    end
    
    # [:col1, "col2", {:name => :col3}] =>
    #   [{:name => "col1"}, {:name => "col2"}, {:name => "col3"}]
    def normalize_attr_config(cols)
      cols.map do |c|
        c.is_a?(Symbol) || c.is_a?(String) ? {:name => c.to_s} : c.merge(:name => c[:name].to_s)
      end
    end

    # Make sure we have keys as symbols, not strings
    def normalize_array_of_columns(arry)
      arry.map do |f| 
        if f.is_a?(Hash)
          f.symbolize_keys
        else
          f.to_sym
        end
      end
    end
    
    # From symbol to config hash
    def normalize_column(field)
      field.is_a?(Symbol) ? {:name => field.to_s} : field
    end
    
    # From symbols to config hashes
    def normalize_columns(items)
      items.map{|c| normalize_column(c)}
    end
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
netzke-basepack-0.5.12 lib/netzke/data_accessor.rb
netzke-basepack-0.5.11 lib/netzke/data_accessor.rb
netzke-basepack-0.5.10 lib/netzke/data_accessor.rb
netzke-basepack-0.5.9 lib/netzke/data_accessor.rb