Skip to content

Commit c4b1d41

Browse files
committed
[STORE] Added the methods from the "elasticsearch-model" gem
Included `Elasticsearch::Model::Indexing::ClassMethods` to support setting the index name and document type, and to allow configuring the mappings and settings for the index. See: https://github.com/elasticsearch/elasticsearch-rails/blob/6f4a57a/elasticsearch-model/lib/elasticsearch/model/indexing.rb
1 parent 485a4f9 commit c4b1d41

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

elasticsearch-persistence/elasticsearch-persistence.gemspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Gem::Specification.new do |s|
2222
s.rdoc_options = [ "--charset=UTF-8" ]
2323

2424
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'
2627
s.add_dependency "hashie"
2728

2829
s.add_development_dependency "bundler", "~> 1.5"

elasticsearch-persistence/lib/elasticsearch/persistence.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'elasticsearch'
2+
require 'elasticsearch/model/indexing'
23
require 'hashie'
34

45
require 'active_support/inflector'

elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Repository
99
include Elasticsearch::Persistence::Repository::Find
1010
include Elasticsearch::Persistence::Repository::Search
1111

12+
include Elasticsearch::Model::Indexing::ClassMethods
13+
1214
def new(options={}, &block)
1315
Elasticsearch::Persistence::Repository::Class.new options, &block
1416
end; module_function :new

elasticsearch-persistence/lib/elasticsearch/persistence/repository/naming.rb

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ def klass=klass
1111
@klass = klass
1212
end
1313

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+
1426
def __get_klass_from_type(type)
1527
klass = type.classify
1628
klass.constantize
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

elasticsearch-persistence/test/unit/repository_naming_test.rb

+28
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,33 @@ module ::Foo; class Bar; end; end
6565
end
6666
end
6767

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
6896
end
6997
end

0 commit comments

Comments
 (0)