Skip to content

Commit

Permalink
[MODEL] Added support for inherited index names and doc types
Browse files Browse the repository at this point in the history
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 #332

Related: #28, #92, #170, #344
  • Loading branch information
rymohr authored and karmi committed Jun 26, 2016
1 parent ceef80e commit b8455db
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
19 changes: 18 additions & 1 deletion elasticsearch-model/lib/elasticsearch/model.rb
Expand Up @@ -131,7 +131,6 @@ class << self
end

module ClassMethods

# Get the client common for all models
#
# @example Get the client
Expand Down Expand Up @@ -181,6 +180,24 @@ def search(query_or_payload, models=[], options={})
request = Searching::SearchRequest.new(models, query_or_payload, options)
Response::Response.new(models, request)
end

# Check if inheritance is enabled
#
# @note Inheritance is disabled by default.
#
def inheritance_enabled
@inheritance_enabled ||= false
end

# Enable inheritance of index_name and document_type
#
# @example Enable inheritance
#
# Elasticsearch::Model.inheritance_enabled = true
#
def inheritance_enabled=(inheritance_enabled)
@inheritance_enabled = inheritance_enabled
end
end
extend ClassMethods

Expand Down
28 changes: 26 additions & 2 deletions elasticsearch-model/lib/elasticsearch/model/naming.rb
Expand Up @@ -34,7 +34,7 @@ def index_name name=nil, &block
if @index_name.respond_to?(:call)
@index_name.call
else
@index_name || self.model_name.collection.gsub(/\//, '-')
@index_name || implicit(:index_name)
end
end

Expand All @@ -58,7 +58,7 @@ def index_name=(name)
# Article.document_type "my-article"
#
def document_type name=nil
@document_type = name || @document_type || self.model_name.element
@document_type = name || @document_type || implicit(:document_type)
end


Expand All @@ -69,6 +69,30 @@ def document_type name=nil
def document_type=(name)
@document_type = name
end

private

def implicit(prop)
value = nil

if Elasticsearch::Model.inheritance_enabled
self.ancestors.each do |klass|
next if klass == self
break if value = klass.respond_to?(prop) && klass.send(prop)
end
end

value || self.send("default_#{prop}")
end

def default_index_name
self.model_name.collection.gsub(/\//, '-')
end

def default_document_type
self.model_name.element
end

end

module InstanceMethods
Expand Down
78 changes: 78 additions & 0 deletions elasticsearch-model/test/unit/naming_inheritance_test.rb
@@ -0,0 +1,78 @@
require "test_helper"

class Elasticsearch::Model::NamingInheritanceTest < Test::Unit::TestCase
def setup
Elasticsearch::Model.inheritance_enabled = true
end

def teardown
Elasticsearch::Model.inheritance_enabled = false
end

context "Naming module with inheritance" do
class ::TestBase
extend ActiveModel::Naming

extend Elasticsearch::Model::Naming::ClassMethods
include Elasticsearch::Model::Naming::InstanceMethods
end

class ::Animal < ::TestBase
extend ActiveModel::Naming

extend Elasticsearch::Model::Naming::ClassMethods
include Elasticsearch::Model::Naming::InstanceMethods

index_name "mammals"
document_type "mammal"
end

class ::Dog < ::Animal
end

module ::MyNamespace
class Dog < ::Animal
end
end

should "return the default index_name" do
assert_equal "test_bases", TestBase.index_name
assert_equal "test_bases", TestBase.new.index_name
end

should "return the explicit index_name" do
assert_equal "mammals", Animal.index_name
assert_equal "mammals", Animal.new.index_name
end

should "return the ancestor index_name" do
assert_equal "mammals", Dog.index_name
assert_equal "mammals", Dog.new.index_name
end

should "return the ancestor index_name for namespaced model" do
assert_equal "mammals", ::MyNamespace::Dog.index_name
assert_equal "mammals", ::MyNamespace::Dog.new.index_name
end

should "return the default document_type" do
assert_equal "test_base", TestBase.document_type
assert_equal "test_base", TestBase.new.document_type
end

should "return the explicit document_type" do
assert_equal "mammal", Animal.document_type
assert_equal "mammal", Animal.new.document_type
end

should "return the ancestor document_type" do
assert_equal "mammal", Dog.document_type
assert_equal "mammal", Dog.new.document_type
end

should "return the ancestor document_type for namespaced model" do
assert_equal "mammal", ::MyNamespace::Dog.document_type
assert_equal "mammal", ::MyNamespace::Dog.new.document_type
end
end
end

0 comments on commit b8455db

Please sign in to comment.