rails5_xhr_update 0.2.0 → 0.3.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: d19181a6108a0532b5961af8d091c75f83bbd8d0
4
- data.tar.gz: de4de5565dd2ea26ed3b43a55d0781bd1e596c47
3
+ metadata.gz: fee933190f7c4836569a6925afd4281466e6150d
4
+ data.tar.gz: 429a19dc4003f90b164972afd2ef3a6c89c9f33b
5
5
  SHA512:
6
- metadata.gz: 0f4a132a262a96259c36343b7bcda68066340124d48ccd4047bade8c7988f459c7b25ae4be71d8fcac107c33c721d4640de4ffcb0e0af2b4461d4f3e3c91e35d
7
- data.tar.gz: 8cd00a5be4ac82e7dcbb96e5ef39f7b7feb66dd3f1c6118aeac1c7116aad266096d9106f9638e4f25e77f08e9e13afce59b476f4de768c38e7d391e99b5128fd
6
+ metadata.gz: '09777cb0429c67a9e58a4c7597fb3715428cfefdcca1503d266b7238de5d508f17a290d1883a31bba55802f5790e5cd43f3602479617352f7b6d9d9cc1b943e6'
7
+ data.tar.gz: 0a16fb5c56d32435e5bcd710bdcbe41fe599ebcdc8353dd66de974fe609674961e06e9218ac4f3bf7515ff6eee31400328189ded031afd5df523c95fa33546b0
data/CHANGES.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.3.0 (2018/05/26)
4
+
5
+ __Added__
6
+
7
+ * Added conversion support for the `flash` parameter.
8
+
9
+ __Fixed__
10
+
11
+ * The second parameter is correctly named `session` and not `headers`.
12
+
3
13
  ## 0.2.0 (2018/05/25)
4
14
 
