Skip to content

Commit f40bb73

Browse files
RUBY-5651 Deprecate for_js API (#5721)
1 parent c9e55f7 commit f40bb73

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

docs/release-notes/mongoid-9.0.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

2020

21+
``for_js`` method is deprecated
22+
-------------------------------
23+
24+
The ``for_js`` method is deprecated and will be removed in Mongoid 10.0.
25+
26+
2127
Deprecated options removed
2228
--------------------------
2329

lib/mongoid/contextual/mongo.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def count(options = {}, &block)
7878
if valid_for_count_documents?
7979
view.count_documents(options)
8080
else
81+
# TODO: Remove this when we remove the deprecated for_js API.
82+
# https://jira.mongodb.org/browse/MONGOID-5681
8183
view.count(options)
8284
end
8385
end
@@ -1049,6 +1051,9 @@ def process_raw_docs(raw_docs, limit)
10491051
#
10501052
# @return [ true | false ] whether or not the current context
10511053
# excludes a `$where` operator.
1054+
#
1055+
# TODO: Remove this method when we remove the deprecated for_js API.
1056+
# https://jira.mongodb.org/browse/MONGOID-5681
10521057
def valid_for_count_documents?(hash = view.filter)
10531058
# Note that `view.filter` is a BSON::Document, and all keys in a
10541059
# BSON::Document are strings; we don't need to worry about symbol

lib/mongoid/criteria.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Criteria
4141
include Clients::Sessions
4242
include Options
4343

44+
Mongoid.deprecate(self, :for_js)
45+
4446
# Static array used to check with method missing - we only need to ever
4547
# instantiate once.
4648
CHECK = []
@@ -439,6 +441,8 @@ def without_options
439441
# @param [ Hash ] scope The scope for the code.
440442
#
441443
# @return [ Criteria ] The criteria.
444+
#
445+
# @deprecated
442446
def for_js(javascript, scope = {})
443447
code = if scope.empty?
444448
# CodeWithScope is not supported for $where as of MongoDB 4.4

lib/mongoid/deprecation.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ module Mongoid
66
# Utility class for logging deprecation warnings.
77
class Deprecation < ::ActiveSupport::Deprecation
88

9-
@gem_name = 'Mongoid'
9+
def initialize
10+
# Per change policy, deprecations will be removed in the next major version.
11+
deprecation_horizon = "#{Mongoid::VERSION.split('.').first.to_i + 1}.0".freeze
12+
gem_name = 'Mongoid'
13+
super(deprecation_horizon, gem_name)
14+
end
1015

11-
# Per change policy, deprecations will be removed in the next major version.
12-
@deprecation_horizon = "#{Mongoid::VERSION.split('.').first.to_i + 1}.0".freeze
1316

1417
# Overrides default ActiveSupport::Deprecation behavior
1518
# to use Mongoid's logger.

spec/mongoid/criteria_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,12 @@ def self.ages; self; end
29772977
Band.create!(name: "Depeche Mode")
29782978
end
29792979

2980+
it 'is deprecated' do
2981+
expect(Mongoid.logger).to receive(:warn).with(/for_js is deprecated/).and_call_original
2982+
2983+
Band.for_js("this.name == 'Depeche Mode'")
2984+
end
2985+
29802986
context "when the code has no scope" do
29812987

29822988
let(:criteria) do

0 commit comments

Comments
 (0)