rails5_xhr_update 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f497a964384cd6e1d4ecf8548e04523b0d67d68
4
- data.tar.gz: a01b99820e5eeb16917828abe3fabaf4790558cd
3
+ metadata.gz: d19181a6108a0532b5961af8d091c75f83bbd8d0
4
+ data.tar.gz: de4de5565dd2ea26ed3b43a55d0781bd1e596c47
5
5
  SHA512:
6
- metadata.gz: 391227f5befb412d105e3562746a6d62d42f1221afdc05a8661632d68f002ec1c9d910364b4a2bd920e6dea7558d6a76ea1e9286f61beba078581ff0e2ef7821
7
- data.tar.gz: 3fd9861315aefd2be0862e104ff6c80164a4170fb6a7630e7a7c6eff6aea408b68c55d541ff8a9fde446836e37653a6ec497382367bd280561891a320eef141a
6
+ metadata.gz: 0f4a132a262a96259c36343b7bcda68066340124d48ccd4047bade8c7988f459c7b25ae4be71d8fcac107c33c721d4640de4ffcb0e0af2b4461d4f3e3c91e35d
7
+ data.tar.gz: 8cd00a5be4ac82e7dcbb96e5ef39f7b7feb66dd3f1c6118aeac1c7116aad266096d9106f9638e4f25e77f08e9e13afce59b476f4de768c38e7d391e99b5128fd
@@ -0,0 +1,14 @@
1
+ # Change Log
2
+
3
+ ## 0.2.0 (2018/05/25)
4
+
5
+ __Added__
6
+
7
+ * Filenames can be provided via STDIN when not provided on the command line.
8
+
9
+ ## 0.1.0 (2018/05/24)
10
+
11
+ __Added__
12
+
13
+ * Initial version of rails5_xhr_update with `--write` option and takes
14
+ filenames as command line arguments.
data/README.md CHANGED
@@ -1,15 +1,16 @@
1
1
  # rails5_xhr_update
2
2
 
3
- rails5_xhr_update is a program that can be used to help convert from the:
3
+ rails5_xhr_update is a program that can be used to help convert from the Rails
4
+ 4 xhr test syntax, like the following:
4
5
 
5
6
  ```ruby
6
- xhr :get, :action
7
+ xhr :get, images_path, limit: 10, sort: 'new'
7
8
  ```
8
9
 
9
- test syntax used in rails prior to Rails 5, to the Rails 5 syntax:
10
+ to the equivalent Rails 5 syntax:
10
11
 
11
12
  ```ruby
12
- get :action, xhr: true
13
+ get images_path, params: { limit: 10, sort: 'new' }, xhr: true
13
14
  ```
14
15
 
15
16
  ## Installation
@@ -33,5 +34,29 @@ output to STDOUT.
33
34
  Consider running the following to find files with potential issues:
34
35
 
35
36
  ```sh
36
- git grep -l "xhr :" | xargs rails5_xhr_update --write
37
+ git grep -l "xhr :" | rails5_xhr_update --write
37
38
  ```
