Module: Netzke::DataAccessor

Included in:
Basepack::FormPanel, Basepack::GridPanel
Defined in:
lib/netzke/data_accessor.rb

Overview

This module is included into such data-driven components as GridPanel, FormPanel, etc.

Instance Method Summary (collapse)

Instance Method Details

- (Object) assoc_and_assoc_method_for_column(c)

Returns association and association method for a column



68
69
70
71
72
# File 'lib/netzke/data_accessor.rb', line 68

def assoc_and_assoc_method_for_column(c)
  assoc_name, assoc_method = c[:name].split('__')
  assoc = data_class.reflect_on_association(assoc_name.to_sym) if assoc_method
  [assoc, assoc_method]
end

- (Boolean) association_attr?(name)

Returns:

  • (Boolean)


74
75
76
# File 'lib/netzke/data_accessor.rb', line 74

def association_attr?(name)
  !!name.to_s.index("__")
end

- (Object) combobox_options_for_column(column, method_options = {})

Returns options for comboboxes in grids/forms



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/netzke/data_accessor.rb', line 8

def combobox_options_for_column(column, method_options = {})
  query = method_options[:query]

  # First, check if we have options for this column defined in persistent storage
  options = column[:combobox_options] && column[:combobox_options].split("\n")
  if options
    query ? options.select{ |o| o.index(/^#{query}/) }.map{ |el| [el] } : options
  else
    assoc, assoc_method = assoc_and_assoc_method_for_column(column)

    if assoc
      # Options for an asssociation attribute

      relation = assoc.klass.scoped

      relation = relation.extend_with(method_options[:scope]) if method_options[:scope]

      if assoc.klass.column_names.include?(assoc_method)
        # apply query
        relation = relation.where(:#{assoc_method}".like => "#{query}%") if query.present?
        relation.all.map{ |r| [r.send(assoc_method)] }
      else
        relation.all.map{ |r| r.send(assoc_method) }.select{ |value| value =~ /^#{query}/  }.map{ |v| [v] }
      end

    else
      # Options for a non-association attribute
      res=data_class.netzke_combo_options_for(column[:name], method_options)

      # ensure it is an array-in-array, as Ext will fail otherwise
      raise RuntimeError, "netzke_combo_options_for should return an Array" unless res.kind_of? Array
      return [[]] if res.empty?

      unless res.first.kind_of? Array
        res=res.map do |v|
          [v]
        end
      end
      return res


    end
  end
end

- (Object) data_class

Model class



80
81
82
83
84
# File 'lib/netzke/data_accessor.rb', line 80

def data_class
  @data_class ||= begin
    klass = constantize_class_name("Netzke::ModelExtensions::#{config[:model]}For#{short_component_class_name}") || original_data_class
  end
end

- (Object) normalize_attr(a)

Normalize an attribute, e.g.: :first_name =>

{:name => "first_name"}


63
64
65
# File 'lib/netzke/data_accessor.rb', line 63

def normalize_attr(a)
  a.is_a?(Symbol) || a.is_a?(String) ? {:name => a.to_s} : a.merge(:name => a[:name].to_s)
end

- (Object) normalize_attrs(attrs)

Normalize array of attributes

:col1, "col2", => :col3

>

=> "col1", => "col2", => "col3"


56
57
58
# File 'lib/netzke/data_accessor.rb', line 56

def normalize_attrs(attrs)
  attrs.map{ |a| normalize_attr(a) }
end

- (Object) original_data_class

Model class before model extensions are taken into account



87
88
89
90
91
92
93
# File 'lib/netzke/data_accessor.rb', line 87

def original_data_class
  @original_data_class ||= begin
    ::ActiveSupport::Deprecation.warn("data_class_name option is deprecated. Use model instead", caller) if config[:data_class_name]
    model_name = config[:model] || config[:data_class_name]
    model_name && constantize_class_name(model_name)
  end
end

- (Boolean) primary_key_attr?(a)

whether a column is bound to the primary_key

Returns:

  • (Boolean)


96
97
98
# File 'lib/netzke/data_accessor.rb', line 96

def primary_key_attr?(a)
  data_class && a[:name].to_s == data_class.primary_key
end

- (Object) set_default_virtual(c)

Mark an attribute as "virtual" by default, when it doesn't reflect a model column, or a model column of an association



101
102
103
104
105
106
107
108
109
110
# File 'lib/netzke/data_accessor.rb', line 101

def set_default_virtual(c)
  if c[:virtual].nil? # sometimes at maybe handy to mark a column as non-virtual forcefully
    assoc, assoc_method = get_assoc_and_method(c)
    if assoc
      c[:virtual] = true if !assoc.klass.column_names.map(&:to_sym).include?(assoc_method.to_sym)
    else
      c[:virtual] = true if !data_class.column_names.map(&:to_sym).include?(c[:name].to_sym)
    end
  end
end