Skip to content

Commit

Permalink
Throw exception if trying to find relationships with illegal arguments
Browse files Browse the repository at this point in the history
…closes #189

Example:  a_person.friends("a string")  will now raise an exception
  • Loading branch information
andreasronge committed Apr 18, 2012
1 parent 39b2101 commit f0f4023
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,7 +1,7 @@
source :gemcutter


# gem "neo4j-wrapper", :path => "/home/andreas/projects/neo4j-wrapper"
#gem "neo4j-wrapper", :path => "/home/andreas/projects/neo4j-wrapper"

gemspec

Expand Down
1 change: 1 addition & 0 deletions lib/neo4j/rails/has_n.rb
Expand Up @@ -151,6 +151,7 @@ def define_has_n_methods_for(rel_type, options) #:nodoc:
def #{rel_type}(cypher_hash_query = nil, &cypher_block)
dsl = _decl_rels_for(:'#{rel_type}')
if cypher_hash_query || cypher_block
raise "Expected a hash, can't translated to cypher where statements" if cypher_hash_query && !cypher_hash_query.is_a?(Hash)
Neo4j::Wrapper::HasN::Nodes.new(self, dsl, cypher_hash_query, &cypher_block)
else
storage = _create_or_get_storage_for_decl_rels(dsl)
Expand Down
1 change: 1 addition & 0 deletions lib/neo4j/rails/relationships/node_dsl.rb
Expand Up @@ -142,6 +142,7 @@ def find(*args, &block)
#
def all(*args)
unless args.empty?
raise "Illegal argument, expected a node" unless args.first.kind_of?(Neo4j::NodeMixin)
enum = Enumerator.new(@storage, :each_node, @dir).find { |n| n == args.first }
else
enum = Enumerator.new(@storage, :each_node, @dir)
Expand Down
7 changes: 7 additions & 0 deletions spec/integration/active_record_style_relationships_spec.rb
Expand Up @@ -131,6 +131,13 @@ class ModelRelationship1 < Neo4j::Rails::Relationship
@actor.acted_in.find(@movie_1).should_not be_nil
end

it "can not find anything else except nodes" do
lambda{@actor.acted_in.find("bla")}.should raise_error
lambda{@actor.acted_in.first("bla")}.should raise_error
lambda{@actor.acted_in.all("bla")}.should raise_error
lambda{@actor.acted_in.all(:bla => 42)}.should raise_error
end

it "find a child node by id" do
@actor.acted_in.find(@movie_1.id).should_not be_nil
end
Expand Down
7 changes: 6 additions & 1 deletion spec/integration/finders_spec.rb
Expand Up @@ -8,7 +8,7 @@
property :name, :index => :exact
property :age, :type => Fixnum, :index => :exact
validates_presence_of :name

has_n(:items)
def to_s
name
end
Expand Down Expand Up @@ -79,6 +79,11 @@ def non_findable_model_allocated_ids
findable_class.find(-99).should be_nil
end

it "should raise an exception when trying to find related nodes with a string" do
lambda{findable_class.new.items("bla")}.should raise_error
lambda{findable_class.new.items.all("bla")}.should raise_error
end

it "should return nil when passed " do
findable_class.find(-99).should be_nil
end
Expand Down

0 comments on commit f0f4023

Please sign in to comment.