Skip to content

[PERSISTENCE] Forward document type to mappings #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def as_json(options={})
# Wraps the [index mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping.html)
#
class Mappings
attr_accessor :options
attr_accessor :options, :type

def initialize(type, options={})
raise ArgumentError, "`type` is missing" if type.nil?
Expand Down
4 changes: 3 additions & 1 deletion elasticsearch-model/test/unit/adapter_mongoid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def ids

context "Importing" do
should "implement the __find_in_batches method" do
DummyClassForMongoid.expects(:all).returns([])
relation = mock()
relation.stubs(:no_timeout).returns([])
DummyClassForMongoid.expects(:all).returns(relation)

DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
DummyClassForMongoid.__find_in_batches do; end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def gateway(&block)
delegate :settings,
:mappings,
:mapping,
:document_type,
:document_type=,
:index_name,
:index_name=,
Expand All @@ -80,6 +79,13 @@ def gateway(&block)
:create_index!,
:refresh_index!,
to: :gateway

# forward document type to mappings when set
def document_type(type = nil)
return gateway.document_type unless type
gateway.document_type type
mapping.type = type
end
end

# Configure the repository based on the model (set up index_name, etc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ::Person
include Elasticsearch::Persistence::Model::Rails

settings index: { number_of_shards: 1 }
document_type 'human_being'

attribute :name, String,
mapping: { fields: {
Expand Down Expand Up @@ -60,7 +61,7 @@ class ::Person
assert_equal 'John Smith', document.name
assert_equal 'John Smith', Person.find(person.id).name

assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id
assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'human_being', id: person.id
end

should "not save an invalid object" do
Expand Down
48 changes: 36 additions & 12 deletions elasticsearch-persistence/test/unit/model_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,53 @@ class DummyBaseModel
end

should "set the ID from attributes during initialization" do
m = DummyBaseModel.new id: 1
assert_equal 1, m.id
model = DummyBaseModel.new id: 1
assert_equal 1, model.id

m = DummyBaseModel.new 'id' => 2
assert_equal 2, m.id
model = DummyBaseModel.new 'id' => 2
assert_equal 2, model.id
end

should "set the ID using setter method" do
m = DummyBaseModel.new id: 1
assert_equal 1, m.id
model = DummyBaseModel.new id: 1
assert_equal 1, model.id

m.id = 2
assert_equal 2, m.id
model.id = 2
assert_equal 2, model.id
end

should "have ID in attributes" do
m = DummyBaseModel.new id: 1, name: 'Test'
assert_equal 1, m.attributes[:id]
model = DummyBaseModel.new id: 1, name: 'Test'
assert_equal 1, model.attributes[:id]
end

should "have the customized inspect method" do
m = DummyBaseModel.new name: 'Test'
assert_match /name\: "Test"/, m.inspect
model = DummyBaseModel.new name: 'Test'
assert_match /name\: "Test"/, model.inspect
end

context "#document_type" do
setup do
@model = DummyBaseModel
@gateway = mock()
@mapping = mock()
@model.stubs(:gateway).returns(@gateway)
@gateway.stubs(:mapping).returns(@mapping)
@document_type = 'dummybase'
end

should "forward argument to mapping" do
@gateway.expects(:document_type).with(@document_type).once
@mapping.expects(:type=).with(@document_type).once
@model.document_type @document_type
end

should "return the value from gateway" do
@gateway.expects(:document_type).once.returns(@document_type)
@mapping.expects(:type=).never
returned_type = @model.document_type
assert_equal @document_type, returned_type
end
end
end
end