Skip to content

Commit

Permalink
Fixed bug: Index not working on a typed attribute for Neo4j::Rails::M…
Browse files Browse the repository at this point in the history
…odel [#152 state:resolved]
  • Loading branch information
andreasronge committed Feb 26, 2011
1 parent d517942 commit 4dba7c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
29 changes: 20 additions & 9 deletions lib/neo4j/rails/finders.rb
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions 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*')
Expand Down

0 comments on commit 4dba7c1

Please sign in to comment.