-
Notifications
You must be signed in to change notification settings - Fork 30
Add support for class_methods do
blocks within concerns
#530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e77740c
03bbd63
ba74746
fcc6515
294e05d
de814a0
9554f2d
5906cd9
d7f75ea
78e0102
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ module Rails | |
class IndexingEnhancementTest < ActiveSupport::TestCase | ||
class << self | ||
# For these tests, it's convenient to have the index fully populated with Rails information, but we don't have | ||
# to reindex on every single example or that will be too slow | ||
# to re-index on every single example or that will be too slow | ||
def populated_index | ||
@index ||= begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vinistock avoiding the memoization solves in test instability here. It's obviously slower though. A few possible alternatives:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we just add note to the test and use different names in each example? Trying to clean up all of the index state might be difficult and it will require exposing some internal APIs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually scratch that. We already have an API to delete entries, let's use that on teardown. |
||
index = RubyIndexer::Index.new | ||
|
@@ -20,22 +20,77 @@ def populated_index | |
|
||
def setup | ||
@index = self.class.populated_index | ||
@indexable_path = RubyIndexer::IndexablePath.new(nil, "/fake.rb") | ||
end | ||
|
||
def teardown | ||
# Prevent state leaking between tests | ||
@index.delete(@indexable_path) | ||
@index.instance_variable_set(:@ancestors, {}) | ||
end | ||
|
||
test "ClassMethods module inside concerns are automatically extended" do | ||
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY) | ||
class Post < ActiveRecord::Base | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this so that all the code is visible in the tests, rather than having to know what Rails does internally. |
||
@index.index_single(@indexable_path, <<~RUBY) | ||
module Verifiable | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def all_verified; end | ||
end | ||
end | ||
|
||
class Post | ||
include Verifiable | ||
end | ||
RUBY | ||
|
||
ancestors = @index.linearized_ancestors_of("Post::<Class:Post>") | ||
assert_includes(ancestors, "ActiveRecord::Associations::ClassMethods") | ||
assert_includes(ancestors, "ActiveRecord::Store::ClassMethods") | ||
assert_includes(ancestors, "ActiveRecord::AttributeMethods::ClassMethods") | ||
|
||
assert_includes(ancestors, "Verifiable::ClassMethods") | ||
refute_nil(@index.resolve_method("all_verified", "Post::<Class:Post>")) | ||
end | ||
|
||
test "class_methods blocks inside concerns are automatically extended via a ClassMethods module" do | ||
@index.index_single(@indexable_path, <<~RUBY) | ||
module Verifiable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def all_verified; end | ||
end | ||
end | ||
|
||
class Post | ||
include Verifiable | ||
end | ||
RUBY | ||
|
||
ancestors = @index.linearized_ancestors_of("Post::<Class:Post>") | ||
|
||
assert_includes(ancestors, "Verifiable::ClassMethods") | ||
refute_nil(@index.resolve_method("all_verified", "Post::<Class:Post>")) | ||
end | ||
|
||
test "ignores `class_methods` calls without a block" do | ||
@index.index_single(@indexable_path, <<~RUBY) | ||
module Verifiable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods | ||
end | ||
|
||
class Post | ||
include Verifiable | ||
end | ||
RUBY | ||
|
||
ancestors = @index.linearized_ancestors_of("Post::<Class:Post>") | ||
|
||
refute_includes(ancestors, "Verifiable::ClassMethods") | ||
end | ||
|
||
test "associations" do | ||
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY) | ||
@index.index_single(@indexable_path, <<~RUBY) | ||
class Post < ActiveRecord::Base | ||
has_one :content | ||
belongs_to :author | ||
|
Uh oh!
There was an error while loading. Please reload this page.