Skip to content

Commit 10028bb

Browse files
authored
[STORE] Warn that the ActiveRecord persistence pattern is deprecated (#809)
1 parent 931830c commit 10028bb

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

elasticsearch-persistence/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ and demonstrates a rich set of features:
445445

446446
### The ActiveRecord Pattern
447447

448+
Please note that this pattern is deprecated and will be removed in version 6.0.
449+
The [Repository Pattern](#the-repository-pattern) is recommended instead.
450+
448451
The `Elasticsearch::Persistence::Model` module provides an implementation of the
449452
active record [pattern](http://www.martinfowler.com/eaaCatalog/activeRecord.html),
450453
with a familiar interface for using Elasticsearch as a persistence layer in

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ def gateway(&block)
6666
@gateway
6767
end
6868

69+
DEPRECATION_WARNING = "This (ActiveRecord) persistence pattern is deprecated. It will be removed in " +
70+
"version 6.0 in favor of the Repository pattern. Please see the ReadMe for " +
71+
"details on the alternative pattern.\n" +
72+
"https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-persistence" +
73+
"#the-repository-pattern".freeze
74+
75+
# Warn that this ActiveRecord persistence pattern is deprecated.
76+
#
77+
def raise_deprecation_warning!
78+
if STDERR.tty?
79+
Kernel.warn("\e[31;1m#{DEPRECATION_WARNING}\e[0m")
80+
else
81+
Kernel.warn(DEPRECATION_WARNING)
82+
end
83+
end
84+
6985
# Delegate methods to repository
7086
#
7187
delegate :settings,
@@ -126,6 +142,8 @@ def deserialize(document)
126142
attribute :updated_at, Time, default: lambda { |o,a| Time.now.utc }
127143

128144
attr_reader :hit
145+
146+
raise_deprecation_warning!
129147
end
130148

131149
end

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class ::Person
3333
Person.create_index! force: true
3434
end
3535

36+
should "warn that the ActiveRecord persistence pattern is deprecated" do
37+
Kernel.expects(:warn).at_least_once
38+
class ShouldWarn; include Elasticsearch::Persistence::Model; end
39+
end
40+
3641
should "save the object with custom ID" do
3742
person = Person.new id: 1, name: 'Number One'
3843
person.save

0 commit comments

Comments
 (0)