2
그래서 내가하려고하는 것은 format_text
메서드를 클래스 수준 메서드에 위임하므로 동일한 이름을 가진 두 메서드가 없을 것입니다. 이 일을하기에 좋은 패턴이나 방법이 있습니까? 그 이유는 뷰와 발표자 모두에서 format_text
으로 전화 할 수 있기 때문입니다. 뷰 외부에서 ApplicationHelper를 사용하는 것이 좋지 않다는 것을 알고 있습니다.ApplicationHelper에서 같은 이름의 클래스 메서드에 메서드 위임
application_helper.rb
module ApplicationHelper
# would like to do something like this:
# delegate :format_text, to: self.format_text
# all this method does is call self.format_text
def format_text(text)
# calls the class level method
format_text(text)
end
# need the self. in front to use outside of view
def self.format_text(text)
# do something to the text and return a string
end
end
뷰는 같은 도우미 사용
some_view.html.haml을
%label= format_text('something needs formatting')
그러나 형식이 아래로 할 필요가 경우에 따라
발표자 수준. 이 경우format_text
메서드를 사용하려면
ApplicationHelper.format_text
과 같이 호출해야합니다.
some_presenter.rb
def header_of_some_data
"#{@name} blah #{ApplicationHelper.format_text('some text')}"
end