File tree 6 files changed +82
-1
lines changed
elasticsearch-persistence
6 files changed +82
-1
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,8 @@ Gem::Specification.new do |s|
22
22
s . rdoc_options = [ "--charset=UTF-8" ]
23
23
24
24
s . add_dependency "elasticsearch" , '> 0.4'
25
- s . add_dependency "active_support"
25
+ s . add_dependency "elasticsearch-model" , '>= 0.1'
26
+ s . add_dependency "activesupport" , '> 3'
26
27
s . add_dependency "hashie"
27
28
28
29
s . add_development_dependency "bundler" , "~> 1.5"
Original file line number Diff line number Diff line change 1
1
require 'elasticsearch'
2
+ require 'elasticsearch/model/indexing'
2
3
require 'hashie'
3
4
4
5
require 'active_support/inflector'
Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ module Repository
9
9
include Elasticsearch ::Persistence ::Repository ::Find
10
10
include Elasticsearch ::Persistence ::Repository ::Search
11
11
12
+ include Elasticsearch ::Model ::Indexing ::ClassMethods
13
+
12
14
def new ( options = { } , &block )
13
15
Elasticsearch ::Persistence ::Repository ::Class . new options , &block
14
16
end ; module_function :new
Original file line number Diff line number Diff line change @@ -11,6 +11,18 @@ def klass=klass
11
11
@klass = klass
12
12
end
13
13
14
+ def index_name name = nil
15
+ @index_name = name || @index_name || self . class . to_s . underscore . gsub ( /\/ / , '-' )
16
+ end
17
+
18
+ def index_name = ( name )
19
+ @index_name = name
20
+ end
21
+
22
+ def document_type
23
+ klass . to_s . underscore
24
+ end
25
+
14
26
def __get_klass_from_type ( type )
15
27
klass = type . classify
16
28
klass . constantize
Original file line number Diff line number Diff line change
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch ::Persistence ::RepositoryIndexingTest < Test ::Unit ::TestCase
4
+ context "The repository index methods" do
5
+ class MyDocument ; end
6
+
7
+ setup do
8
+ @shoulda_subject = Class . new ( ) { include Elasticsearch ::Model ::Indexing ::ClassMethods } . new
9
+ @shoulda_subject . stubs ( :index_name ) . returns ( 'my_index' )
10
+ @shoulda_subject . stubs ( :document_type ) . returns ( 'my_document' )
11
+ end
12
+
13
+ should "have the convenience index management methods" do
14
+ %w( create_index! delete_index! refresh_index! ) . each do |method |
15
+ assert_respond_to subject , method
16
+ end
17
+ end
18
+
19
+ context "mappings" do
20
+ should "configure the mappings for the type" do
21
+ subject . mappings do
22
+ indexes :title
23
+ end
24
+
25
+ assert_equal ( { :"my_document" => { :properties => { :title => { :type => "string" } } } } , subject . mappings . to_hash )
26
+ end
27
+ end
28
+
29
+ context "settings" do
30
+ should "configure the settings for the index" do
31
+ subject . settings foo : 'bar'
32
+ assert_equal ( { foo : 'bar' } , subject . settings . to_hash )
33
+ end
34
+ end
35
+
36
+ end
37
+ end
Original file line number Diff line number Diff line change @@ -65,5 +65,33 @@ module ::Foo; class Bar; end; end
65
65
end
66
66
end
67
67
68
+ context "index_name" do
69
+ should "default to the class name" do
70
+ subject . instance_eval do
71
+ def self . class
72
+ 'FakeRepository'
73
+ end
74
+ end
75
+
76
+ assert_equal 'fake_repository' , subject . index_name
77
+ end
78
+
79
+ should "be settable" do
80
+ subject . index_name = 'foobar1'
81
+ assert_equal 'foobar1' , subject . index_name
82
+
83
+ subject . index_name 'foobar2'
84
+ assert_equal 'foobar2' , subject . index_name
85
+ end
86
+ end
87
+
88
+ context "document_type" do
89
+ should "default to klass" do
90
+ assert_equal '' , subject . document_type
91
+
92
+ subject . klass Foobar
93
+ assert_equal 'foobar' , subject . document_type
94
+ end
95
+ end
68
96
end
69
97
end
You can’t perform that action at this time.
0 commit comments