From b20958149b0136a37c0248f235316fb91d0c587f Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 17 Oct 2024 02:16:23 +0900 Subject: [PATCH] activerecord: Add AR::Relation#sanitize_sql_like The ActiveRecord::Relation provides methods defined at AR::Base via delegation (see ActiveRecord::Delegation). This allows to use sanitize_sql_like and its family via AR::Relation. It is often used in the scope definition. --- gems/activerecord/6.0/_test/activerecord-generated.rb | 1 + gems/activerecord/6.0/activerecord.rbs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/gems/activerecord/6.0/_test/activerecord-generated.rb b/gems/activerecord/6.0/_test/activerecord-generated.rb index fe03a1d6..48bb5dc9 100644 --- a/gems/activerecord/6.0/_test/activerecord-generated.rb +++ b/gems/activerecord/6.0/_test/activerecord-generated.rb @@ -8,6 +8,7 @@ class User < ActiveRecord::Base validates :name, presence: true, if: -> { something } validates :age, presence: true, if: ->(user) { user.something } + scope :name_like, ->(name) { where(arel_table[:name].matches("%#{sanitize_sql_like(name)}%")) } scope :matured, -> { where(arel_table[:age].gteq(18)) } before_save -> (obj) { obj.something; self.something } diff --git a/gems/activerecord/6.0/activerecord.rbs b/gems/activerecord/6.0/activerecord.rbs index 6859c6b9..0dddb1cb 100644 --- a/gems/activerecord/6.0/activerecord.rbs +++ b/gems/activerecord/6.0/activerecord.rbs @@ -326,6 +326,11 @@ module ActiveRecord # user.name # => Oscar def new: (?untyped? attributes) ?{ () -> untyped } -> untyped + # ActiveRecord::Relation delegates method calls to a ActiveRecord::Base class. + # To represent the behavior in the RBS system, we define and include the class methods of ActiveRecord::Base here. + + include Sanitization::ClassMethods + def arel_table: () -> Arel::Table end end