Baxter
Baxter

Reputation: 142

Determining source location of label method in Rails when using helper method inside Rails console

I understand that there are two label methods (ActionView::Helpers::FormHelper#label and ActionView::Helpers::FormBuilder#label) available in Rails. Which #label method is being called when the following is being typed in the rails console:

$ rails c
Loading development environment (Rails 5.0.7.2)
2.6.0 :001 > helper.label(:post, :title, "A short title", class: "title_label")
 => "<label class=\"title_label\" for=\"post_title\">A short title</label>"

I've gone into gems/actionview-5.0.7.2/lib/action_view/helpers/form_helper.rb and commented out both label methods like so:

#      def label(object_name, method, content_or_options = nil, options = nil, &block)
#        Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
#      end
.
.
.
#      def label(method, text = nil, options = {}, &block)
#        @template.label(@object_name, method, text, objectify_options(options), &block)
#      end

Yet it's still executing the label method in the Rails console, how can I determine the source location of this method call?

Upvotes: 1

Views: 186

Answers (2)

lmtaq
lmtaq

Reputation: 448

One way to do this natively in Ruby is to use Method#source_location.

> helper.method(:label).source_location
=> ["/Users/myuser/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/actionview-7.0.8.5/lib/action_view/helpers/form_helper.rb", 1143]

Upvotes: 0

CAmador
CAmador

Reputation: 1951

It is the first one. The second needs a FormBuilder object (the f. part) to work.

Did you reload the console after commenting out those methods?

I you are using Pry you can use show-method:

pry(main)> show-method helper.label

From: /home/user/.rvm/gems/ruby-2.5.3@gemset/gems/actionview-5.2.3/lib/action_view/helpers/form_helper.rb @ line 1114:
Owner: ActionView::Helpers::FormHelper
Visibility: public
Number of lines: 3

def label(object_name, method, content_or_options = nil, options = nil, &block)
  Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
end

Upvotes: 1

Related Questions