Permalink
Browse files

Maintain the original method visibility on prepend

This solves issue #102 where private and protected methods became public
on the prepended modules used for autoescaping.

I'm not that much happy with the code, but it works and isn't that ugly.
  • Loading branch information...
mereghost committed Jun 20, 2016
1 parent af7ff33 commit 1d37fa011ca1bfe2d41f6c490a58d17d776b0a29
Showing with 21 additions and 2 deletions.
  1. +4 −1 lib/hanami/view/escape.rb
  2. +6 −1 test/escape_test.rb
  3. +11 −0 test/fixtures.rb
@@ -165,10 +165,13 @@ def self.extended(base)
# @since 0.4.0
# @api private
def method_added(method_name)
visibility = :private if private_method_defined? method_name
visibility ||= :protected if protected_method_defined? method_name

unless autoescape_methods[method_name]
prepend Module.new {
module_eval %{
def #{ method_name }(*args, &blk); ::Hanami::View::Escape.html super; end
#{ visibility } def #{ method_name }(*args, &blk); ::Hanami::View::Escape.html super; end
}
}

@@ -31,7 +31,7 @@
end

it "doesn't interfer with other views" do
Users::Show.autoescape_methods.must_equal({custom: true, username: true, raw_username: true, book: true})
Users::Show.autoescape_methods.must_equal({custom: true, username: true, raw_username: true, book: true, protected_username: true, private_username: true})
Users::Extra.autoescape_methods.must_equal({username: true})
end

@@ -48,4 +48,9 @@

json.must_match %({"username":"L"})
end

it "does not alter the method visibility" do
Users::Show.private_instance_methods.must_include(:private_username)
Users::Show.protected_instance_methods.must_include(:protected_username)
end
end
@@ -499,6 +499,17 @@ def raw_username
def book
_escape(locals[:book])
end

protected

def protected_username
user.username
end

private
def private_username
user.username
end
end

class XmlShow < Show

0 comments on commit 1d37fa0

Please sign in to comment.