Class: Neo4Apis::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/neo4apis/activerecord.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.model_importer(model_class) ⇒ Object



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
# File 'lib/neo4apis/activerecord.rb', line 9

def self.model_importer(model_class)
  if model_class.primary_key.is_a?(Array)
    relationship_name = model_class.table_name
    associations = model_class.reflect_on_all_associations

    importer model_class.name.to_sym do |object|
      from_node = object.send(associations[0].name)
      from_node = add_model_node from_node.class, from_node

      to_node = object.send(associations[1].name)

      puts 'add_model_relationship...'
      add_model_relationship relationship_name, from_node, to_node
    end
  else
    return if model_class.primary_key.nil?
    uuid model_class.name.to_sym, model_class.primary_key

    importer model_class.name.to_sym do |object|
      node = add_model_node model_class, object

      model_class.reflect_on_all_associations.each do |association_reflection|
        case association_reflection.macro
        when :belongs_to, :has_one
          if options[:"import_#{association_reflection.macro}"]
            referenced_object = object.send(association_reflection.name)
            add_model_relationship association_reflection.name, node, referenced_object if referenced_object
          end
        when :has_many, :has_and_belongs_to_many
          if options[:"import_#{association_reflection.macro}"]
            object.send(association_reflection.name).each do |referenced_object|
              add_model_relationship association_reflection.name, node, referenced_object if referenced_object
            end
          end
        end
      end
    end
  end
end

Instance Method Details

#add_model_node(model_class, object) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/neo4apis/activerecord.rb', line 56

def add_model_node(model_class, object)
  object_data = OpenStruct.new

  object.class.column_names.each do |column_name|
    object_data.send("#{column_name}=", attribute_for_coder(object, column_name))
  end

  add_node model_class.name.to_sym, object_data, model_class.column_names
end

#add_model_relationship(relationship_name, node, referenced_object) ⇒ Object



49
50
51
52
53
54
# File 'lib/neo4apis/activerecord.rb', line 49

def add_model_relationship(relationship_name, node, referenced_object)
  referenced_class = referenced_object.class
  referenced_node = add_model_node referenced_class, referenced_object

  add_relationship relationship_name, node, referenced_node
end

#attribute_for_coder(object, column_name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/neo4apis/activerecord.rb', line 66

def attribute_for_coder(object, column_name)
  column = object.class.columns_hash[column_name]
  if column.respond_to?(:cast_type)
    column.cast_type.type_cast_from_user(object.attributes[column_name])
  else
    value = object.attributes[column_name]
    if coder = object.class.serialized_attributes[column_name]
      coder.dump(value)
    else
      value
    end
  end
end