Module: DataGraph::Utils

Included in:
Graph, Linkage, Node
Defined in:
lib/data_graph/utils.rb

Class Method Summary collapse

Class Method Details

.cpk?(assoc) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/data_graph/utils.rb', line 27

def cpk?(assoc)
  assoc = assoc.through_reflection if assoc.through_reflection
  assoc.primary_key_name.to_s.include?(',')
end

.foreign_key(assoc) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/data_graph/utils.rb', line 14

def foreign_key(assoc)
  # actually returns options[:foreign_key], or the default foreign key
  foreign_key = assoc.primary_key_name

  # cpk returns a csv string
  foreign_key.to_s.split(',')
end

.patherize_attrs(attrs, nest_paths = [], paths = [], prefix = '') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/data_graph/utils.rb', line 32

def patherize_attrs(attrs, nest_paths=[], paths=[], prefix='')
  attrs.each_pair do |key, value|
    case key
    when String, Symbol
      path = "#{prefix}#{key}"
      
      if nest_paths.include?(path)
        value = value.values
      end
      
      case value
      when Hash
        patherize_attrs(value, nest_paths, paths, "#{path}.")
      when Array
        next_prefix = "#{path}."
        value.each {|hash| patherize_attrs(hash, nest_paths, paths, next_prefix) }
      else
        paths << path
      end
    else
      raise "unexpected attribute key: #{key.inspect}"
    end
  end
  
  paths
end

.primary_keys(model) ⇒ Object



10
11
12
# File 'lib/data_graph/utils.rb', line 10

def primary_keys(model)
  model.respond_to?(:primary_keys) ? model.primary_keys : [model.primary_key]
end

.reference_key(assoc) ⇒ Object



22
23
24
25
# File 'lib/data_graph/utils.rb', line 22

def reference_key(assoc)
  primary_key = assoc.options[:primary_key] || primary_keys(assoc.macro == :belongs_to ? assoc.klass : assoc.active_record)
  primary_key.kind_of?(Array) ? primary_key.collect {|key| key.to_s } : primary_key.to_s.split(',')
end