Class: Rails5XHRUpdate::XHRToRails5

Inherits:
Parser::TreeRewriter
  • Object
show all
Defined in:
lib/rails5_xhr_update/xhr_to_rails5.rb

Overview

Convert uses of the xhr method to use the rails 5 syntax.

For example prior to rails 5 one might write:

xhr :get, images_path, limit: 10, sort: 'new'

This class will convert that into:

get images_path, params: { limit: 10, sort: 'new' }, xhr: true

Conversion of xhr methods using headers is also supported:

xhr :get, root_path {}, { Accept: => 'application/json' }

This class will convert the above into:

get root_path, headers: { Accept: => 'application/json' }, xhr: true

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



27
28
29
30
31
32
# File 'lib/rails5_xhr_update/xhr_to_rails5.rb', line 27

def on_send(node)
  return if node.children[1] != :xhr
  arguments = extract_and_validate_arguments(node)
  children = initial_children(node) + add_xhr_node(*arguments)
  replace(node.loc.expression, ast_to_string(node.updated(nil, children)))
end