1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #39210 from kamipo/fix_kernel_open

Eager generate relation methods if a method is on `Kernel`
This commit is contained in:
Ryuta Kamizono 2020-05-10 06:52:44 +09:00 committed by GitHub
commit ae46546ea3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 0 deletions

View file

@ -198,6 +198,10 @@ module ActiveRecord
end
private
def singleton_method_added(name)
generate_relation_method(name) if Kernel.respond_to?(name)
end
def valid_scope_name?(name)
if respond_to?(name, true) && logger
logger.warn "Creating scope :#{name}. " \

View file

@ -2119,6 +2119,8 @@ class RelationTest < ActiveRecord::TestCase
sub_accounts = SubAccount.all
assert_equal [accounts(:signals37)], sub_accounts.open
assert_equal [accounts(:signals37)], sub_accounts.available
assert_equal [topics(:second)], topics(:first).open_replies
end
def test_where_with_take_memoization

View file

@ -9,6 +9,10 @@ class Reply < Topic
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
scope :ordered, -> { Reply.order(:id) }
def self.open
approved
end
end
class SillyReply < Topic

View file

@ -42,6 +42,7 @@ class Topic < ActiveRecord::Base
has_many :replies, dependent: :destroy, foreign_key: "parent_id", autosave: true
has_many :approved_replies, -> { approved }, class_name: "Reply", foreign_key: "parent_id", counter_cache: "replies_count"
has_many :open_replies, -> { open }, class_name: "Reply", foreign_key: "parent_id"
has_many :unique_replies, dependent: :destroy, foreign_key: "parent_id"
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"