diff --git a/lib/deja/cast.rb b/lib/deja/cast.rb index c01cb01..919bd37 100644 --- a/lib/deja/cast.rb +++ b/lib/deja/cast.rb @@ -43,7 +43,7 @@ def sans_initial_node(relation_hash, initial_node) memo[k] = rel[:rel][k] memo end - relationship = rel_class.new(rel[:rel][:type], initial_node, related_node, :out, rel_attributes) + relationship = rel_class.new(rel[:rel][:type], initial_node, related_node, rel_attributes) last_relationship = relationship relationship.instance_variable_set('@id', rel[:rel][:id]) [related_node, relationship] diff --git a/lib/deja/relationship.rb b/lib/deja/relationship.rb index 69127bd..4128d93 100644 --- a/lib/deja/relationship.rb +++ b/lib/deja/relationship.rb @@ -31,7 +31,7 @@ def valid_direction?(from_type=nil, to_type=nil) end end - # initialize(label, start_node, end_node, direction, options = {}) + # initialize(label, start_node, end_node, options = {}) # the method below ensures that the relationship configuration is done between before_initialize and after_initialize def initialize(*args) super(*args) do |config| @@ -67,8 +67,8 @@ def destroy true end - def add_to_index(index, key, value, unique = false) - Deja.add_relationship_to_index(index, key, value) + def add_to_index(index, key, value, space = nil) + Deja.add_relationship_to_index(index, key, value, @id) end def remove_from_index(*args) diff --git a/lib/deja/schema_generator.rb b/lib/deja/schema_generator.rb index b62d3b4..aa6a612 100644 --- a/lib/deja/schema_generator.rb +++ b/lib/deja/schema_generator.rb @@ -33,7 +33,8 @@ def attr_accessorize(name, opts) end end - def create_index_methods(key, values = nil, unique = nil) + def create_index_methods(key, values = nil, unique = false) + define_attribute_method(key) @@indexed_attributes[self.name] ||= [] @@indexed_attributes[self.name] << key diff --git a/spec/index_spec.rb b/spec/index_spec.rb index 6300577..90eb880 100644 --- a/spec/index_spec.rb +++ b/spec/index_spec.rb @@ -11,7 +11,7 @@ class Friends < Relationship before :each do @first_node = FactoryGirl.create(:person); @second_node = FactoryGirl.create(:person); - @relationship = Friends.new(:friends, @first_node, @second_node, :out).create! + @relationship = Friends.new(:friends, @first_node, @second_node).create! Deja.create_node_index('test_node_index') Deja.create_relationship_index('test_relationship_index') end diff --git a/spec/relationship_spec.rb b/spec/relationship_spec.rb index c1c5415..d2fdf40 100644 --- a/spec/relationship_spec.rb +++ b/spec/relationship_spec.rb @@ -4,9 +4,11 @@ class FriendsWith < Relationship attribute :uuid, Integer - attribute :name, String + attribute :name, String, :index => true end +Deja.create_relationship_index('idx_FriendsWith') + describe Node do before :each do @first_node = FactoryGirl.create(:person); @@ -16,6 +18,10 @@ class FriendsWith < Relationship @relationship = FriendsWith.new(:friends_with, @first_node, @second_node, :out, @relationship_properties) end + after :each do + Deja.neo.execute_query("START n=node(*) MATCH n-[r?]->() WHERE ID(n) <> 0 DELETE r DELETE n") + end + describe ".save" do context "with a relationship that has not yet been saved to the graph" do it "should create a new relationship in the graph" do @@ -55,5 +61,15 @@ class FriendsWith < Relationship new_rel.end_node.should be_a(Node) end end + + context "given a relationship index which exists in the graph" do + it "should return a relationship object with related nodes" do + @relationship.save + new_rel = FriendsWith.find({:index => 'idx_FriendsWith', :key => 'name', :value => @relationship.name}) + new_rel.should be_a(Relationship) + new_rel.start_node.should be_a(Node) + new_rel.end_node.should be_a(Node) + end + end end end