lib/neo4j/active_node/has_n.rb in neo4j-3.0.0.alpha.11 vs lib/neo4j/active_node/has_n.rb in neo4j-3.0.0.rc.2

- old
+ new

@@ -1,41 +1,51 @@ module Neo4j::ActiveNode - module HasN - extend ActiveSupport::Concern +module HasN + extend ActiveSupport::Concern - module ClassMethods + class NonPersistedNodeError < StandardError; end - def has_association?(name) - !!associations[name] - end + module ClassMethods - def associations - @associations || {} - end + def has_association?(name) + !!associations[name.to_sym] + end - # make sure the inherited classes inherit the <tt>_decl_rels</tt> hash - def inherited(klass) - klass.instance_variable_set(:@associations, associations.clone) + def associations + @associations || {} + end - super - end + # make sure the inherited classes inherit the <tt>_decl_rels</tt> hash + def inherited(klass) + klass.instance_variable_set(:@associations, associations.clone) - def has_many(direction, name, options = {}) - name = name.to_sym + super + end - association = Neo4j::ActiveNode::HasN::Association.new(:has_many, direction, name, options) - name = name.to_sym + def has_many(direction, name, options = {}) + name = name.to_sym - @associations ||= {} - @associations[name] = association + association = Neo4j::ActiveNode::HasN::Association.new(:has_many, direction, name, options) - target_class_name = association.target_class_name || 'nil' + @associations ||= {} + @associations[name] = association - # TODO: Make assignment more efficient? (don't delete nodes when they are being assigned) - module_eval(%Q{ - def #{name}(node = nil, rel = nil) - Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name}, self.class.associations[#{name.inspect}], session: self.class.neo4j_session, start_object: self, node: node, rel: rel) + target_class_name = association.target_class_name || 'nil' + + # TODO: Make assignment more efficient? (don't delete nodes when they are being assigned) + module_eval(%Q{ + def #{name}(node = nil, rel = nil) + return [].freeze unless self.persisted? + Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name}, + self.class.associations[#{name.inspect}], + { + session: self.class.neo4j_session, + start_object: self, + node: node, + rel: rel, + context: '#{self.name}##{name}' + }) end def #{name}=(other_nodes) #{name}(nil, :r).query_as(:n).delete(:r).exec @@ -48,27 +58,36 @@ #{name}(nil, :r).pluck(:r) end}, __FILE__, __LINE__) instance_eval(%Q{ def #{name}(node = nil, rel = nil) - Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name}, @associations[#{name.inspect}], session: self.neo4j_session, query_proxy: self.query_proxy, node: node, rel: rel) + context = (self.query_proxy && self.query_proxy.context ? self.query_proxy.context : '#{self.name}') + '##{name}' + Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name}, + @associations[#{name.inspect}], + { + session: self.neo4j_session, + query_proxy: self.query_proxy, + node: node, + rel: rel, + context: context + }) end}, __FILE__, __LINE__) end def has_one(direction, name, options = {}) name = name.to_sym association = Neo4j::ActiveNode::HasN::Association.new(:has_one, direction, name, options) - name = name.to_sym @associations ||= {} @associations[name] = association target_class_name = association.target_class_name || 'nil' module_eval(%Q{ def #{name}=(other_node) + raise(Neo4j::ActiveNode::HasN::NonPersistedNodeError, 'Unable to create relationship with non-persisted nodes') unless self.persisted? #{name}_query_proxy(rel: :r).query_as(:n).delete(:r).exec #{name}_query_proxy << other_node end def #{name}_query_proxy(options = {}) @@ -78,19 +97,23 @@ def #{name}_rel #{name}_query_proxy(rel: :r).pluck(:r).first end def #{name}(node = nil, rel = nil) - #{name}_query_proxy(node: node, rel: rel).first + return nil unless self.persisted? + #{name}_query_proxy(node: node, rel: rel, context: '#{self.name}##{name}').first end}, __FILE__, __LINE__) instance_eval(%Q{ def #{name}_query_proxy(options = {}) - Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name}, @associations[#{name.inspect}], {session: self.neo4j_session}.merge(options)) + Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class_name}, + @associations[#{name.inspect}], + {session: self.neo4j_session}.merge(options)) end def #{name}(node = nil, rel = nil) - #{name}_query_proxy(query_proxy: self.query_proxy, node: node, rel: rel) + context = (self.query_proxy && self.query_proxy.context ? self.query_proxy.context : '#{self.name}') + '##{name}' + #{name}_query_proxy(query_proxy: self.query_proxy, node: node, rel: rel, context: context) end}, __FILE__, __LINE__) end end