Skip to content

Commit b8455db

Browse files
rymohrkarmi
authored andcommitted
[MODEL] Added support for inherited index names and doc types
This patch adds support for inheriting index_name and document_type on an opt-in basis: Elasticsearch::Model.inheritance_enabled = true class Animal < ActiveRecord::Base document_type 'mammal' index_name 'mammals' end class Dog < Animal end Animal.document_type # 'mammal' Animal.index_name # 'mammals' Dog.document_type # 'mammal' Dog.index_name # 'mammals' Closes elastic#332 Related: elastic#28, elastic#92, elastic#170, elastic#344
1 parent ceef80e commit b8455db

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

elasticsearch-model/lib/elasticsearch/model.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ class << self
131131
end
132132

133133
module ClassMethods
134-
135134
# Get the client common for all models
136135
#
137136
# @example Get the client
@@ -181,6 +180,24 @@ def search(query_or_payload, models=[], options={})
181180
request = Searching::SearchRequest.new(models, query_or_payload, options)
182181
Response::Response.new(models, request)
183182
end
183+
184+
# Check if inheritance is enabled
185+
#
186+
# @note Inheritance is disabled by default.
187+
#
188+
def inheritance_enabled
189+
@inheritance_enabled ||= false
190+
end
191+
192+
# Enable inheritance of index_name and document_type
193+
#
194+
# @example Enable inheritance
195+
#
196+
# Elasticsearch::Model.inheritance_enabled = true
197+
#
198+
def inheritance_enabled=(inheritance_enabled)
199+
@inheritance_enabled = inheritance_enabled
200+
end
184201
end
185202
extend ClassMethods
186203

elasticsearch-model/lib/elasticsearch/model/naming.rb

+26-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def index_name name=nil, &block
3434
if @index_name.respond_to?(:call)
3535
@index_name.call
3636
else
37-
@index_name || self.model_name.collection.gsub(/\//, '-')
37+
@index_name || implicit(:index_name)
3838
end
3939
end
4040

@@ -58,7 +58,7 @@ def index_name=(name)
5858
# Article.document_type "my-article"
5959
#
6060
def document_type name=nil
61-
@document_type = name || @document_type || self.model_name.element
61+
@document_type = name || @document_type || implicit(:document_type)
6262
end
6363

6464

@@ -69,6 +69,30 @@ def document_type name=nil
6969
def document_type=(name)
7070
@document_type = name
7171
end
72+
73+
private
74+
75+
def implicit(prop)
76+
value = nil
77+
78+
if Elasticsearch::Model.inheritance_enabled
79+
self.ancestors.each do |klass|
80+
next if klass == self
81+
break if value = klass.respond_to?(prop) && klass.send(prop)
82+
end
83+
end
84+
85+
value || self.send("default_#{prop}")
86+
end
87+
88+
def default_index_name
89+
self.model_name.collection.gsub(/\//, '-')
90+
end
91+
92+
def default_document_type
93+
self.model_name.element
94+
end
95+
7296
end
7397

7498
module InstanceMethods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require "test_helper"
2+
3+
class Elasticsearch::Model::NamingInheritanceTest < Test::Unit::TestCase
4+
def setup
5+
Elasticsearch::Model.inheritance_enabled = true
6+
end
7+
8+
def teardown
9+
Elasticsearch::Model.inheritance_enabled = false
10+
end
11+
12+
context "Naming module with inheritance" do
13+
class ::TestBase
14+
extend ActiveModel::Naming
15+
16+
extend Elasticsearch::Model::Naming::ClassMethods
17+
include Elasticsearch::Model::Naming::InstanceMethods
18+
end
19+
20+
class ::Animal < ::TestBase
21+
extend ActiveModel::Naming
22+
23+
extend Elasticsearch::Model::Naming::ClassMethods
24+
include Elasticsearch::Model::Naming::InstanceMethods
25+
26+
index_name "mammals"
27+
document_type "mammal"
28+
end
29+
30+
class ::Dog < ::Animal
31+
end
32+
33+
module ::MyNamespace
34+
class Dog < ::Animal
35+
end
36+
end
37+
38+
should "return the default index_name" do
39+
assert_equal "test_bases", TestBase.index_name
40+
assert_equal "test_bases", TestBase.new.index_name
41+
end
42+
43+
should "return the explicit index_name" do
44+
assert_equal "mammals", Animal.index_name
45+
assert_equal "mammals", Animal.new.index_name
46+
end
47+
48+
should "return the ancestor index_name" do
49+
assert_equal "mammals", Dog.index_name
50+
assert_equal "mammals", Dog.new.index_name
51+
end
52+
53+
should "return the ancestor index_name for namespaced model" do
54+
assert_equal "mammals", ::MyNamespace::Dog.index_name
55+
assert_equal "mammals", ::MyNamespace::Dog.new.index_name
56+
end
57+
58+
should "return the default document_type" do
59+
assert_equal "test_base", TestBase.document_type
60+
assert_equal "test_base", TestBase.new.document_type
61+
end
62+
63+
should "return the explicit document_type" do
64+
assert_equal "mammal", Animal.document_type
65+
assert_equal "mammal", Animal.new.document_type
66+
end
67+
68+
should "return the ancestor document_type" do
69+
assert_equal "mammal", Dog.document_type
70+
assert_equal "mammal", Dog.new.document_type
71+
end
72+
73+
should "return the ancestor document_type for namespaced model" do
74+
assert_equal "mammal", ::MyNamespace::Dog.document_type
75+
assert_equal "mammal", ::MyNamespace::Dog.new.document_type
76+
end
77+
end
78+
end

0 commit comments

Comments
 (0)