Skip to content

Commit 98b6ce0

Browse files
maciek-rrpicandocodigo
authored andcommittedMay 14, 2024·
Address Rails 7.1 deprecation warning in elasticsearch-rails
This PR switches the elasticsearch-rails gem to use a new signature of the ActiveSupport::LogSubscriber#color method when called in an app using Rails 7.1+. With older Rails, the gem uses the working, old signature.
1 parent 5411782 commit 98b6ce0

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
 

‎elasticsearch-rails/lib/elasticsearch/rails/instrumentation/log_subscriber.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,19 @@ def search(event)
4646
payload = event.payload
4747
name = "#{payload[:klass]} #{payload[:name]} (#{event.duration.round(1)}ms)"
4848
search = payload[:search].inspect.gsub(/:(\w+)=>/, '\1: ')
49+
debug %Q| #{color(name, GREEN, color_option(true))} #{colorize_logging ? "\e[2m#{search}\e[0m" : search}|
50+
end
51+
52+
private
53+
54+
def color_option(bold_value)
55+
new_color_syntax? ? { bold: bold_value } : bold_value
56+
end
57+
58+
def new_color_syntax?
59+
return @new_color_syntax if defined?(@new_color_syntax)
4960

50-
debug %Q| #{color(name, GREEN, true)} #{colorize_logging ? "\e[2m#{search}\e[0m" : search}|
61+
@new_color_syntax = ::Rails.respond_to?(:gem_version) && ::Rails.gem_version >= '7.1'
5162
end
5263
end
5364

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require 'spec_helper'
19+
require 'elasticsearch/rails/instrumentation/log_subscriber'
20+
21+
describe Elasticsearch::Rails::Instrumentation::LogSubscriber do
22+
subject(:instance) { described_class.new }
23+
24+
let(:logger) { instance_double(Logger) }
25+
26+
before do
27+
allow(instance).to receive(:logger) { logger }
28+
end
29+
30+
describe "#search" do
31+
subject { instance.search(event) }
32+
33+
let(:event) { double("search.elasticsearch", duration: 1.2345, payload: { name: "execute", search: { query: { match_all: {}}}}) }
34+
35+
it "logs the event" do
36+
expect(instance).to receive(:color).with(" execute (1.2ms)", described_class::GREEN, { bold: true }).and_call_original
37+
expect(logger).to receive(:debug?) { true }
38+
expect(logger).to receive(:debug).with(" \e[1m\e[32m execute (1.2ms)\e[0m \e[2m{query: {match_all: {}}}\e[0m")
39+
subject
40+
end
41+
42+
context "when Rails version is older" do
43+
let(:rails_version) { "7.0.0" }
44+
45+
before do
46+
allow(::Rails).to receive(:gem_version) { Gem::Version.new(rails_version) }
47+
end
48+
49+
it "logs the event" do
50+
expect(instance).to receive(:color).with(" execute (1.2ms)", described_class::GREEN, true).and_call_original
51+
expect(logger).to receive(:debug?) { true }
52+
expect(logger).to receive(:debug).with(" \e[1m\e[32m execute (1.2ms)\e[0m \e[2m{query: {match_all: {}}}\e[0m")
53+
subject
54+
end
55+
end
56+
end
57+
end

‎elasticsearch-rails/spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require 'elasticsearch/model'
2121
require 'elasticsearch/rails'
2222
require 'rails/railtie'
23+
require 'rails/version'
2324
require 'elasticsearch/rails/instrumentation'
2425

2526

0 commit comments

Comments
 (0)