neo4j_legacy 7.2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1357 -0
- data/CONTRIBUTORS +8 -0
- data/Gemfile +38 -0
- data/README.md +103 -0
- data/bin/neo4j-jars +33 -0
- data/bin/rake +17 -0
- data/config/locales/en.yml +5 -0
- data/config/neo4j/add_classnames.yml +1 -0
- data/config/neo4j/config.yml +35 -0
- data/lib/active_support/per_thread_registry.rb +1 -0
- data/lib/backports/action_controller/metal/strong_parameters.rb +672 -0
- data/lib/backports/active_model/forbidden_attributes_protection.rb +30 -0
- data/lib/backports/active_support/concern.rb +13 -0
- data/lib/backports/active_support/core_ext/module/attribute_accessors.rb +10 -0
- data/lib/backports/active_support/logger.rb +99 -0
- data/lib/backports/active_support/logger_silence.rb +27 -0
- data/lib/backports/active_support/logger_thread_safe_level.rb +32 -0
- data/lib/backports/active_support/per_thread_registry.rb +60 -0
- data/lib/backports.rb +4 -0
- data/lib/neo4j/active_node/callbacks.rb +8 -0
- data/lib/neo4j/active_node/dependent/association_methods.rb +48 -0
- data/lib/neo4j/active_node/dependent/query_proxy_methods.rb +50 -0
- data/lib/neo4j/active_node/dependent.rb +11 -0
- data/lib/neo4j/active_node/enum.rb +29 -0
- data/lib/neo4j/active_node/has_n/association/rel_factory.rb +61 -0
- data/lib/neo4j/active_node/has_n/association/rel_wrapper.rb +23 -0
- data/lib/neo4j/active_node/has_n/association.rb +280 -0
- data/lib/neo4j/active_node/has_n/association_cypher_methods.rb +108 -0
- data/lib/neo4j/active_node/has_n.rb +532 -0
- data/lib/neo4j/active_node/id_property/accessor.rb +62 -0
- data/lib/neo4j/active_node/id_property.rb +187 -0
- data/lib/neo4j/active_node/initialize.rb +21 -0
- data/lib/neo4j/active_node/labels/index.rb +87 -0
- data/lib/neo4j/active_node/labels/reloading.rb +21 -0
- data/lib/neo4j/active_node/labels.rb +198 -0
- data/lib/neo4j/active_node/node_wrapper.rb +52 -0
- data/lib/neo4j/active_node/orm_adapter.rb +82 -0
- data/lib/neo4j/active_node/persistence.rb +175 -0
- data/lib/neo4j/active_node/property.rb +60 -0
- data/lib/neo4j/active_node/query/query_proxy.rb +361 -0
- data/lib/neo4j/active_node/query/query_proxy_eager_loading.rb +61 -0
- data/lib/neo4j/active_node/query/query_proxy_enumerable.rb +90 -0
- data/lib/neo4j/active_node/query/query_proxy_find_in_batches.rb +19 -0
- data/lib/neo4j/active_node/query/query_proxy_link.rb +117 -0
- data/lib/neo4j/active_node/query/query_proxy_methods.rb +210 -0
- data/lib/neo4j/active_node/query/query_proxy_methods_of_mass_updating.rb +83 -0
- data/lib/neo4j/active_node/query.rb +76 -0
- data/lib/neo4j/active_node/query_methods.rb +65 -0
- data/lib/neo4j/active_node/reflection.rb +86 -0
- data/lib/neo4j/active_node/rels.rb +11 -0
- data/lib/neo4j/active_node/scope.rb +146 -0
- data/lib/neo4j/active_node/unpersisted.rb +48 -0
- data/lib/neo4j/active_node/validations.rb +59 -0
- data/lib/neo4j/active_node.rb +105 -0
- data/lib/neo4j/active_rel/callbacks.rb +15 -0
- data/lib/neo4j/active_rel/initialize.rb +28 -0
- data/lib/neo4j/active_rel/persistence/query_factory.rb +95 -0
- data/lib/neo4j/active_rel/persistence.rb +114 -0
- data/lib/neo4j/active_rel/property.rb +95 -0
- data/lib/neo4j/active_rel/query.rb +95 -0
- data/lib/neo4j/active_rel/rel_wrapper.rb +22 -0
- data/lib/neo4j/active_rel/related_node.rb +83 -0
- data/lib/neo4j/active_rel/types.rb +82 -0
- data/lib/neo4j/active_rel/validations.rb +8 -0
- data/lib/neo4j/active_rel.rb +67 -0
- data/lib/neo4j/class_arguments.rb +39 -0
- data/lib/neo4j/config.rb +124 -0
- data/lib/neo4j/core/query.rb +22 -0
- data/lib/neo4j/errors.rb +28 -0
- data/lib/neo4j/migration.rb +127 -0
- data/lib/neo4j/paginated.rb +27 -0
- data/lib/neo4j/railtie.rb +169 -0
- data/lib/neo4j/schema/operation.rb +91 -0
- data/lib/neo4j/shared/attributes.rb +220 -0
- data/lib/neo4j/shared/callbacks.rb +64 -0
- data/lib/neo4j/shared/cypher.rb +37 -0
- data/lib/neo4j/shared/declared_properties.rb +204 -0
- data/lib/neo4j/shared/declared_property/index.rb +37 -0
- data/lib/neo4j/shared/declared_property.rb +118 -0
- data/lib/neo4j/shared/enum.rb +148 -0
- data/lib/neo4j/shared/filtered_hash.rb +79 -0
- data/lib/neo4j/shared/identity.rb +28 -0
- data/lib/neo4j/shared/initialize.rb +28 -0
- data/lib/neo4j/shared/marshal.rb +23 -0
- data/lib/neo4j/shared/mass_assignment.rb +58 -0
- data/lib/neo4j/shared/permitted_attributes.rb +28 -0
- data/lib/neo4j/shared/persistence.rb +231 -0
- data/lib/neo4j/shared/property.rb +220 -0
- data/lib/neo4j/shared/query_factory.rb +101 -0
- data/lib/neo4j/shared/rel_type_converters.rb +43 -0
- data/lib/neo4j/shared/serialized_properties.rb +30 -0
- data/lib/neo4j/shared/type_converters.rb +418 -0
- data/lib/neo4j/shared/typecasted_attributes.rb +98 -0
- data/lib/neo4j/shared/typecaster.rb +53 -0
- data/lib/neo4j/shared/validations.rb +48 -0
- data/lib/neo4j/shared.rb +51 -0
- data/lib/neo4j/tasks/migration.rake +24 -0
- data/lib/neo4j/timestamps/created.rb +9 -0
- data/lib/neo4j/timestamps/updated.rb +9 -0
- data/lib/neo4j/timestamps.rb +11 -0
- data/lib/neo4j/type_converters.rb +7 -0
- data/lib/neo4j/version.rb +3 -0
- data/lib/neo4j/wrapper.rb +4 -0
- data/lib/neo4j.rb +96 -0
- data/lib/rails/generators/neo4j/model/model_generator.rb +86 -0
- data/lib/rails/generators/neo4j/model/templates/model.erb +15 -0
- data/lib/rails/generators/neo4j_generator.rb +67 -0
- data/neo4j.gemspec +43 -0
- metadata +389 -0
@@ -0,0 +1,86 @@
|
|
1
|
+
module Neo4j::ActiveNode
|
2
|
+
# A reflection contains information about an association.
|
3
|
+
# They are often used in connection with form builders to determine associated classes.
|
4
|
+
# This module contains methods related to the creation and retrieval of reflections.
|
5
|
+
module Reflection
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
class_attribute :reflections
|
10
|
+
self.reflections = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# Adds methods to the class related to creating and retrieving reflections.
|
14
|
+
module ClassMethods
|
15
|
+
# @param macro [Symbol] the association type, :has_many or :has_one
|
16
|
+
# @param name [Symbol] the association name
|
17
|
+
# @param association_object [Neo4j::ActiveNode::HasN::Association] the association object created in the course of creating this reflection
|
18
|
+
def create_reflection(macro, name, association_object, model)
|
19
|
+
self.reflections = self.reflections.merge(name => AssociationReflection.new(macro, name, association_object))
|
20
|
+
association_object.add_destroy_callbacks(model)
|
21
|
+
end
|
22
|
+
|
23
|
+
private :create_reflection
|
24
|
+
# @param association [Symbol] an association declared on the model
|
25
|
+
# @return [Neo4j::ActiveNode::Reflection::AssociationReflection] of the given association
|
26
|
+
def reflect_on_association(association)
|
27
|
+
reflections[association.to_sym]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns an array containing one reflection for each association declared in the model.
|
31
|
+
def reflect_on_all_associations(macro = nil)
|
32
|
+
association_reflections = reflections.values
|
33
|
+
macro ? association_reflections.select { |reflection| reflection.macro == macro } : association_reflections
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# The actual reflection object that contains information about the given association.
|
38
|
+
# These should never need to be created manually, they will always be created by declaring a :has_many or :has_one association on a model.
|
39
|
+
class AssociationReflection
|
40
|
+
# The name of the association
|
41
|
+
attr_reader :name
|
42
|
+
|
43
|
+
# The type of association
|
44
|
+
attr_reader :macro
|
45
|
+
|
46
|
+
# The association object referenced by this reflection
|
47
|
+
attr_reader :association
|
48
|
+
|
49
|
+
def initialize(macro, name, association)
|
50
|
+
@macro = macro
|
51
|
+
@name = name
|
52
|
+
@association = association
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns the target model
|
56
|
+
def klass
|
57
|
+
@klass ||= class_name.constantize
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns the name of the target model
|
61
|
+
def class_name
|
62
|
+
@class_name ||= association.target_class.name
|
63
|
+
end
|
64
|
+
|
65
|
+
def rel_klass
|
66
|
+
@rel_klass ||= rel_class_name.constantize
|
67
|
+
end
|
68
|
+
|
69
|
+
def rel_class_name
|
70
|
+
@rel_class_name ||= association.relationship_class.name.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
def type
|
74
|
+
@type ||= association.relationship_type
|
75
|
+
end
|
76
|
+
|
77
|
+
def collection?
|
78
|
+
macro == :has_many
|
79
|
+
end
|
80
|
+
|
81
|
+
def validate?
|
82
|
+
true
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Neo4j::ActiveNode
|
2
|
+
module Rels
|
3
|
+
extend Forwardable
|
4
|
+
def_delegators :_rels_delegator, :rel?, :rel, :rels, :node, :nodes, :create_rel
|
5
|
+
|
6
|
+
def _rels_delegator
|
7
|
+
fail "Can't access relationship on a non persisted node" unless _persisted_obj
|
8
|
+
_persisted_obj
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'active_support/per_thread_registry'
|
2
|
+
|
3
|
+
module Neo4j::ActiveNode
|
4
|
+
module Scope
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
# Similar to ActiveRecord scope
|
9
|
+
#
|
10
|
+
# @example without argument
|
11
|
+
# class Person
|
12
|
+
# include Neo4j::ActiveNode
|
13
|
+
# property :name
|
14
|
+
# property :score
|
15
|
+
# has_many :out, :friends, type: :has_friend, model_class: self
|
16
|
+
# scope :top_students, -> { where(score: 42)}") }
|
17
|
+
# end
|
18
|
+
# Person.top_students.to_a
|
19
|
+
# a_person.friends.top_students.to_a
|
20
|
+
# a_person.friends.friends.top_students.to_a
|
21
|
+
# a_person.friends.top_students.friends.to_a
|
22
|
+
#
|
23
|
+
# @example Argument for scopes
|
24
|
+
# Person.scope :level, ->(num) { where(level_num: num)}
|
25
|
+
#
|
26
|
+
# @example Argument as a cypher identifier
|
27
|
+
# class Person
|
28
|
+
# include Neo4j::ActiveNode
|
29
|
+
# property :name
|
30
|
+
# property :score
|
31
|
+
# has_many :out, :friends, type: :has_friend, model_class: self
|
32
|
+
# scope :great_students, ->(identifier) { where("#{identifier}.score > 41") }
|
33
|
+
# end
|
34
|
+
# Person.as(:all_people).great_students(:all_people).to_a
|
35
|
+
#
|
36
|
+
# @see http://guides.rubyonrails.org/active_record_querying.html#scopes
|
37
|
+
def scope(name, proc)
|
38
|
+
scopes[name.to_sym] = proc
|
39
|
+
|
40
|
+
klass = class << self; self; end
|
41
|
+
klass.instance_eval do
|
42
|
+
define_method(name) do |query_params = nil, _ = nil|
|
43
|
+
eval_context = ScopeEvalContext.new(self, current_scope || self.query_proxy)
|
44
|
+
proc = full_scopes[name.to_sym]
|
45
|
+
_call_scope_context(eval_context, query_params, proc)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# rubocop:disable Style/PredicateName
|
51
|
+
def has_scope?(name)
|
52
|
+
ActiveSupport::Deprecation.warn 'has_scope? is deprecated and may be removed from future releases, use scope? instead.', caller
|
53
|
+
|
54
|
+
scope?(name)
|
55
|
+
end
|
56
|
+
# rubocop:enable Style/PredicateName
|
57
|
+
|
58
|
+
def scope?(name)
|
59
|
+
full_scopes.key?(name.to_sym)
|
60
|
+
end
|
61
|
+
|
62
|
+
def scopes
|
63
|
+
@scopes ||= {}
|
64
|
+
end
|
65
|
+
|
66
|
+
def full_scopes
|
67
|
+
scopes.merge(self.superclass.respond_to?(:scopes) ? self.superclass.scopes : {})
|
68
|
+
end
|
69
|
+
|
70
|
+
def _call_scope_context(eval_context, query_params, proc)
|
71
|
+
if proc.arity == 1
|
72
|
+
eval_context.instance_exec(query_params, &proc)
|
73
|
+
else
|
74
|
+
eval_context.instance_exec(&proc)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
def current_scope #:nodoc:
|
80
|
+
ScopeRegistry.value_for(:current_scope, base_class.to_s)
|
81
|
+
end
|
82
|
+
|
83
|
+
def current_scope=(scope) #:nodoc:
|
84
|
+
ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope)
|
85
|
+
end
|
86
|
+
|
87
|
+
def all(new_var = nil)
|
88
|
+
var = new_var || (current_scope ? current_scope.node_identity : :n)
|
89
|
+
if current_scope
|
90
|
+
current_scope.new_link(var)
|
91
|
+
else
|
92
|
+
self.as(var)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class ScopeEvalContext
|
98
|
+
def initialize(target, query_proxy)
|
99
|
+
@query_proxy = query_proxy
|
100
|
+
@target = target
|
101
|
+
end
|
102
|
+
|
103
|
+
Neo4j::ActiveNode::Query::QueryProxy::METHODS.each do |method|
|
104
|
+
module_eval(%{
|
105
|
+
def #{method}(params={})
|
106
|
+
@target.all.scoping do
|
107
|
+
(@query_proxy || @target).#{method}(params)
|
108
|
+
end
|
109
|
+
end}, __FILE__, __LINE__)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
# Stolen from ActiveRecord
|
115
|
+
# https://github.com/rails/rails/blob/08754f12e65a9ec79633a605e986d0f1ffa4b251/activerecord/lib/active_record/scoping.rb#L57
|
116
|
+
class ScopeRegistry # :nodoc:
|
117
|
+
extend ActiveSupport::PerThreadRegistry
|
118
|
+
|
119
|
+
VALID_SCOPE_TYPES = [:current_scope, :ignore_default_scope]
|
120
|
+
|
121
|
+
def initialize
|
122
|
+
@registry = Hash.new { |hash, key| hash[key] = {} }
|
123
|
+
end
|
124
|
+
|
125
|
+
# Obtains the value for a given +scope_name+ and +variable_name+.
|
126
|
+
def value_for(scope_type, variable_name)
|
127
|
+
raise_invalid_scope_type!(scope_type)
|
128
|
+
@registry[scope_type][variable_name]
|
129
|
+
end
|
130
|
+
|
131
|
+
# Sets the +value+ for a given +scope_type+ and +variable_name+.
|
132
|
+
def set_value_for(scope_type, variable_name, value)
|
133
|
+
raise_invalid_scope_type!(scope_type)
|
134
|
+
@registry[scope_type][variable_name] = value
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def raise_invalid_scope_type!(scope_type)
|
140
|
+
return if VALID_SCOPE_TYPES.include?(scope_type)
|
141
|
+
|
142
|
+
fail ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Neo4j
|
2
|
+
module ActiveNode
|
3
|
+
module Unpersisted
|
4
|
+
# The values in this Hash are returned and used outside by reference
|
5
|
+
# so any modifications to the Array should be in-place
|
6
|
+
def deferred_create_cache
|
7
|
+
@deferred_create_cache ||= {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def defer_create(association_name, object, options = {})
|
11
|
+
clear_deferred_nodes_for_association(association_name) if options[:clear]
|
12
|
+
|
13
|
+
deferred_nodes_for_association(association_name) << object
|
14
|
+
end
|
15
|
+
|
16
|
+
def deferred_nodes_for_association(association_name)
|
17
|
+
deferred_create_cache[association_name.to_sym] ||= []
|
18
|
+
end
|
19
|
+
|
20
|
+
def pending_deferred_creations?
|
21
|
+
!deferred_create_cache.values.all?(&:empty?)
|
22
|
+
end
|
23
|
+
|
24
|
+
def clear_deferred_nodes_for_association(association_name)
|
25
|
+
deferred_nodes_for_association(association_name.to_sym).clear
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def process_unpersisted_nodes!
|
31
|
+
deferred_create_cache.each do |association_name, nodes|
|
32
|
+
association_proxy = association_proxy(association_name)
|
33
|
+
|
34
|
+
nodes.each do |node|
|
35
|
+
if node.respond_to?(:changed?)
|
36
|
+
node.save if node.changed? || !node.persisted?
|
37
|
+
fail "Unable to defer node persistence, could not save #{node.inspect}" unless node.persisted?
|
38
|
+
end
|
39
|
+
|
40
|
+
association_proxy << node
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
@deferred_create_cache = {}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Neo4j
|
2
|
+
module ActiveNode
|
3
|
+
# This mixin replace the original save method and performs validation before the save.
|
4
|
+
module Validations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
include Neo4j::Shared::Validations
|
7
|
+
|
8
|
+
|
9
|
+
# @return [Boolean] true if valid
|
10
|
+
def valid?(context = nil)
|
11
|
+
context ||= (new_record? ? :create : :update)
|
12
|
+
super(context)
|
13
|
+
errors.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def validates_uniqueness_of(*attr_names)
|
18
|
+
validates_with UniquenessValidator, _merge_attributes(attr_names)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
class UniquenessValidator < ::ActiveModel::EachValidator
|
24
|
+
def initialize(options)
|
25
|
+
super(options.reverse_merge(case_sensitive: true))
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate_each(record, attribute, value)
|
29
|
+
return unless found(record, attribute, value).exists?
|
30
|
+
|
31
|
+
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(value: value))
|
32
|
+
end
|
33
|
+
|
34
|
+
def found(record, attribute, value)
|
35
|
+
conditions = scope_conditions(record)
|
36
|
+
|
37
|
+
# TODO: Added as find(:name => nil) throws error
|
38
|
+
value = '' if value.nil?
|
39
|
+
|
40
|
+
conditions[attribute] = options[:case_sensitive] ? value : /#{Regexp.escape(value.to_s)}/i
|
41
|
+
|
42
|
+
found = record.class.as(:result).where(conditions)
|
43
|
+
found = found.where_not(neo_id: record.neo_id) if record._persisted_obj
|
44
|
+
found
|
45
|
+
end
|
46
|
+
|
47
|
+
def message(instance)
|
48
|
+
super || 'has already been taken'
|
49
|
+
end
|
50
|
+
|
51
|
+
def scope_conditions(instance)
|
52
|
+
Array(options[:scope] || []).inject({}) do |conditions, key|
|
53
|
+
conditions.merge(key => instance[key])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module Neo4j
|
2
|
+
# Makes Neo4j nodes and relationships behave like ActiveRecord objects.
|
3
|
+
# By including this module in your class it will create a mapping for the node to your ruby class
|
4
|
+
# by using a Neo4j Label with the same name as the class. When the node is loaded from the database it
|
5
|
+
# will check if there is a ruby class for the labels it has.
|
6
|
+
# If there Ruby class with the same name as the label then the Neo4j node will be wrapped
|
7
|
+
# in a new object of that class.
|
8
|
+
#
|
9
|
+
# = ClassMethods
|
10
|
+
# * {Neo4j::ActiveNode::Labels::ClassMethods} defines methods like: <tt>index</tt> and <tt>find</tt>
|
11
|
+
# * {Neo4j::ActiveNode::Persistence::ClassMethods} defines methods like: <tt>create</tt> and <tt>create!</tt>
|
12
|
+
# * {Neo4j::ActiveNode::Property::ClassMethods} defines methods like: <tt>property</tt>.
|
13
|
+
#
|
14
|
+
# @example Create a Ruby wrapper for a Neo4j Node
|
15
|
+
# class Company
|
16
|
+
# include Neo4j::ActiveNode
|
17
|
+
# property :name
|
18
|
+
# end
|
19
|
+
# company = Company.new
|
20
|
+
# company.name = 'My Company AB'
|
21
|
+
# Company.save
|
22
|
+
#
|
23
|
+
module ActiveNode
|
24
|
+
extend ActiveSupport::Concern
|
25
|
+
|
26
|
+
MARSHAL_INSTANCE_VARIABLES = [:@attributes, :@_persisted_obj, :@default_property_value]
|
27
|
+
|
28
|
+
include Neo4j::Shared
|
29
|
+
include Neo4j::Shared::Identity
|
30
|
+
include Neo4j::Shared::Marshal
|
31
|
+
include Neo4j::ActiveNode::Initialize
|
32
|
+
include Neo4j::ActiveNode::IdProperty
|
33
|
+
include Neo4j::Shared::SerializedProperties
|
34
|
+
include Neo4j::ActiveNode::Property
|
35
|
+
include Neo4j::ActiveNode::Reflection
|
36
|
+
include Neo4j::ActiveNode::Persistence
|
37
|
+
include Neo4j::ActiveNode::Validations
|
38
|
+
include Neo4j::ActiveNode::Callbacks
|
39
|
+
include Neo4j::ActiveNode::Query
|
40
|
+
include Neo4j::ActiveNode::Labels
|
41
|
+
include Neo4j::ActiveNode::Rels
|
42
|
+
include Neo4j::ActiveNode::Unpersisted
|
43
|
+
include Neo4j::ActiveNode::HasN
|
44
|
+
include Neo4j::ActiveNode::Scope
|
45
|
+
include Neo4j::ActiveNode::Dependent
|
46
|
+
include Neo4j::ActiveNode::Enum
|
47
|
+
include Neo4j::Shared::PermittedAttributes
|
48
|
+
|
49
|
+
def initialize(args = nil)
|
50
|
+
args = sanitize_input_parameters(args)
|
51
|
+
super(args)
|
52
|
+
end
|
53
|
+
|
54
|
+
def neo4j_obj
|
55
|
+
_persisted_obj || fail('Tried to access native neo4j object on a non persisted object')
|
56
|
+
end
|
57
|
+
|
58
|
+
module ClassMethods
|
59
|
+
def nodeify(object)
|
60
|
+
if object.is_a?(::Neo4j::ActiveNode) || object.nil?
|
61
|
+
object
|
62
|
+
else
|
63
|
+
self.find(object)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
included do
|
69
|
+
include Neo4j::Timestamps if Neo4j::Config[:record_timestamps]
|
70
|
+
|
71
|
+
def self.inherited(other)
|
72
|
+
Neo4j::ActiveNode::Labels.clear_wrapped_models
|
73
|
+
|
74
|
+
inherit_id_property(other)
|
75
|
+
attributes.each_pair do |k, v|
|
76
|
+
other.inherit_property k.to_sym, v.clone, declared_properties[k].options
|
77
|
+
end
|
78
|
+
|
79
|
+
Neo4j::ActiveNode::Labels.add_wrapped_class(other)
|
80
|
+
super
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.inherit_id_property(other)
|
84
|
+
Neo4j::Session.on_next_session_available do |_|
|
85
|
+
next if other.manual_id_property? || !self.id_property?
|
86
|
+
id_prop = self.id_property_info
|
87
|
+
conf = id_prop[:type].empty? ? {auto: :uuid} : id_prop[:type]
|
88
|
+
other.id_property id_prop[:name], conf
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
Neo4j::Session.on_next_session_available do |_|
|
93
|
+
next if manual_id_property?
|
94
|
+
id_property :uuid, auto: :uuid unless self.id_property?
|
95
|
+
|
96
|
+
name = Neo4j::Config[:id_property]
|
97
|
+
type = Neo4j::Config[:id_property_type]
|
98
|
+
value = Neo4j::Config[:id_property_type_value]
|
99
|
+
id_property(name, type => value) if name && type && value
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
ActiveSupport.run_load_hooks(:active_node, self)
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Neo4j
|
2
|
+
module ActiveRel
|
3
|
+
module Callbacks #:nodoc:
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
include Neo4j::Shared::Callbacks
|
6
|
+
|
7
|
+
def save(*args)
|
8
|
+
unless _persisted_obj || (from_node.respond_to?(:neo_id) && to_node.respond_to?(:neo_id))
|
9
|
+
fail Neo4j::ActiveRel::Persistence::RelInvalidError, 'from_node and to_node must be node objects'
|
10
|
+
end
|
11
|
+
super(*args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Neo4j::ActiveRel
|
2
|
+
module Initialize
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Neo4j::Shared::Initialize
|
5
|
+
|
6
|
+
# called when loading the rel from the database
|
7
|
+
# @param [Neo4j::Embedded::EmbeddedRelationship, Neo4j::Server::CypherRelationship] persisted_rel properties of this relationship
|
8
|
+
# @param [Neo4j::Relationship] from_node_id The neo_id of the starting node of this rel
|
9
|
+
# @param [Neo4j::Relationship] to_node_id The neo_id of the ending node of this rel
|
10
|
+
# @param [String] type the relationship type
|
11
|
+
def init_on_load(persisted_rel, from_node_id, to_node_id, type)
|
12
|
+
@rel_type = type
|
13
|
+
@_persisted_obj = persisted_rel
|
14
|
+
changed_attributes && changed_attributes.clear
|
15
|
+
@attributes = convert_and_assign_attributes(persisted_rel.props)
|
16
|
+
load_nodes(from_node_id, to_node_id)
|
17
|
+
end
|
18
|
+
|
19
|
+
def init_on_reload(unwrapped_reloaded)
|
20
|
+
@attributes = nil
|
21
|
+
init_on_load(unwrapped_reloaded,
|
22
|
+
unwrapped_reloaded._start_node_id,
|
23
|
+
unwrapped_reloaded._end_node_id,
|
24
|
+
unwrapped_reloaded.rel_type)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Neo4j::ActiveRel::Persistence
|
2
|
+
# This class builds and executes a Cypher query, using information from the graph objects to determine
|
3
|
+
# whether they need to be created simultaneously.
|
4
|
+
# It keeps the rel instance from being responsible for inspecting the nodes or talking with Shared::QueryFactory.
|
5
|
+
class QueryFactory
|
6
|
+
NODE_SYMBOLS = [:from_node, :to_node]
|
7
|
+
attr_reader :from_node, :to_node, :rel, :unwrapped_rel
|
8
|
+
|
9
|
+
def initialize(from_node, to_node, rel)
|
10
|
+
@from_node = from_node
|
11
|
+
@to_node = to_node
|
12
|
+
@rel = rel
|
13
|
+
end
|
14
|
+
|
15
|
+
# TODO: This feels like it should also wrap the rel, but that is handled in Neo4j::ActiveRel::Persistence at the moment.
|
16
|
+
# Builds and executes the query using the objects giving during init.
|
17
|
+
# It holds the process:
|
18
|
+
# * Execute node callbacks if needed
|
19
|
+
# * Create and execute the query
|
20
|
+
# * Mix the query response into the unpersisted objects given during init
|
21
|
+
def build!
|
22
|
+
node_before_callbacks! do
|
23
|
+
res = query_factory(rel, rel_id, iterative_query).query.unwrapped.return(*unpersisted_return_ids).first
|
24
|
+
node_symbols.each { |n| wrap!(send(n), res, n) }
|
25
|
+
@unwrapped_rel = res.send(rel_id)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def rel_id
|
32
|
+
@rel_id ||= rel.rel_identifier
|
33
|
+
end
|
34
|
+
|
35
|
+
# Node callbacks only need to be executed if the node is not persisted. We let the `conditional_callback` method do the work,
|
36
|
+
# we only have to give it the type of callback we expect to be run and the condition which, if true, will prevent it from executing.
|
37
|
+
def node_before_callbacks!
|
38
|
+
validate_unpersisted_nodes!
|
39
|
+
from_node.conditional_callback(:create, from_node.persisted?) do
|
40
|
+
to_node.conditional_callback(:create, to_node.persisted?) do
|
41
|
+
yield
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def validate_unpersisted_nodes!
|
47
|
+
node_symbols.each do |s|
|
48
|
+
obj = send(s)
|
49
|
+
next if obj.persisted?
|
50
|
+
fail RelCreateFailedError, "Cannot create rel with unpersisted, invalid #{s}" unless obj.valid?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Each node must be either created or matched before the relationship can be created. This class does not know or care about
|
55
|
+
# how that happens, it just knows that it needs a usable Neo4j::Core::Query object to do that.
|
56
|
+
# This method is "iterative" because it creates one factory for each node but the second builds upon the first.
|
57
|
+
def iterative_query
|
58
|
+
node_symbols.inject(false) do |iterative_query, sym|
|
59
|
+
obj = send(sym)
|
60
|
+
query_factory(obj, sym, iterative_query)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Isolates the dependency to the shared class. This has an awareness of Neo4j::Core::Query and will match or create
|
65
|
+
# based on the current state of the object passed in.
|
66
|
+
def query_factory(obj, sym, factory = false)
|
67
|
+
Neo4j::Shared::QueryFactory.create(obj, sym).tap do |factory_instance|
|
68
|
+
factory_instance.base_query = factory.blank? ? false : factory.query
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [Array<Symbol>] The Cypher identifiers that will be returned from the query.
|
73
|
+
# We only need to return objects from our query that were created during it, otherwise we impact performance.
|
74
|
+
def unpersisted_return_ids
|
75
|
+
[rel_id].tap do |result|
|
76
|
+
node_symbols.each { |k| result << k unless send(k).persisted? }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# @param [Neo4j::ActiveNode] node A node, persisted or unpersisted
|
81
|
+
# @param [Struct] res The result of calling `return` on a Neo4j::Core::Query object. It responds to the same keys
|
82
|
+
# as our graph objects. If the object is unpersisted and was created during the query, the unwrapped node is mixed
|
83
|
+
# in, making the object reflect as "persisted".
|
84
|
+
# @param [Symbol] key :from_node or :to_node, the object to request from the response.
|
85
|
+
def wrap!(node, res, key)
|
86
|
+
return if node.persisted? || !res.respond_to?(key)
|
87
|
+
unwrapped = res.send(key)
|
88
|
+
node.init_on_load(unwrapped, unwrapped.props)
|
89
|
+
end
|
90
|
+
|
91
|
+
def node_symbols
|
92
|
+
self.class::NODE_SYMBOLS
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|