From 4dba7c1c99cdec469ec0690fed802ab3438b3756 Mon Sep 17 00:00:00 2001 From: Andreas Ronge Date: Sat, 26 Feb 2011 07:03:33 +0100 Subject: [PATCH] Fixed bug: Index not working on a typed attribute for Neo4j::Rails::Model [#152 state:resolved] --- lib/neo4j/rails/finders.rb | 29 ++++++++++++++++++++--------- spec/rails/finders_spec.rb | 18 +++++++++++++----- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/neo4j/rails/finders.rb b/lib/neo4j/rails/finders.rb index 321b9560c..5320fbea1 100644 --- a/lib/neo4j/rails/finders.rb +++ b/lib/neo4j/rails/finders.rb @@ -11,16 +11,27 @@ module ClassMethods # overwrite the index method to add find_by_xxx class methods def index(*args) field = args.first - module_eval <<-RUBY, __FILE__, __LINE__ - def self.all_by_#{field}(value) - find_with_indexer("#{field}: \\"\#{value}\\"") - end - - def self.find_by_#{field}(value) - all_by_#{field}(value).first - end - RUBY + if self._decl_props[field.to_sym] && self._decl_props[field.to_sym][:type] == Fixnum + module_eval <<-RUBY, __FILE__, __LINE__ + def self.all_by_#{field}(value) + find_with_indexer(:#{field} => value) + end + def self.find_by_#{field}(value) + all_by_#{field}(value).first + end + RUBY + else + module_eval <<-RUBY, __FILE__, __LINE__ + def self.all_by_#{field}(value) + find_with_indexer("#{field}: \\"\#{value}\\"") + end + + def self.find_by_#{field}(value) + all_by_#{field}(value).first + end + RUBY + end super end diff --git a/spec/rails/finders_spec.rb b/spec/rails/finders_spec.rb index 66ba2f18e..750211ab1 100644 --- a/spec/rails/finders_spec.rb +++ b/spec/rails/finders_spec.rb @@ -1,24 +1,32 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper') class FindableModel < Neo4j::Rails::Model - property :name - index :name, :type => :exact - + property :name + property :age, :type => Fixnum + index :name + index :age + def to_s name end end describe "finders" do - subject { FindableModel.create!(:name => "Test 1") } + subject { FindableModel.create!(:name => "Test 1", :age => 4241) } before(:each) do @test_0 = FindableModel.create!(:name => "Test 0") @test_2 = FindableModel.create!(:name => "Test 2") - @test_3 = FindableModel.create!(:name => "Test 3") + @test_3 = FindableModel.create!(:name => "Test 3", :age => 3) @test_4 = FindableModel.create!(:name => "Test 1") end + context "index property with type Fixnum, :age, :type => Fixnum" do + it "find_by_age(a fixnum) should work because age is declared as a Fixnum" do + FindableModel.find_by_age(3).should == @test_3 + end + end + context "#close_lucene_connections" do it "sets the Thread.current[:neo4j_lucene_connection] to nil and close all lucene connections" do FindableModel.find('name: Test*')