5
15
  __Added__
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem](https://img.shields.io/gem/v/rails5_xhr_update.svg)](https://rubygems.org/gems/rails5_xhr_update)
2
+ [![Build Status](https://travis-ci.org/appfolio/rails5_xhr_update.svg?branch=master)](https://travis-ci.org/appfolio/rails5_xhr_update)
3
+
1
4
  # rails5_xhr_update
2
5
 
3
6
  rails5_xhr_update is a program that can be used to help convert from the Rails
@@ -13,6 +16,19 @@ to the equivalent Rails 5 syntax:
13
16
  get images_path, params: { limit: 10, sort: 'new' }, xhr: true
14
17
  ```
15
18
 
19
+ Furthermore, `xhr` calls using `session`, or `flash` parameters are also
20
+ supported. The following call:
21
+
22
+ ```ruby
23
+ xhr :get, images_path, { id: 1 }, { user_ud: 2 }, success: 'logged in'
24
+ ```
25
+
26
+ is translated into:
27
+
28
+ ```ruby
29
+ get images_path, flash: { success: "logged in" }, params: { id: 1 }, session: { user_ud: 2 }, xhr: true
30
+ ```
31
+
16
32
  ## Installation
17
33
 
18
34
  To install rails5_xhr_update run:
@@ -22,7 +38,7 @@ To install rails5_xhr_update run:
22
38
 
23
39
  ## Running
24
40
 
25
- Simply run:
41
+ Execute this program via:
26
42
 
27
43
  ```sh
28
44
  rails5_xhr_update --write FILE...
@@ -31,7 +47,8 @@ rails5_xhr_update --write FILE...
31
47
  Omit ``--write`` if you don't want to write back to the files, and instead
32
48
  output to STDOUT.
33
49
 
34
- Consider running the following to find files with potential issues:
50
+ Consider running the following to locate and run against files with potential
51
+ issues:
35
52
 
36
53
  ```sh
37
54
  git grep -l "xhr :" | rails5_xhr_update --write
@@ -59,4 +76,4 @@ compatible with Rails 5. And you might want to output a `DeprecationWarning`,
59
76
  or even raise an exception when attempting to use the older syntax. To help
60
77
  with that please see our
61
78
  [rails-forward_compatible_controller_tests](https://github.com/appfolio/rails-forward_compatible_controller_tests)
62
- gem.
79
+ gem.
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails5_xhr_update/cli'
4
+ require 'rails5_xhr_update/util'
4
5
  require 'rails5_xhr_update/xhr_to_rails5'
@@ -7,7 +7,7 @@ module Rails5XHRUpdate
7
7
  class Cli
8
8
  def run
9
9
  parse_options
10
- filenames.each do |path|
10
+ paths.each do |path|
11
11
  buffer = Parser::Source::Buffer.new(path)
12
12
  buffer.read
13
13
  new_source = XHRToRails5.new.rewrite(
@@ -20,7 +20,7 @@ module Rails5XHRUpdate
20
20
 
21
21
  private
22
22
 
23
- def filenames
23
+ def paths
24
24
  ARGV.empty? ? STDIN.read.split("\n") : ARGV
25
25
  end
26
26
 
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Provide some helper methods to the module
4
+ module Rails5XHRUpdate
5
+ def self.ast_pair(name, data)
6
+ Parser::AST::Node.new(:pair, [Parser::AST::Node.new(:sym, [name]), data])
7
+ end
8
+
9
+ def self.ast_to_string(ast)
10
+ string = Unparser.unparse(ast)[0..-2]
11
+ string[string.index('(')] = ' '
12
+ string
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rails5XHRUpdate
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'parser'
3
4
  require 'unparser'
4
5
 
5
6
  # Provide the XHRToRails5 class.
@@ -15,36 +16,34 @@ module Rails5XHRUpdate
15
16
  # This class will convert that into:
16
17
  #
17
18
  # 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
19
  class XHRToRails5 < Parser::TreeRewriter
27
20
  def on_send(node)
28
21
  return if node.children[1] != :xhr
29
22
  arguments = extract_and_validate_arguments(node)
30
23
  children = initial_children(node) + add_xhr_node(*arguments)
31
- replace(node.loc.expression, ast_to_string(node.updated(nil, children)))
24
+ replace(node.loc.expression, Rails5XHRUpdate.ast_to_string(
25
+ node.updated(nil, children)
26
+ ))
32
27
  end
33
28
 
34
29
  private
35
30
 
36
- def add_xhr_node(params, headers = nil)
31
+ def add_xhr_node(params = nil, session = nil, flash = nil)
37
32
  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)
33
+ children << Rails5XHRUpdate.ast_pair(:flash, flash) \
34
+ unless flash.nil? || flash.children.empty?
35
+ children << Rails5XHRUpdate.ast_pair(:params, params) \
36
+ unless params.nil? || params.children.empty?
37
+ children << Rails5XHRUpdate.ast_pair(:session, session) \
38
+ unless session.nil? || session.children.empty?
39
+ children << Rails5XHRUpdate.ast_pair(:xhr, AST_TRUE)
41
40
  [Parser::AST::Node.new(:hash, children)]
42
41
  end
43
42
 
44
43
  def extract_and_validate_arguments(node)
45
44
  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
45
+ raise Exception, 'should this happen?' if keyword_args?(arguments)
46
+ raise Exception, "Unhandled:\n\n #{arguments}" if arguments.size > 3
48
47
  arguments
49
48
  end
50
49
 
@@ -54,20 +53,11 @@ module Rails5XHRUpdate
54
53
  [nil, http_method, http_path]
55
54
  end
56
55
 
57
- def new_syntax?(arguments)
56
+ def keyword_args?(arguments)
58
57
  return false if arguments.size != 1
58
+ return false if arguments[0].children.empty?
59
59
  first_key = arguments[0].children[0].children[0].children[0]
60
- %i[params headers].include?(first_key)
60
+ %i[params session flash].include?(first_key)
61
61
  end
62
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
63
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails5_xhr_update
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-25 00:00:00.000000000 Z
11
+ date: 2018-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: unparser
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -40,6 +68,7 @@ files:
40
68
  - bin/rails5_xhr_update
41
69
  - lib/rails5_xhr_update.rb
42
70
  - lib/rails5_xhr_update/cli.rb
71
+ - lib/rails5_xhr_update/util.rb
43
72
  - lib/rails5_xhr_update/version.rb
44
73
  - lib/rails5_xhr_update/xhr_to_rails5.rb
45
74
  homepage: https://github.com/appfolio/rails5_xhr_update