未验证 提交 1b3a0a03 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #39280 from kamipo/bypass_argument_forwording_issue

Allow define scope for Ruby reserved keywords
# frozen_string_literal: true
require "mutex_m"
require "active_support/core_ext/module/delegation"
module ActiveRecord
module Delegation # :nodoc:
......@@ -59,7 +60,7 @@ def generate_method(method)
synchronize do
return if method_defined?(method)
if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method)
if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method) && !DELEGATION_RESERVED_METHOD_NAMES.include?(method.to_s)
definition = RUBY_VERSION >= "2.7" ? "..." : "*args, &block"
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(#{definition})
......
......@@ -64,6 +64,11 @@ def test_method_missing_priority_when_delegating
assert_equal klazz.to.since.to_a, klazz.since.to.to_a
end
def test_define_scope_for_reserved_words
assert Topic.true.all?(&:approved?), "all objects should be approved"
assert Topic.false.none?(&:approved?), "all objects should not be approved"
end
def test_scope_should_respond_to_own_methods_and_methods_of_the_proxy
assert_respond_to Topic.approved, :limit
assert_respond_to Topic.approved, :count
......
......@@ -10,6 +10,9 @@ class Topic < ActiveRecord::Base
scope :approved, -> { where(approved: true) }
scope :rejected, -> { where(approved: false) }
scope :true, -> { where(approved: true) }
scope :false, -> { where(approved: false) }
scope :scope_with_lambda, lambda { all }
scope :by_lifo, -> { where(author_name: "lifo") }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册