Skip to content

Commit

Permalink
[MODEL] Add warning and documentation about STI support being depreca…
Browse files Browse the repository at this point in the history
…ted (#895)

* [MODEL] Add warning and documentation about STI support being deprecated

* [MODEL] Minor change to STI deprecation warning

* [MODEL] Freeze string constant depreaction warning
  • Loading branch information
estolfo authored Aug 5, 2019
1 parent 62191f8 commit 006108e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
18 changes: 11 additions & 7 deletions elasticsearch-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
23 changes: 15 additions & 8 deletions elasticsearch-model/lib/elasticsearch/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 006108e

Please # to comment.