Skip to content

Commit

Permalink
add: ActiveRecord::QueryMethods::WhereChain#missing (#596)
Browse files Browse the repository at this point in the history
* add: ActiveRecord::QueryMethods::WhereChain#missing

Added `ActiveRecord::QueryMethods::WhereChain#missing` in version 6.1 or later.

Fixed to return `::ActiveRecord::QueryMethods::WhereChain` when the `where` method has no argument.

* update: ActiveRecord::QueryMethods::WhereChain has model information

* test: add testing code for `missing`

* test: code to the minimum necessary
  • Loading branch information
rhiroe authored Jun 14, 2024
1 parent 06a8f44 commit 14cafb8
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 40 deletions.
34 changes: 0 additions & 34 deletions gems/activerecord/6.0/activerecord-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18591,40 +18591,6 @@ module ActiveRecord

include ActiveModel::ForbiddenAttributesProtection

# WhereChain objects act as placeholder for queries in which #where does not have any parameter.
# In this case, #where must be chained with #not to return a new relation.
class WhereChain
include ActiveModel::ForbiddenAttributesProtection

def initialize: (untyped scope) -> untyped

# Returns a new relation expressing WHERE + NOT condition according to
# the conditions in the arguments.
#
# #not accepts conditions as a string, array, or hash. See QueryMethods#where for
# more details on each format.
#
# User.where.not("name = 'Jon'")
# # SELECT * FROM users WHERE NOT (name = 'Jon')
#
# User.where.not(["name = ?", "Jon"])
# # SELECT * FROM users WHERE NOT (name = 'Jon')
#
# User.where.not(name: "Jon")
# # SELECT * FROM users WHERE name != 'Jon'
#
# User.where.not(name: nil)
# # SELECT * FROM users WHERE name IS NOT NULL
#
# User.where.not(name: %w(Ko1 Nobu))
# # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
def not: (untyped opts, *untyped rest) -> untyped

private

def not_behaves_as_nor?: (untyped opts) -> (::FalseClass | untyped)
end

FROZEN_EMPTY_ARRAY: untyped

FROZEN_EMPTY_HASH: untyped
Expand Down
26 changes: 22 additions & 4 deletions gems/activerecord/6.0/activerecord.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ module ActiveRecord

def self.default_scope: (?untyped? scope) -> untyped | ...
end

module QueryMethods
class WhereChain[Relation]
include ActiveModel::ForbiddenAttributesProtection

def initialize: (Relation scope) -> void

def not: (untyped opts, *untyped rest) -> Relation

private

def not_behaves_as_nor?: (untyped opts) -> (::FalseClass | untyped)
end
end
end

module ActiveRecord
Expand All @@ -298,7 +312,8 @@ module ActiveRecord
def none: () -> self
def pluck: (Symbol | String column) -> Array[untyped]
| (*Symbol | String columns) -> Array[Array[untyped]]
def where: (*untyped) -> self
def where: () -> ::ActiveRecord::QueryMethods::WhereChain[self]
| (*untyped) -> self
def exists?: (*untyped) -> bool
def order: (*untyped) -> self
def group: (*Symbol | String) -> untyped
Expand Down Expand Up @@ -376,7 +391,8 @@ module ActiveRecord
def none: () -> Relation
def pluck: (Symbol | String column) -> Array[untyped]
| (*Symbol | String columns) -> Array[Array[untyped]]
def where: (*untyped) -> Relation
def where: () -> ::ActiveRecord::QueryMethods::WhereChain[Relation]
| (*untyped) -> Relation
def exists?: (*untyped) -> bool
def any?: () -> bool
| () { (Model) -> boolish } -> bool
Expand Down Expand Up @@ -460,7 +476,8 @@ interface _ActiveRecord_Relation[Model, PrimaryKey]
def none: () -> self
def pluck: (Symbol | String column) -> Array[untyped]
| (*Symbol | String columns) -> Array[Array[untyped]]
def where: (*untyped) -> self
def where: () -> ::ActiveRecord::QueryMethods::WhereChain[self]
| (*untyped) -> self
def exists?: (*untyped) -> bool
def order: (*untyped) -> self
def group: (*Symbol | String) -> untyped
Expand Down Expand Up @@ -537,7 +554,8 @@ interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey]
def none: () -> Relation
def pluck: (Symbol | String column) -> Array[untyped]
| (*Symbol | String columns) -> Array[Array[untyped]]
def where: (*untyped) -> Relation
def where: () -> ::ActiveRecord::QueryMethods::WhereChain[Relation]
| (*untyped) -> Relation
def exists?: (*untyped) -> bool
def any?: () -> bool
| () { (Model) -> boolish } -> bool
Expand Down
6 changes: 6 additions & 0 deletions gems/activerecord/6.1/activerecord-6.1.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ module ActiveRecord
class MismatchedForeignKey < StatementInvalid
def initialize: (?primary_key_column: untyped? primary_key_column, ?primary_key: untyped? primary_key, ?target_table: untyped? target_table, ?foreign_key: untyped? foreign_key, ?table: untyped? table, ?binds: untyped? binds, ?sql: untyped? sql, ?message: untyped? message) -> untyped
end

module QueryMethods
class WhereChain[Relation]
def missing: (*Symbol associations) -> Relation
end
end
end
1 change: 1 addition & 0 deletions gems/activerecord/7.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User < ApplicationRecord
encrypts :phrase, ignore_case: true
end

User.where.missing.to_sql
User.deterministic_encrypted_attributes
User.source_attribute_from_preserved_attribute(:phrase)
user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy')
Expand Down
6 changes: 5 additions & 1 deletion gems/activerecord/7.0/_test/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Test
class ApplicationRecord < ActiveRecord::Base
end

class User < ActiveRecord::Base
class User < ApplicationRecord
extend ::ActiveRecord::Base::ClassMethods[User, ActiveRecord_Relation, Integer]

class ActiveRecord_Relation < ::ActiveRecord::Relation
end
end
end
6 changes: 6 additions & 0 deletions gems/activerecord/7.0/activerecord-7.0.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ module ActiveRecord
extend RuntimeRegistry
end

module QueryMethods
class WhereChain[Relation]
def missing: (*Symbol associations) -> Relation
end
end

module Encryption
module EncryptableRecord
module ClassMethods
Expand Down
1 change: 1 addition & 0 deletions gems/activerecord/7.1/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class User < ApplicationRecord
}
end

User.where.missing.to_sql
User.deterministic_encrypted_attributes
User.source_attribute_from_preserved_attribute(:phrase)
user = User.new(secret: 'dummy', key: 'dummy', token: 'dummy', phrase: 'dummy')
Expand Down
6 changes: 5 additions & 1 deletion gems/activerecord/7.1/_test/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Test
class ApplicationRecord < ActiveRecord::Base
end

class User < ActiveRecord::Base
class User < ApplicationRecord
extend ::ActiveRecord::Base::ClassMethods[User, ActiveRecord_Relation, Integer]

class ActiveRecord_Relation < ::ActiveRecord::Relation
end
end
end
6 changes: 6 additions & 0 deletions gems/activerecord/7.1/activerecord-7.1.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module ActiveRecord
extend RuntimeRegistry
end

module QueryMethods
class WhereChain[Relation]
def missing: (*Symbol associations) -> Relation
end
end

module Encryption
module EncryptableRecord
module ClassMethods
Expand Down

0 comments on commit 14cafb8

Please sign in to comment.