39
+
40
+
41
+ ## See Also
42
+
43
+ ### Handling non-XHR cases
44
+
45
+ As part of the upgrade to Rails 5 one also needs to use keyword arguments for
46
+ `params` and `headers` when calling these *request* test methods. Fortunately,
47
+ [rubocop](https://github.com/bbatsov/rubocop) can help handle that conversion:
48
+
49
+
50
+ ```sh
51
+ rubocop -a --only Rails/HttpPositionalArguments PATH
52
+ ```
53
+
54
+ ### Supporting Rails 5 syntax in Rails 4
55
+
56
+ Finally, with your project, it might be difficult to do all this conversion
57
+ work at once. You might instead, prefer to remain on Rails 4, but be forward
58
+ compatible with Rails 5. And you might want to output a `DeprecationWarning`,
59
+ or even raise an exception when attempting to use the older syntax. To help
60
+ with that please see our
61
+ [rails-forward_compatible_controller_tests](https://github.com/appfolio/rails-forward_compatible_controller_tests)
62
+ gem.
@@ -2,4 +2,4 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'rails5_xhr_update'
5
- exit Rails5XhrUpdate::Cli.new.run
5
+ exit Rails5XHRUpdate::Cli.new.run
@@ -1,110 +1,4 @@
1
1
  # frozen_string_literal: true
2
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
3
+ require 'rails5_xhr_update/cli'
4
+ require 'rails5_xhr_update/xhr_to_rails5'
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ module Rails5XHRUpdate
6
+ # Provide the entry point to this program.
7
+ class Cli
8
+ def run
9
+ parse_options
10
+ filenames.each do |path|
11
+ buffer = Parser::Source::Buffer.new(path)
12
+ buffer.read
13
+ new_source = XHRToRails5.new.rewrite(
14
+ buffer, Parser::CurrentRuby.new.parse(buffer)
15
+ )
16
+ output(new_source, path)
17
+ end
18
+ 0
19
+ end
20
+
21
+ private
22
+
23
+ def filenames
24
+ ARGV.empty? ? STDIN.read.split("\n") : ARGV
25
+ end
26
+
27
+ def output(source, path)
28
+ if @options[:write]
29
+ File.open(path, 'w') do |file|
30
+ file.write(source)
31
+ end
32
+ else
33
+ puts source
34
+ end
35
+ end
36
+
37
+ def parse_options
38
+ @options = {}
39
+ OptionParser.new do |config|
40
+ config.banner = <<~USAGE
41
+ Usage: rails5_update.rb [options] FILE...
42
+
43
+ Files can also be provided via stdin when not provided as a command line argument.
44
+ USAGE
45
+ config.on('-w', '--write', 'Write changes back to files') do |write|
46
+ @options[:write] = write
47
+ end
48
+ end.parse!
49
+ end
50
+ end
51
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Rails5XhrUpdate
4
- module Rails5XhrUpdate
5
- VERSION = '0.1.0'
3
+ module Rails5XHRUpdate
4
+ VERSION = '0.2.0'
6
5
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'unparser'
4
+
5
+ # Provide the XHRToRails5 class.
6
+ module Rails5XHRUpdate
7
+ AST_TRUE = Parser::AST::Node.new(:true) # rubocop:disable Lint/BooleanSymbol)
8
+
9
+ # Convert uses of the xhr method to use the rails 5 syntax.
10
+ #
11
+ # For example prior to rails 5 one might write:
12
+ #
13
+ # xhr :get, images_path, limit: 10, sort: 'new'
14
+ #
15
+ # This class will convert that into:
16
+ #
17
+ # get images_path, params: { limit: 10, sort: 'new' }, xhr: true
18
+ #
19
+ # Conversion of xhr methods using headers is also supported:
20
+ #
21
+ # xhr :get, root_path {}, { Accept: => 'application/json' }
22
+ #
23
+ # This class will convert the above into:
24
+ #
25
+ # get root_path, headers: { Accept: => 'application/json' }, xhr: true
26
+ class XHRToRails5 < Parser::TreeRewriter
27
+ def on_send(node)
28
+ return if node.children[1] != :xhr
29
+ arguments = extract_and_validate_arguments(node)
30
+ children = initial_children(node) + add_xhr_node(*arguments)
31
+ replace(node.loc.expression, ast_to_string(node.updated(nil, children)))
32
+ end
33
+
34
+ private
35
+
36
+ def add_xhr_node(params, headers = nil)
37
+ children = []
38
+ children << ast_pair(:headers, headers) unless headers.nil?
39
+ children << ast_pair(:params, params) unless params.children.empty?
40
+ children << ast_pair(:xhr, AST_TRUE)
41
+ [Parser::AST::Node.new(:hash, children)]
42
+ end
43
+
44
+ def extract_and_validate_arguments(node)
45
+ arguments = node.children[4..-1]
46
+ raise Exception, 'should this happen?' if new_syntax?(arguments)
47
+ raise Exception "Unhandled:\n\n #{arguments}" if arguments.size > 2
48
+ arguments
49
+ end
50
+
51
+ def initial_children(node)
52
+ http_method = node.children[2].children[0]
53
+ http_path = node.children[3]
54
+ [nil, http_method, http_path]
55
+ end
56
+
57
+ def new_syntax?(arguments)
58
+ return false if arguments.size != 1
59
+ first_key = arguments[0].children[0].children[0].children[0]
60
+ %i[params headers].include?(first_key)
61
+ end
62
+ end
63
+
64
+ def ast_pair(name, data)
65
+ Parser::AST::Node.new(:pair, [Parser::AST::Node.new(:sym, [name]), data])
66
+ end
67
+
68
+ def ast_to_string(ast)
69
+ string = Unparser.unparse(ast)[0..-2]
70
+ string[string.index('(')] = ' '
71
+ string
72
+ end
73
+ end
metadata CHANGED
@@ -1,47 +1,50 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails5_xhr_update
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryce Boe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-24 00:00:00.000000000 Z
11
+ date: 2018-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unparser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '0.2'
27
27
  description: |
28
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``.
29
+ ``xhr :get, :action`` test syntax used in rails prior to Rails 5, to the
30
+ Rails 5 syntax: ``get :action, xhr: true``.
31
31
  email: bryce.boe@appfolio.com
32
32
  executables:
33
33
  - rails5_xhr_update
34
34
  extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
+ - CHANGES.md
37
38
  - LICENSE.txt
38
39
  - README.md
39
40
  - bin/rails5_xhr_update
40
41
  - lib/rails5_xhr_update.rb
42
+ - lib/rails5_xhr_update/cli.rb
41
43
  - lib/rails5_xhr_update/version.rb
44
+ - lib/rails5_xhr_update/xhr_to_rails5.rb
42
45
  homepage: https://github.com/appfolio/rails5_xhr_update
43
46
  licenses:
44
- - Simplified BSD
47
+ - BSD-2-Clause
45
48
  metadata: {}
46
49
  post_install_message:
47
50
  rdoc_options: []
@@ -62,5 +65,5 @@ rubyforge_project:
62
65
  rubygems_version: 2.6.13
63
66
  signing_key:
64
67
  specification_version: 4
65
- summary: Program to help update Rails 4 xhr test method calls to rails 5 syntax.
68
+ summary: Update Rails 4 xhr test method calls to rails 5 syntax.
66
69
  test_files: []