rails5_xhr_update 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f497a964384cd6e1d4ecf8548e04523b0d67d68
4
+ data.tar.gz: a01b99820e5eeb16917828abe3fabaf4790558cd
5
+ SHA512:
6
+ metadata.gz: 391227f5befb412d105e3562746a6d62d42f1221afdc05a8661632d68f002ec1c9d910364b4a2bd920e6dea7558d6a76ea1e9286f61beba078581ff0e2ef7821
7
+ data.tar.gz: 3fd9861315aefd2be0862e104ff6c80164a4170fb6a7630e7a7c6eff6aea408b68c55d541ff8a9fde446836e37653a6ec497382367bd280561891a320eef141a
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2018, AppFolio, Inc.
2
+ Copyright (c) 2018, Bryce Boe
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+ 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # rails5_xhr_update
2
+
3
+ rails5_xhr_update is a program that can be used to help convert from the:
4
+
5
+ ```ruby
6
+ xhr :get, :action
7
+ ```
8
+
9
+ test syntax used in rails prior to Rails 5, to the Rails 5 syntax:
10
+
11
+ ```ruby
12
+ get :action, xhr: true
13
+ ```
14
+
15
+ ## Installation
16
+
17
+ To install rails5_xhr_update run:
18
+
19
+ gem install rails5_xhr_udpate
20
+
21
+
22
+ ## Running
23
+
24
+ Simply run:
25
+
26
+ ```sh
27
+ rails5_xhr_update --write FILE...
28
+ ```
29
+
30
+ Omit ``--write`` if you don't want to write back to the files, and instead
31
+ output to STDOUT.
32
+
33
+ Consider running the following to find files with potential issues:
34
+
35
+ ```sh
36
+ git grep -l "xhr :" | xargs rails5_xhr_update --write
37
+ ```
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'rails5_xhr_update'
5
+ exit Rails5XhrUpdate::Cli.new.run
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+ require 'unparser'
5
+
6
+ # Provide the Rails5XhrUpdate module including its command-line tool.
7
+ module Rails5XhrUpdate
8
+ AST_TRUE = Parser::AST::Node.new(:true) # rubocop:disable Lint/BooleanSymbol)
9
+
10
+ # Provide the entry point to this program.
11
+ class Cli
12
+ def output(source, path)
13
+ if @options[:write]
14
+ File.open(path, 'w') do |file|
15
+ file.write(source)
16
+ end
17
+ else
18
+ puts source
19
+ end
20
+ end
21
+
22
+ def parse_options
23
+ @options = {}
24
+ OptionParser.new do |config|
25
+ config.banner = 'Usage: rails5_update.rb [options] FILE...'
26
+ config.on('-w', '--write', 'Write changes back to files') do |write|
27
+ @options[:write] = write
28
+ end
29
+ end.parse!
30
+ end
31
+
32
+ def run
33
+ parse_options
34
+ ARGV.each do |path|
35
+ buffer = Parser::Source::Buffer.new(path)
36
+ buffer.read
37
+ new_source = XHRToRails5.new.rewrite(
38
+ buffer, Parser::CurrentRuby.new.parse(buffer)
39
+ )
40
+ output(new_source, path)
41
+ end
42
+ 0
43
+ end
44
+ end
45
+
46
+ # Convert uses of the xhr method to use the rails 5 syntax.
47
+ #
48
+ # For example prior to rails 5 one might write:
49
+ #
50
+ # xhr :get, images_path, limit: 10, sort: 'new'
51
+ #
52
+ # This class will convert that into:
53
+ #
54
+ # get images_path, params: { limit: 10, sort: 'new' }, xhr: true
55
+ #
56
+ # Conversion of xhr methods using headers is also supported:
57
+ #
58
+ # xhr :get, root_path {}, { Accept: => 'application/json' }
59
+ #
60
+ # This class will convert the above into:
61
+ #
62
+ # get root_path, headers: { Accept: => 'application/json' }, xhr: true
63
+ class XHRToRails5 < Parser::TreeRewriter
64
+ def on_send(node)
65
+ return if node.children[1] != :xhr
66
+ arguments = extract_and_validate_arguments(node)
67
+ children = initial_children(node) + add_xhr_node(*arguments)
68
+ replace(node.loc.expression, ast_to_string(node.updated(nil, children)))
69
+ end
70
+
71
+ private
72
+
73
+ def add_xhr_node(params, headers = nil)
74
+ children = []
75
+ children << ast_pair(:headers, headers) unless headers.nil?
76
+ children << ast_pair(:params, params) unless params.children.empty?
77
+ children << ast_pair(:xhr, AST_TRUE)
78
+ [Parser::AST::Node.new(:hash, children)]
79
+ end
80
+
81
+ def extract_and_validate_arguments(node)
82
+ arguments = node.children[4..-1]
83
+ raise Exception, 'should this happen?' if new_syntax?(arguments)
84
+ raise Exception "Unhandled:\n\n #{arguments}" if arguments.size > 2
85
+ arguments
86
+ end
87
+
88
+ def initial_children(node)
89
+ http_method = node.children[2].children[0]
90
+ http_path = node.children[3]
91
+ [nil, http_method, http_path]
92
+ end
93
+
94
+ def new_syntax?(arguments)
95
+ return false if arguments.size != 1
96
+ first_key = arguments[0].children[0].children[0].children[0]
97
+ %i[params headers].include?(first_key)
98
+ end
99
+ end
100
+
101
+ def ast_pair(name, data)
102
+ Parser::AST::Node.new(:pair, [Parser::AST::Node.new(:sym, [name]), data])
103
+ end
104
+
105
+ def ast_to_string(ast)
106
+ string = Unparser.unparse(ast)[0..-2]
107
+ string[string.index('(')] = ' '
108
+ string
109
+ end
110
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Rails5XhrUpdate
4
+ module Rails5XhrUpdate
5
+ VERSION = '0.1.0'
6
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails5_xhr_update
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bryce Boe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unparser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: |
28
+ rails5_xhr_update is a program that can be used to help convert from the
29
+ ``xhr :get, :action`` test syntax used in rails prior to Rails 5, to the Rails
30
+ 5 syntax: ``get :action, xhr: true``.
31
+ email: bryce.boe@appfolio.com
32
+ executables:
33
+ - rails5_xhr_update
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - LICENSE.txt
38
+ - README.md
39
+ - bin/rails5_xhr_update
40
+ - lib/rails5_xhr_update.rb
41
+ - lib/rails5_xhr_update/version.rb
42
+ homepage: https://github.com/appfolio/rails5_xhr_update
43
+ licenses:
44
+ - Simplified BSD
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.6.13
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Program to help update Rails 4 xhr test method calls to rails 5 syntax.
66
+ test_files: []