From 006108e87e24fa7d55e706776c015447b60d75c6 Mon Sep 17 00:00:00 2001 From: Emily S Date: Mon, 5 Aug 2019 15:43:49 +0200 Subject: [PATCH] [MODEL] Add warning and documentation about STI support being deprecated (#895) * [MODEL] Add warning and documentation about STI support being deprecated * [MODEL] Minor change to STI deprecation warning * [MODEL] Freeze string constant depreaction warning --- elasticsearch-model/README.md | 18 +++++++++------ .../lib/elasticsearch/model.rb | 23 ++++++++++++------- .../model/naming_inheritance_spec.rb | 6 ++--- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/elasticsearch-model/README.md b/elasticsearch-model/README.md index 21aae0cf0..0a798d8b3 100644 --- a/elasticsearch-model/README.md +++ b/elasticsearch-model/README.md @@ -732,13 +732,8 @@ module and its submodules for technical information. The module provides a common `settings` method to customize various features. -At the moment, the only supported setting is `:inheritance_enabled`, which makes the class receiving the module -respect index names and document types of a super-class, eg. in case you're using "single table inheritance" (STI) -in Rails: - -```ruby -Elasticsearch::Model.settings[:inheritance_enabled] = true -``` +Before version 7.0.0 of the gem, the only supported setting was `:inheritance_enabled`. This setting has been deprecated +and removed. ## Development and Community @@ -756,6 +751,15 @@ curl -# https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticse SERVER=start TEST_CLUSTER_COMMAND=$PWD/tmp/elasticsearch-1.0.0.RC1/bin/elasticsearch bundle exec rake test:all ``` +### Single Table Inheritance support + +Versions < 7.0.0 of this gem supported inheritance-- more specifically, `Single Table Inheritance`. With this feature, +settings on a parent model could be inherited by a child model leading to different model documents being indexed +into the same Elasticsearch index. This feature depended on the ability to set a `type` for a document in Elasticsearch. +The Elasticsearch team has deprecated support for `types`, as is described [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html) +so this gem has also removed support as it encourages an anti-pattern. Please save different model documents in +separate indices or implement an artificial `type` field manually in each document. + ## License This software is licensed under the Apache 2 license, quoted below. diff --git a/elasticsearch-model/lib/elasticsearch/model.rb b/elasticsearch-model/lib/elasticsearch/model.rb index 492bd6ff1..32a79f911 100644 --- a/elasticsearch-model/lib/elasticsearch/model.rb +++ b/elasticsearch-model/lib/elasticsearch/model.rb @@ -121,12 +121,6 @@ class << self Registry.add(base) if base.is_a?(Class) end - # Access the module settings - # - def self.settings - @settings ||= {} - end - module ClassMethods # Get the client common for all models # @@ -183,7 +177,7 @@ def search(query_or_payload, models=[], options={}) # @note Inheritance is disabled by default. # def inheritance_enabled - @inheritance_enabled ||= false + @settings[:inheritance_enabled] ||= false end # Enable inheritance of index_name and document_type @@ -193,8 +187,21 @@ def inheritance_enabled # Elasticsearch::Model.inheritance_enabled = true # def inheritance_enabled=(inheritance_enabled) - @inheritance_enabled = inheritance_enabled + warn STI_DEPRECATION_WARNING + @settings[:inheritance_enabled] = inheritance_enabled + end + + # Access the module settings + # + def settings + @settings ||= {} end + + private + + STI_DEPRECATION_WARNING = "DEPRECATION WARNING: Support for Single Table Inheritance (STI) is deprecated " + + "and will be removed in version 7.0.0.\nPlease save different model documents in separate indices and refer " + + "to the Elasticsearch documentation for more information.".freeze end extend ClassMethods diff --git a/elasticsearch-model/spec/elasticsearch/model/naming_inheritance_spec.rb b/elasticsearch-model/spec/elasticsearch/model/naming_inheritance_spec.rb index ef13d590d..826e44362 100644 --- a/elasticsearch-model/spec/elasticsearch/model/naming_inheritance_spec.rb +++ b/elasticsearch-model/spec/elasticsearch/model/naming_inheritance_spec.rb @@ -61,10 +61,10 @@ module ::MyNamespace end around(:all) do |example| - original_value = Elasticsearch::Model.settings[:inheritance_enabled] - Elasticsearch::Model.settings[:inheritance_enabled] = true + original_value = Elasticsearch::Model.inheritance_enabled + Elasticsearch::Model.inheritance_enabled = true example.run - Elasticsearch::Model.settings[:inheritance_enabled] = original_value + Elasticsearch::Model.inheritance_enabled = original_value end