Cadeia de métodos de alias em rubi se chamando
-
25-09-2019 - |
Pergunta
Estou reescrevendo o método de renderização do controlador, no entanto, quero usar o método antigo quando no método render_to_string. Estes são meus códigos atuais:
def render_with_xhr(options = {}, extra_options = {}, xhr_check = true, &block)
if xhr_check && request.xhr?
template = render_to_string(options)
render_without_xhr(:update) {|page| page.replace_html("#popup .dialog", template)}
else
render_without_xhr(options, extra_options, &block)
end
end
alias_method_chain :render, :xhr
O que acontece é que, como render_to_string faz uso de renderização (presumivelmente), acabo em um loop infinito. Como posso fazê -lo voltar ao método antigo apenas para essa linha meu novo método de renderização?
Eu ajustei os códigos da resposta aceita, o código final está abaixo:
def render_to_string(options = {}, &block)
render(options, {}, false, &block)
ensure
response.content_type = nil
erase_render_results
reset_variables_added_to_assigns
end
def render_with_xhr(options = nil, extra_options = {}, xhr_check = true, &block)
if xhr_check && request.xhr?
template = render_to_string(options)
render_without_xhr :update do |page|
page.replace_html("#popup .dialog", template)
end
else
render_without_xhr(options, extra_options, &block)
end
end
alias_method_chain :render, :xhr
Solução
Você pode na linha 2 passar algum valor exclusivo para o hash de opções, e depois detectá -lo em seu código e remover
def render_with_xhr(options = {}, extra_options = {}, xhr_check = true, &block)
if xhr_check && request.xhr? && !options.delete(:bacon)
template = render_to_string(options.merge(:bacon => true))
render_without_xhr(:update) {|page| page.replace_html("#popup .dialog", template)}
else
render_without_xhr(options, extra_options, &block)
end
end
alias_method_chain :render, :xhr
Curtiu isso :)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow