Skip to content

Conversation

justinko
Copy link
Contributor

@justinko justinko commented Jun 8, 2024

Fixes #52000

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
# assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
public def fixture(fixture_set_name, *fixture_names); end
silence_warnings { alias_method :fixture, :_active_record_fixture }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without silence_warnings, this is outputted:

/Users/jko/Developer/rails/rails/activerecord/lib/active_record/test_fixtures.rb:286: warning: method redefined; discarding old fixture
/Users/jko/Developer/rails/rails/activerecord/lib/active_record/test_fixtures.rb:285: warning: previous definition of fixture was here

Is it worth adding a comment specifying what warning we are silencing?

# Generic fixture accessor for fixture names that may conflict.
#
# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
# assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
Copy link
Contributor Author

@justinko justinko Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm an RDoc noob, definitely let me know if I'm missing something. Perhaps point the source to _active_record_fixture? Not sure how to do that if so.

@justinko
Copy link
Contributor Author

justinko commented Jun 8, 2024

https://3f2f1ead.rails-docs-preview.pages.dev/api/classes/ActiveRecord/TestFixtures

image

# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
# assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
public def fixture(fixture_set_name, *fixture_names); end
silence_warnings { alias_method :fixture, :_active_record_fixture }
Copy link
Contributor Author

@justinko justinko Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, even though the new documentation says it's a public method, the alias_method to a private method flips it to be private. Not sure if that's disingenuous or not.

@skipkayhil
Copy link
Member

skipkayhil commented Jun 8, 2024

Since we want to document fixture but not _active_record_fixture, can we just flip which one is the alias?

diff --git a/activerecord/lib/active_record/test_fixtures.rb b/activerecord/lib/active_record/test_fixtures.rb
index 00b3f5a846..6dc68087f1 100644
--- a/activerecord/lib/active_record/test_fixtures.rb
+++ b/activerecord/lib/active_record/test_fixtures.rb
@@ -269,14 +269,14 @@ def respond_to_missing?(method, include_private = false)
         end
       end

-      def _active_record_fixture(fixture_set_name, *fixture_names)
+      def fixture(fixture_set_name, *fixture_names) # :doc:
         if fs_name = fixture_sets[fixture_set_name.name]
           access_fixture(fs_name, *fixture_names)
         else
           raise StandardError, "No fixture set named '#{fixture_set_name.inspect}'"
         end
       end
-      alias_method :fixture, :_active_record_fixture
+      alias_method :_active_record_fixture, :fixture

       def access_fixture(fs_name, *fixture_names)

This makes it show up in the doc as a private method for me

image

(and I guess add # :nodoc: on the alias line to hide the Also aliased as bit)

@justinko
Copy link
Contributor Author

justinko commented Jun 9, 2024

Since we want to document fixture but not _active_record_fixture, can we just flip which one is the alias?

The risk is if the user overrides fixture, it would break all the fixture name methods because it would hold the implementation. We get around this by relying on _active_record_fixture to have the implementation and using it internally.

This makes it show up in the doc as a private method for me

I believe the entire point of this pull is to make fixture show up as a public method in the docs to encourage use.

@skipkayhil
Copy link
Member

The risk is if the user overrides fixture, it would break all the fixture name methods because it would hold the implementation. We get around this by relying on _active_record_fixture to have the implementation and using it internally.

$ irb
irb(main):001* class A
irb(main):002*   def a = puts "A"
irb(main):003*   alias_method :b, :a
irb(main):004> end
=> :b
irb(main):005> A.new.a
A
=> nil
irb(main):006> A.new.b
A
=> nil
irb(main):007* class B < A
irb(main):008*   def a = puts "B"
irb(main):009> end
=> :a
irb(main):010> B.new.a
B
=> nil
irb(main):011> B.new.b
A
=> nil
irb(main):012>

I don't think method redefinition affects aliases, so it shouldn't be a problem.

I believe the entire point of this pull is to make fixture show up as a public method in the docs to encourage use.

My understanding was the purpose was to document the method (whether public or private). If we want it to show up as public then personally it makes more sense to me to just make the method public than "lie" about the privacy.

@justinko
Copy link
Contributor Author

justinko commented Jun 9, 2024

If we want it to show up as public then personally it makes more sense to me to just make the method public than "lie" about the privacy.

Agreed. I made this pull more for an intellectual exercise than caring about its intent.

I was confused by byroot's comment here: #51213 (comment)

@justinko
Copy link
Contributor Author

justinko commented Jun 9, 2024

I don't think method redefinition affects aliases, so it shouldn't be a problem.

You are correct.

@justinko
Copy link
Contributor Author

justinko commented Jun 9, 2024

Why can't we just do this? 110c65d

@byroot
Copy link
Member

byroot commented Jun 9, 2024

I was confused by byroot's comment here

The thing is, assertion methods and test helpers are supposed to be private, but that makes them hard to document. Looking at other assertion methods etc in Rails, they are all public, likely for documentation purposes. So yeah, we should just go with that.

Fixes rails#52000

Co-Authored-By: Hartley McGuire <skipkayhil@gmail.com>
@justinko justinko requested a review from byroot June 9, 2024 21:59
@rafaelfranca rafaelfranca merged commit 445529c into rails:main Jun 12, 2024
rafaelfranca added a commit that referenced this pull request Jun 12, 2024
Document TestFixtures#fixture
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing documentation for "fixture" method
4 participants