lib/active_graph/node/has_n.rb in activegraph-10.0.0.pre.beta.4 vs lib/active_graph/node/has_n.rb in activegraph-10.0.0.pre.beta.5
- old
+ new
@@ -439,34 +439,38 @@
association_proxy(name, {node: node, rel: rel, labels: options[:labels]}.merge!(options))
end
end
def define_has_many_setter(name)
- define_method("#{name}=") do |other_nodes|
- association_proxy_cache.clear
-
- clear_deferred_nodes_for_association(name)
-
- ActiveGraph::Base.transaction { association_proxy(name).replace_with(other_nodes) }
- end
+ define_setter(name, "#{name}=")
end
def define_has_many_id_methods(name)
define_method_unless_defined("#{name.to_s.singularize}_ids") do
association_proxy(name).result_ids
end
- define_method_unless_defined("#{name.to_s.singularize}_ids=") do |ids|
- clear_deferred_nodes_for_association(name)
- association_proxy(name).replace_with(Array(ids).reject(&:blank?))
- end
+ define_setter(name, "#{name.to_s.singularize}_ids=")
define_method_unless_defined("#{name.to_s.singularize}_neo_ids") do
association_proxy(name).pluck(:neo_id)
end
end
+ def define_setter(name, setter_name)
+ define_method_unless_defined(setter_name) do |others|
+ association_proxy_cache.clear # TODO: Should probably just clear for this association...
+ clear_deferred_nodes_for_association(name)
+ others = Array(others).reject(&:blank?)
+ if persisted?
+ ActiveGraph::Base.transaction { association_proxy(name).replace_with(others) }
+ else
+ defer_create(name, others, clear: true)
+ end
+ end
+ end
+
def define_method_unless_defined(method_name, &block)
define_method(method_name, block) unless method_defined?(method_name)
end
def define_has_one_methods(name, association_options)
@@ -490,13 +494,11 @@
def define_has_one_id_methods(name)
define_method_unless_defined("#{name}_id") do
association_proxy(name).result_ids.first
end
- define_method_unless_defined("#{name}_id=") do |id|
- association_proxy(name).replace_with(id)
- end
+ define_setter(name, "#{name}_id=")
define_method_unless_defined("#{name}_neo_id") do
association_proxy(name).pluck(:neo_id).first
end
end
@@ -518,21 +520,11 @@
end
end
end
def define_has_one_setter(name)
- define_method("#{name}=") do |other_node|
- if persisted?
- other_node.save if other_node.respond_to?(:persisted?) && !other_node.persisted?
- association_proxy_cache.clear # TODO: Should probably just clear for this association...
- ActiveGraph::Base.transaction { association_proxy(name).replace_with(other_node) }
- # handle_non_persisted_node(other_node)
- else
- defer_create(name, other_node, clear: true)
- other_node
- end
- end
+ define_setter(name, "#{name}=")
end
def define_class_method(*args, &block)
klass = class << self; self; end
klass.instance_eval do
@@ -543,12 +535,11 @@
def association_query_proxy(name, options = {})
previous_query_proxy = options[:previous_query_proxy] || current_scope
query_proxy = previous_query_proxy || default_association_query_proxy
ActiveGraph::Node::Query::QueryProxy.new(association_target_class(name),
associations[name],
- {driver: neo4j_driver,
- query_proxy: query_proxy,
+ {query_proxy: query_proxy,
context: "#{query_proxy.context || self.name}##{name}",
optional: query_proxy.optional?,
association_labels: options[:labels],
source_object: query_proxy.source_object}.merge!(options)).tap do |query_proxy_result|
target_classes = association_target_classes(name)
@@ -576,10 +567,10 @@
target_classes_or_nil
end
def default_association_query_proxy
ActiveGraph::Node::Query::QueryProxy.new("::#{self.name}".constantize, nil,
- driver: neo4j_driver, query_proxy: nil, context: self.name.to_s)
+ query_proxy: nil, context: self.name.to_s)
end
def build_association(macro, direction, name, options)
options[:model_class] = options[:model_class].name if options[:model_class] == self
ActiveGraph::Node::HasN::Association.new(macro, direction, name, options).tap do |association|