Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Kernel#warn for deprecation messages #526

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/pg_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module PgSearch
autoload :Document, "pg_search/document"

def self.included(base)
ActiveSupport::Deprecation.warn <<~MESSAGE
warn(<<~MESSAGE, category: :deprecated, uplevel: 1)
Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.

Please replace `include PgSearch` with `include PgSearch::Model`.
Expand All @@ -34,8 +34,8 @@ def self.included(base)
self.unaccent_function = "unaccent"

class << self
def multisearch(*args)
PgSearch::Document.search(*args)
def multisearch(...)
PgSearch::Document.search(...)
end

def disable_multisearch
Expand Down
6 changes: 4 additions & 2 deletions lib/pg_search/features/tsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ def deprecated_headline_options
unless value.nil?
key = deprecated_key.camelize

ActiveSupport::Deprecation.warn(
warn(
"pg_search 3.0 will no longer accept :#{deprecated_key} as an argument to :ts_headline, " \
"use :#{key} instead."
"use :#{key} instead.",
category: :deprecated,
uplevel: 1
)

hash[key] = ts_headline_option_value(value)
Expand Down
6 changes: 4 additions & 2 deletions lib/pg_search/multisearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ module Multisearch
class << self
def rebuild(model, deprecated_clean_up = nil, clean_up: true, transactional: true)
unless deprecated_clean_up.nil?
ActiveSupport::Deprecation.warn(
warn(
"pg_search 3.0 will no longer accept a boolean second argument to PgSearchMultisearch.rebuild, " \
"use keyword argument `clean_up:` instead."
"use keyword argument `clean_up:` instead.",
category: :deprecated,
uplevel: 1
)
clean_up = deprecated_clean_up
end
Expand Down
15 changes: 7 additions & 8 deletions spec/integration/deprecation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require "spec_helper"
require "active_support/core_ext/kernel/reporting"

describe "Including the deprecated PgSearch module" do
with_model :SomeModel do
model do
ActiveSupport::Deprecation.silence do
silence_warnings do
include PgSearch
end
end
Expand All @@ -18,16 +19,14 @@
end

it "prints a deprecation message" do
allow(ActiveSupport::Deprecation).to receive(:warn)
allow(PgSearch).to receive(:warn)

AnotherModel.include(PgSearch)

expect(ActiveSupport::Deprecation).to have_received(:warn).with(
<<~MESSAGE
Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.
expect(PgSearch).to have_received(:warn).with(<<~MESSAGE, category: :deprecated, uplevel: 1)
Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.

Please replace `include PgSearch` with `include PgSearch::Model`.
MESSAGE
)
Please replace `include PgSearch` with `include PgSearch::Model`.
MESSAGE
end
end
4 changes: 2 additions & 2 deletions spec/lib/pg_search/features/tsearch_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require "active_support/deprecation"
require "active_support/core_ext/kernel/reporting"

describe PgSearch::Features::TSearch do
describe "#rank" do
Expand Down Expand Up @@ -251,7 +251,7 @@

feature = described_class.new(query, options, columns, Model, normalizer)

highlight_sql = ActiveSupport::Deprecation.silence { feature.highlight.to_sql }
highlight_sql = silence_warnings { feature.highlight.to_sql }
expected_sql = %{(ts_headline('simple', (coalesce((#{Model.quoted_table_name}."name")::text, '')), (to_tsquery('simple', ''' ' || 'query' || ' ''')), 'StartSel = "<start class=""search"">", StopSel = "<stop>", MaxFragments = 3, MaxWords = 123, MinWords = 456, ShortWord = 4, FragmentDelimiter = "&hellip;", HighlightAll = FALSE'))}

expect(highlight_sql).to eq(expected_sql)
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/pg_search/multisearch_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require "active_support/deprecation"
require "active_support/core_ext/kernel/reporting"

# rubocop:disable RSpec/NestedGroups
describe PgSearch::Multisearch do
Expand Down Expand Up @@ -82,15 +82,15 @@

context "when deprecated_clean_up is true" do
it "deletes the document for the model" do
ActiveSupport::Deprecation.silence { described_class.rebuild(model, true) }
silence_warnings { described_class.rebuild(model, true) }
expect(PgSearch::Document.count).to eq(1)
expect(PgSearch::Document.first.searchable_type).to eq("Bar")
end
end

context "when deprecated_clean_up is false" do
it "does not delete the document for the model" do
ActiveSupport::Deprecation.silence { described_class.rebuild(model, false) }
silence_warnings { described_class.rebuild(model, false) }
expect(PgSearch::Document.count).to eq(2)
end
end
Expand Down