Skip to content

Commit 63da64a

Browse files
committed
[STORE] Added, that index_name is inferred from the including class
class NoteRepository include Elasticsearch::Persistence::Repository end NoteRepository.index_name => "note_repository"
1 parent c391a3b commit 63da64a

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def respond_to?(method_name, include_private=false)
1212

1313
module Repository
1414
def self.included(base)
15-
gateway = Elasticsearch::Persistence::Repository::Class.new
15+
gateway = Elasticsearch::Persistence::Repository::Class.new host: base
1616

1717
base.class_eval do
1818
define_method :gateway do

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def initialize(options={}, &block)
1818
@options = options
1919
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
2020
end
21+
22+
def host
23+
options[:host]
24+
end
2125
end
2226

2327
end

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ def klass=klass
1212
end
1313

1414
def index_name name=nil
15-
@index_name = name || @index_name || self.class.to_s.underscore.gsub(/\//, '-')
15+
@index_name = name || @index_name || begin
16+
if respond_to?(:host) && host && host.is_a?(Module)
17+
self.host.to_s.underscore.gsub(/\//, '-')
18+
else
19+
self.class.to_s.underscore.gsub(/\//, '-')
20+
end
21+
end
1622
end; alias :index :index_name
1723

1824
def index_name=(name)

elasticsearch-persistence/test/unit/repository_module_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,9 @@ def serialize(document)
108108
end
109109
end
110110

111+
should_eventually "configure the index name in the shortcut initializer" do
112+
assert_equal 'repository', Elasticsearch::Persistence::Repository.new.index_name
113+
end
114+
111115
end
112116
end

elasticsearch-persistence/test/unit/repository_naming_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ def self.class
8888
subject.index_name = 'foobar1'
8989
assert_equal 'foobar1', subject.index
9090
end
91+
92+
should "be inferred from the host class" do
93+
class ::MySpecialRepository; end
94+
subject.define_singleton_method(:host) { MySpecialRepository }
95+
assert_equal 'my_special_repository', subject.index_name
96+
end
9197
end
9298

9399
context "document_type" do

0 commit comments

Comments
 (0)