Skip to content
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

Honor public/private in ActionView::Helpers::Tags::Base#value #14180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionview/lib/action_view/helpers/tags/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def render
private

def value(object)
object.send @method_name if object
object.public_send @method_name if object
end

def value_before_type_cast(object)
Expand Down
6 changes: 6 additions & 0 deletions actionview/test/template/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def test_tags_base_child_without_render_method
assert_raise(NotImplementedError) { FooTag.new.render }
end

def test_tags_base_value_honors_public_private
test_object = Class.new { private def my_method ; end }.new
tag = ActionView::Helpers::Tags::Base.new 'test_object', :my_method, nil
assert_raise(NoMethodError) { tag.send :value, test_object }
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the send here? Couldn't you just do tag.value(test_object) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

value is a private method unfortunately... :-/ So using send to work around it...

edit: thanks for your quick feedback! =D

end

Choose a reason for hiding this comment

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

I'm not sure that using send here would be a good way to test this, can you think of another way to test it other than bypassing encapsulation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I am unsure about that as well. The right way to test this would be exercising a behavior that ends up calling value(object) and then raises the error/exercises a different behavior.

However the downside is, that it will most likely be a much bigger/more complicated test set up. Dunno if it's worth the cost so didn't wanna start with it (as I was unsure if this change is even welcome).

Happy to try out a more behavior driven approach for testing.

Copy link
Member

Choose a reason for hiding this comment

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

How about trying to render a text input?


def test_label
assert_dom_equal('<label for="post_title">Title</label>', label("post", "title"))
assert_dom_equal(
Expand Down