js-rails-routes 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d936f02d646b13f67d7460f66cef7e1804a37f36
4
+ data.tar.gz: 1b438a3b9963d857cb50ba5c6415068f2b0455ab
5
+ SHA512:
6
+ metadata.gz: e83d45c4c891fb9ae5794320723161c67d82b37ac02c14a974c38b373671a5ab6136bbea741e6e4f41fef5bcd5302f30ec2b0e371e2ad8ddc22bfa29034c32d6
7
+ data.tar.gz: 11d00134a46f9f9e631a04929ddc998aad88f7a2669a175c48529a1092a0cc32a0c6c3cb438c39707b0a21ee9f945a9a417d91df49f28f3a2662b6934adbb860
File without changes
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'js-rails-routes'
4
+
5
+ def usage
6
+ puts <<EOF
7
+ js-rails-routes [flags]
8
+
9
+ Run this command from the base directory of a Ruby on Rails project.
10
+ It will inspect the routes for the Rails project and generate a javascript file
11
+ that will add all the [route_name]_path functions to the global namespace.
12
+ By default, the generated javascript is output to stdout.
13
+
14
+ Flags:
15
+ --help
16
+ Displays this message
17
+ --output
18
+ Writes the generated javascript to the file:
19
+ app/assets/javascripts/routes.js
20
+ This operation will fail if you are not in the base directory of a Rails project.
21
+ In other words, it does not try to create the subdirectories
22
+ app/assets/javascripts, so will fail if it can not find them.
23
+ The idea behind requiring you to explicitly type "--output" is to protect
24
+ against accidentally blowing away an old routes.js file, as this _will_
25
+ overwrite it if one exists.
26
+ --no-link-to
27
+ By default this utility produces a function named "link_to" that mimics a
28
+ simplified version of the Rails method by the same name. This flag supresses
29
+ creating that function.
30
+ EOF
31
+
32
+ abort
33
+ end
34
+
35
+ to_file = false
36
+ do_link_to = true
37
+
38
+ until ARGV.empty?
39
+ arg = ARGV.shift
40
+ if arg == '--help'
41
+ usage
42
+ elsif arg == '--output'
43
+ to_file = true
44
+ elsif arg == '--no-link-to'
45
+ do_link_to = false
46
+ else
47
+ usage
48
+ end
49
+ end
50
+
51
+ out = STDOUT
52
+
53
+ if to_file
54
+ begin
55
+ out = File.open("app/assets/javascripts/routes.js", "w")
56
+ rescue
57
+ STDERR.puts "You must be in the base directory of a Rails project in order to output"
58
+ STDERR.puts "to a file!!!"
59
+ abort
60
+ end
61
+ end
62
+
63
+ text = `rake routes`
64
+ # Protect against goofy carriage returns on non-unix systems
65
+ text.gsub!(/\r\n?/, "\n")
66
+
67
+ # Get rid of the header
68
+ text.sub!(/\A[^\n]+\n/, '')
69
+
70
+ out.puts JsRailsRoutes::Routes.new(text).create_javascript(do_link_to)
@@ -0,0 +1,120 @@
1
+ module JsRailsRoutes
2
+ VERSION = "1.0.0"
3
+
4
+ class Routes
5
+
6
+ def initialize(text)
7
+ @text = text
8
+ end
9
+
10
+ def create_javascript(with_link_to = true)
11
+ result = <<EOF
12
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13
+ //
14
+ // This file is auto-generated by js-rails-routes #{VERSION}
15
+ //
16
+ // DO NOT EDIT BY HAND!!
17
+ //
18
+
19
+ EOF
20
+
21
+ if with_link_to
22
+ result += generate_link_to
23
+ end
24
+
25
+
26
+ lines = @text.split("\n")
27
+
28
+ lines.each do |line|
29
+ # clear leading whitespace
30
+ line.sub!(/^\s+/, '')
31
+ (name, method, format, action) = line.split(/\s+/)
32
+
33
+ # action will only contain something if there were at least four segments
34
+ # to split. This is a little ass-backwards. Really when action contains no
35
+ # non-whitespace, what it really means is that name contained no non-whitespace.
36
+ if action =~ /\S/
37
+ # Get rid of the (.:format) business at the end of each format
38
+ format.gsub!(/\(.*\)/, '')
39
+
40
+ # Get rid of the leading slash
41
+ format.sub!(/^\//, '')
42
+
43
+ # Split it into its parts
44
+ parts = format.split("/")
45
+
46
+ args = []
47
+ parts.each do |part|
48
+ if part =~ /^:/
49
+ args.push part.sub(/^:/, '')
50
+ end
51
+ end
52
+
53
+ if args.empty?
54
+ # This is the easy case. There are no parameters we need to deal with, just
55
+ # a simple path
56
+
57
+ result += <<EOF
58
+
59
+ var #{name}_path = function() {
60
+ return "/#{format}";
61
+ }
62
+ EOF
63
+ else
64
+ # The trickier case. We want to accept values as args. The first arg could
65
+ # be a hash object, containing all the information, or we could have several
66
+ # primitive arguments that appear in the order that they do in the format
67
+ result += <<EOF
68
+
69
+ var #{name}_path = function(#{ args.join(", ") }) {
70
+ if (#{args[0]} instanceof Object) {
71
+ EOF
72
+ result += ' return '
73
+ components = []
74
+ parts.each do |part|
75
+ components.push '"/"'
76
+ if part =~ /^:/
77
+ components.push args[0] + '["' + part.sub(/^:/, '') + '"]'
78
+ else
79
+ components.push '"' + part + '"'
80
+ end
81
+ end
82
+ result += components.join(" + ") + ";\n"
83
+ result += <<EOF
84
+ } else {
85
+ EOF
86
+
87
+ result += ' return '
88
+ components = []
89
+ parts.each do |part|
90
+ components.push '"/"'
91
+ if part =~ /^:/
92
+ components.push part.sub(/^:/, '')
93
+ else
94
+ components.push '"' + part + '"'
95
+ end
96
+ end
97
+
98
+ result += components.join(' + ') + ";\n"
99
+ result += <<EOF
100
+ }
101
+ }
102
+ EOF
103
+ end
104
+ end
105
+ end
106
+
107
+ result
108
+ end
109
+
110
+
111
+ def generate_link_to
112
+ # This is a fairly silly method.
113
+ # It just regurgitates javascript I've written elsewhere by hand.
114
+
115
+ File.read File.expand_path("../js-rails-routes/js/link_to.js", __FILE__)
116
+ end
117
+
118
+
119
+ end
120
+ end
@@ -0,0 +1,28 @@
1
+ // Generated by CoffeeScript 1.9.3
2
+ var link_to;
3
+
4
+ link_to = function(label, link, opts) {
5
+ var key, ref, result, value;
6
+ if (opts == null) {
7
+ opts = {};
8
+ }
9
+ result = '<a href="' + link + '"';
10
+ if (opts.id != null) {
11
+ result += ' id="' + opts.id + '"';
12
+ }
13
+ if (opts["class"] != null) {
14
+ result += ' class="' + opts["class"] + '"';
15
+ }
16
+ if (opts.method != null) {
17
+ result += ' rel="nofollow" data-method="' + opts.method + '"';
18
+ }
19
+ if (opts.data != null) {
20
+ ref = opts.data;
21
+ for (key in ref) {
22
+ value = ref[key];
23
+ result += ' data-' + key + '="' + value + '"';
24
+ }
25
+ }
26
+ result += '>' + label + '</a>';
27
+ return result;
28
+ };
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-rails-routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Bowers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.13'
41
+ description: "+js-rails-routes+ is a utility for generating JavaScript equivalents
42
+ to the +<route>_path+\nfunctions provided by {Ruby on Rails}[https://github.com/rails/rails].
43
+ \ This allows you\nto do very similar things in your {+ejs+}[https://rubygems.org/gems/ejs/]
44
+ JavaScript templates as you would in your +erb+\nruby templates. You can move html
45
+ rendering to the client and keep it looking very \nsimilar to how it would look
46
+ on the server.\n\nFor example, if you have a model +Item+ and a simple route to
47
+ list all the items, a link\nto that items page (using an explicit +a+ anchor tag
48
+ instead of the Rails +link_to+)\nwould look the same in either an +erb+ file or
49
+ an +ejs+ file:\n\n <a href=\"<%= items_path() %>\">List all Items</a>\n\nThis
50
+ gem was originally developed as part of the {MVCoffee}[http://mvcoffee.org] suite
51
+ of tools, and integrates strongly with the {mvcoffee.js}[https://github.com/kirkbowers/mvcoffee]
52
+ CoffeeScript MVC framework."
53
+ email:
54
+ - kirkbowers@yahoo.com
55
+ executables:
56
+ - js-rails-routes
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gemtest"
61
+ - bin/js-rails-routes
62
+ - lib/js-rails-routes.rb
63
+ - lib/js-rails-routes/js/link_to.js
64
+ homepage: https://github.com/kirkbowers/js-rails-routes
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options:
70
+ - "--main"
71
+ - README.txt
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.2.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: "+js-rails-routes+ is a utility for generating JavaScript equivalents to
90
+ the +<route>_path+ functions provided by {Ruby on Rails}[https://github.com/rails/rails]"
91
+ test_files: []