Skip to content

Commit

Permalink
has_n and has_one method generate class method returning the name of …
Browse files Browse the repository at this point in the history
…the relationship as a Symbol, closes #170
  • Loading branch information
andreasronge committed Apr 19, 2012
1 parent cc5aa10 commit f876af7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/neo4j/rails/has_n.rb
Expand Up @@ -143,6 +143,12 @@ def create_#{rel_type}!(attr)
RUBY
end

instance_eval <<-RUBY, __FILE__, __LINE__
def #{rel_type}
_decl_rels[:'#{rel_type}'].rel_type.to_sym
end
RUBY

_decl_rels[rel_type.to_sym] = Neo4j::Wrapper::HasN::DeclRel.new(rel_type, true, self)
end

Expand Down Expand Up @@ -198,7 +204,7 @@ def #{rel_type}_rels

instance_eval <<-RUBY, __FILE__, __LINE__
def #{rel_type}
_decl_rels[:'#{rel_type}'].rel_type.to_s
_decl_rels[:'#{rel_type}'].rel_type.to_sym
end
RUBY

Expand Down
14 changes: 14 additions & 0 deletions spec/unit/has_n_spec.rb
Expand Up @@ -57,9 +57,23 @@ def wrapper
other_klass.new
end

describe "has_one(:best_friend)" do
it "has a best_friend class method for the relationship type" do
klass.has_one(:best_friend).to(other_klass)
klass.has_one(:stuff)

klass.stuff.should == :stuff
klass.best_friend.should == :"#{klass}#best_friend"
end
end

describe "has_n(:friends).to(Other)" do
before { klass.has_n(:friends).to(other_klass) }

it "has a friend class method for the relationship type" do
klass.friends.should == :"#{klass}#friends"
end

describe "node.friends.build(:name => 'foo')" do
it "initialize a node with give properties" do
f = node.friends.build(:name => 'foo')
Expand Down

0 comments on commit f876af7

Please sign in to comment.