Skip to content

Commit 2b338c1

Browse files
miguelffkarmi
authored andcommitted
[MODEL] [STORE] Fixed the problem where document_type configuration was not propagated to mapping
This PR tries to fix the bug described in elastic#270 The problem was the following: * Once a model extends Persistence::Model it sets up two attributes. As a consequence, the attribute macro instantiates a Elasticsearch::Model::Indexing::Mappings in the model. This instance contains an attribute @type that defaults to the model name in lowercase. * Any other definition of document_type will override the document_type in the gateway delegate, but the mappings instance originally created in the previous step remains untouched, and hence in an inconsistent state. What I did here is: Instead of simply forward document_type to the gateway, also change the mappings' document_type accordingly, leaving it in a correct state. An integration test was modified, in order to have a regression of the fix. Unit tests were added. Fixes elastic#270 Closes elastic#366
1 parent f5fc4ea commit 2b338c1

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def as_json(options={})
3434
# Wraps the [index mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping.html)
3535
#
3636
class Mappings
37-
attr_accessor :options
37+
attr_accessor :options, :type
3838

3939
def initialize(type, options={})
4040
raise ArgumentError, "`type` is missing" if type.nil?

elasticsearch-persistence/lib/elasticsearch/persistence/model.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def gateway(&block)
7171
delegate :settings,
7272
:mappings,
7373
:mapping,
74-
:document_type,
7574
:document_type=,
7675
:index_name,
7776
:index_name=,
@@ -80,6 +79,13 @@ def gateway(&block)
8079
:create_index!,
8180
:refresh_index!,
8281
to: :gateway
82+
83+
# forward document type to mappings when set
84+
def document_type(type = nil)
85+
return gateway.document_type unless type
86+
gateway.document_type type
87+
mapping.type = type
88+
end
8389
end
8490

8591
# Configure the repository based on the model (set up index_name, etc)

elasticsearch-persistence/test/integration/model/model_basic_test.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ::Person
1212
include Elasticsearch::Persistence::Model::Rails
1313

1414
settings index: { number_of_shards: 1 }
15+
document_type 'human_being'
1516

1617
attribute :name, String,
1718
mapping: { fields: {
@@ -60,7 +61,7 @@ class ::Person
6061
assert_equal 'John Smith', document.name
6162
assert_equal 'John Smith', Person.find(person.id).name
6263

63-
assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id
64+
assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'human_being', id: person.id
6465
end
6566

6667
should "not save an invalid object" do

elasticsearch-persistence/test/unit/model_base_test.rb

+36-12
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,53 @@ class DummyBaseModel
2020
end
2121

2222
should "set the ID from attributes during initialization" do
23-
m = DummyBaseModel.new id: 1
24-
assert_equal 1, m.id
23+
model = DummyBaseModel.new id: 1
24+
assert_equal 1, model.id
2525

26-
m = DummyBaseModel.new 'id' => 2
27-
assert_equal 2, m.id
26+
model = DummyBaseModel.new 'id' => 2
27+
assert_equal 2, model.id
2828
end
2929

3030
should "set the ID using setter method" do
31-
m = DummyBaseModel.new id: 1
32-
assert_equal 1, m.id
31+
model = DummyBaseModel.new id: 1
32+
assert_equal 1, model.id
3333

34-
m.id = 2
35-
assert_equal 2, m.id
34+
model.id = 2
35+
assert_equal 2, model.id
3636
end
3737

3838
should "have ID in attributes" do
39-
m = DummyBaseModel.new id: 1, name: 'Test'
40-
assert_equal 1, m.attributes[:id]
39+
model = DummyBaseModel.new id: 1, name: 'Test'
40+
assert_equal 1, model.attributes[:id]
4141
end
4242

4343
should "have the customized inspect method" do
44-
m = DummyBaseModel.new name: 'Test'
45-
assert_match /name\: "Test"/, m.inspect
44+
model = DummyBaseModel.new name: 'Test'
45+
assert_match /name\: "Test"/, model.inspect
46+
end
47+
48+
context "with custom document_type" do
49+
setup do
50+
@model = DummyBaseModel
51+
@gateway = mock()
52+
@mapping = mock()
53+
@model.stubs(:gateway).returns(@gateway)
54+
@gateway.stubs(:mapping).returns(@mapping)
55+
@document_type = 'dummybase'
56+
end
57+
58+
should "forward the argument to mapping" do
59+
@gateway.expects(:document_type).with(@document_type).once
60+
@mapping.expects(:type=).with(@document_type).once
61+
@model.document_type @document_type
62+
end
63+
64+
should "return the value from the gateway" do
65+
@gateway.expects(:document_type).once.returns(@document_type)
66+
@mapping.expects(:type=).never
67+
returned_type = @model.document_type
68+
assert_equal @document_type, returned_type
69+
end
4670
end
4771
end
4872
end

0 commit comments

Comments
 (0)