j-walker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/plugins/blue-ridge"]
2
+ path = vendor/plugins/blue-ridge
3
+ url = http://github.com/relevance/blue-ridge.git
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Henry Hsu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,91 @@
1
+ # j_walker
2
+
3
+ A way to run JavaScript based on Rails controller actions. This gives you the freedom to DRY up your JavaScript, or modularize code around REST resources, whatever you can think of!
4
+
5
+ For example, with `jWalker` you can route a JS function to `WelcomeController#index`. When a user visits that page, jWalker will automatically cross to the correct route via `controller_name`/`action_name` and execute your JS.
6
+
7
+ With a few quick lines of code, you can get Rails and jWalker working in unison. Read on!
8
+
9
+ *****
10
+ *****
11
+
12
+ ## Installation
13
+
14
+ First:
15
+
16
+ gem install j-walker
17
+
18
+ ### Rails
19
+
20
+ config.gem 'j-walker', :lib => 'j_walker
21
+
22
+ In the body of your layout (preferably the end):
23
+
24
+ <%= render 'shared/j_walker' %>
25
+
26
+ This is the glue that bonds Rails and jWalker together.
27
+
28
+ *****
29
+
30
+ ### Javascript
31
+
32
+ Requires `jQuery`.
33
+
34
+ #### Sprockets
35
+
36
+ This project was structured with [Sprockets](http://getsprockets.org) in mind. I'm assuming you have [`sprocket-rails`](http://github.com/sstephenson/sprockets-rails/tree/master) set up.
37
+
38
+ Just like any sprocketized dependency:
39
+
40
+ //= require <j_walker>
41
+
42
+ in `application.js` (or any other source file preprocessed by Sprockets)
43
+
44
+ #### Non-sprockets
45
+
46
+ I can't offer any suggestions other than cloning `src/j_walker.js` into wherever your javascript files are stored and
47
+ adding a `<%= javascript_include_tag 'j_walker' %>` into your `application` layout.
48
+
49
+ *****
50
+ *****
51
+
52
+ ## Usage
53
+
54
+ To set up routes (i.e., in `application.js` or any file that's loaded in every single Rails page):
55
+
56
+ jWalker.route("welcome", "index", function() {
57
+ say_hello();
58
+ })
59
+
60
+ jWalker.route("door", "show", function() {
61
+ say_goodbye();
62
+ })
63
+
64
+ Routes should be invoked using:
65
+
66
+ jWalker.cross("welcome", "index");
67
+
68
+ This is exactly what the `j_walker` partial does, except it plugs in the `controller_name` and `action_name`
69
+
70
+ *****
71
+ *****
72
+
73
+ ## Testing
74
+
75
+ This thing was developed test-first using [blue-ridge](http://github.com/relevance/blue-ridge). If you want to verify that everything works as expected, make sure the `blue-ridge` submodule is updated and open `spec/javascripts/fixtures/j_walker.html` in Firefox.
76
+
77
+ *****
78
+ *****
79
+
80
+ ## Future
81
+
82
+ * Params-based routing (instead of just controller and action)
83
+ * Route aliasing
84
+ * Full-stack testing
85
+
86
+ *****
87
+ *****
88
+
89
+ ## Copyright
90
+
91
+ Copyright (c) 2010 Henry Hsu. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "j-walker"
8
+ gem.summary = %Q{Modularize your *.js, the Rails way. Jaywalk your Rails routes.}
9
+ gem.description = %Q{A way to run JavaScript based on Rails controller actions. This gives you the freedom to DRY up your JavaScript, or modularize code around REST resources, whatever you can think of!}
10
+ gem.email = "henry@qlane.com"
11
+ gem.homepage = "http://github.com/hsume2/j-walker"
12
+ gem.authors = ["Henry Hsu"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem.add_development_dependency "cucumber", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ begin
38
+ require 'cucumber/rake/task'
39
+ Cucumber::Rake::Task.new(:features)
40
+
41
+ task :features => :check_dependencies
42
+ rescue LoadError
43
+ task :features do
44
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
45
+ end
46
+ end
47
+
48
+ task :default => :spec
49
+
50
+ begin
51
+ require 'yard'
52
+ YARD::Rake::YardocTask.new
53
+ rescue LoadError
54
+ task :yardoc do
55
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
56
+ end
57
+ end
58
+
59
+ Dir[File.join('tasks', '*.rake')].each { |f| load f }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,5 @@
1
+ <script type="text/javascript" charset="utf-8">
2
+ jQuery(document).ready(function($) {
3
+ jWalker.cross("<%= controller_name %>", "<%= action_name %>", jQuery);
4
+ });
5
+ </script>
data/j-walker.gemspec ADDED
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{j-walker}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Henry Hsu"]
12
+ s.date = %q{2010-05-21}
13
+ s.description = %q{A way to run JavaScript based on Rails controller actions. This gives you the freedom to DRY up your JavaScript, or modularize code around REST resources, whatever you can think of!}
14
+ s.email = %q{henry@qlane.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".gitmodules",
23
+ "LICENSE",
24
+ "README.markdown",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "app/views/shared/_j_walker.html.erb",
28
+ "j-walker.gemspec",
29
+ "lib/j_walker.rb",
30
+ "rails/init.rb",
31
+ "spec/j_walker_spec.rb",
32
+ "spec/javascripts/fixtures/j_walker.html",
33
+ "spec/javascripts/j_walker_spec.js",
34
+ "spec/javascripts/spec_helper.js",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb",
37
+ "src/j_walker.js"
38
+ ]
39
+ s.homepage = %q{http://github.com/hsume2/j-walker}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.6}
43
+ s.summary = %q{Modularize your *.js, the Rails way. Jaywalk your Rails routes.}
44
+ s.test_files = [
45
+ "spec/j_walker_spec.rb",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
55
+ s.add_development_dependency(%q<yard>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_dependency(%q<yard>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ s.add_dependency(%q<yard>, [">= 0"])
63
+ end
64
+ end
65
+
data/lib/j_walker.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'sprockets'
2
+
3
+ Sprockets::Environment.class_eval do
4
+ def initialize_with_j_walker(root, load_path = [])
5
+ load_path.unshift File.join(File.dirname(__FILE__), %w[.. src])
6
+ initialize_without_j_walker(root, load_path)
7
+ end
8
+
9
+ alias_method :initialize_without_j_walker, :initialize unless method_defined?(:initialize_without_j_walker)
10
+ alias_method :initialize, :initialize_with_j_walker
11
+ end
data/rails/init.rb ADDED
File without changes
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "JWalker" do
4
+ before do
5
+ @sec = Sprockets::Secretary.new
6
+ @j_walker_path = File.expand_path(File.join(File.dirname(__FILE__), %w[.. src]))
7
+ @env = @sec.environment
8
+ end
9
+
10
+ it "should be added to sprockets load path" do
11
+ j_walker_pathname = Sprockets::Pathname.new(@env, @j_walker_path)
12
+
13
+ @env.load_path.should include(j_walker_pathname)
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml">
3
+
4
+ <head>
5
+ <title>jWalker In-Browser Test Runner | JavaScript Testing Results</title>
6
+ <link rel="stylesheet" href="../../../vendor/plugins/blue-ridge/generators/blue_ridge/templates/screw.css" type="text/css" charset="utf-8" />
7
+ <script type="text/javascript" src="../../../vendor/plugins/blue-ridge/lib/blue-ridge.js"></script>
8
+ </head>
9
+
10
+ <body>
11
+ <!-- Put any HTML fixture elements here. -->
12
+ </body>
13
+ </html>
@@ -0,0 +1,124 @@
1
+ require('spec_helper.js');
2
+ require('../../src/j_walker.js');
3
+
4
+ Screw.Unit(function() {
5
+ describe("jWalker", function(){
6
+ before(function(){
7
+ jWalker.clear();
8
+ });
9
+
10
+ it("exists", function(){ expect(jWalker).to_not(equal, null); });
11
+
12
+ describe("#routes", function(){
13
+ it("should respond", function(){ expect(jWalker).to(respond_to, "routes"); });
14
+ it("should be object", function(){
15
+ expect(jWalker.routes()).to(equal, {});
16
+ });
17
+ });
18
+
19
+ describe("#clear", function(){
20
+ it("should clear", function(){
21
+ jWalker.route("taxons", "index");
22
+ expect(jWalker.routes()).to_not(equal, {});
23
+
24
+ jWalker.clear();
25
+
26
+ expect(jWalker.routes()).to(equal, {});
27
+ });
28
+ it("should respond", function(){ expect(jWalker).to(respond_to, "clear"); });
29
+ });
30
+
31
+ describe("#route", function(){
32
+ it("should route controller and action to function", function(){
33
+ var my_func = function() {}
34
+ jWalker.route("taxons", "index", my_func);
35
+
36
+ var routes = jWalker.routes();
37
+
38
+ expect(routes).to(equal, {
39
+ "taxons": {
40
+ "index": my_func
41
+ }
42
+ });
43
+ });
44
+
45
+ it("should route multiple actions for controller", function(){
46
+ var my_func = function() {}
47
+ jWalker.route("taxons", "index", my_func);
48
+ jWalker.route("taxons", "show", my_func);
49
+
50
+ var routes = jWalker.routes();
51
+
52
+ expect(routes).to(equal, {
53
+ "taxons": {
54
+ "index": my_func,
55
+ "show": my_func
56
+ }
57
+ });
58
+ });
59
+
60
+ it("should route default action for controller", function(){
61
+ var my_func = function() {}
62
+ jWalker.route("taxons", null, my_func);
63
+
64
+ var routes = jWalker.routes();
65
+
66
+ expect(routes).to(equal, {
67
+ "taxons": {
68
+ "/jWalker.defaultAction/": my_func
69
+ }
70
+ });
71
+ });
72
+ it("should respond", function(){ expect(jWalker).to(respond_to, "route"); });
73
+ });
74
+
75
+ describe("#cross", function(){
76
+ it("should invoke controller and action", function(){
77
+ var received_action;
78
+ jWalker.route("taxons", "index", function() {
79
+ received_action = this;
80
+ });
81
+
82
+ jWalker.cross("taxons", "index", jQuery);
83
+
84
+ expect(received_action).to(equal, jQuery);
85
+ });
86
+
87
+ it("should invoke default action for controller", function(){
88
+ var received_action_count = 0;
89
+ jWalker.route("taxons", null, function() {
90
+ received_action_count += 1;
91
+ });
92
+
93
+ jWalker.cross("taxons", "index", jQuery);
94
+ jWalker.cross("taxons", "show", jQuery);
95
+ jWalker.cross("taxons", null, jQuery);
96
+ jWalker.cross("other", null, jQuery);
97
+
98
+ expect(received_action_count).to(equal, 3);
99
+ });
100
+
101
+ it("should quietly invoke bad params", function(){ // TODO: decide on this
102
+ jWalker.cross(null, "index", jQuery);
103
+ jWalker.cross("index", null, jQuery);
104
+ });
105
+ it("should respond", function(){ expect(jWalker).to(respond_to, "cross"); });
106
+ });
107
+
108
+ describe("#isRouted", function(){
109
+ it("should check if not routed", function(){
110
+ expect(jWalker.isRouted("taxons", "show")).to(equal, false);
111
+ });
112
+
113
+ it("should check if routed", function(){
114
+ expect(jWalker.isRouted("taxons", "show")).to(equal, false);
115
+
116
+ var my_func = function() {}
117
+ jWalker.route("taxons", "index", my_func);
118
+
119
+ expect(jWalker.isRouted("taxons", "index")).to(equal, true);
120
+ });
121
+ it("should respond", function(){ expect(jWalker).to(respond_to, "isRouted"); });
122
+ });
123
+ });
124
+ });
@@ -0,0 +1,21 @@
1
+ Screw.Matchers["respond_to"] = (function($) {
2
+ var is_defined = false;
3
+ var is_func = false;
4
+ var func;
5
+
6
+ return {
7
+ match: function(method, object) {
8
+ eval("func = object." + method + ";");
9
+ is_defined = func != undefined;
10
+ is_func = typeof(func) == "function";
11
+ return is_defined && is_func;
12
+ },
13
+ failure_message: function(expected, actual, not) {
14
+ if (!is_defined) {
15
+ return 'expected ' + $.print(actual) + (not ? ' not' : '') + ' to respond to: ' + expected;
16
+ } else if (!is_func) {
17
+ return 'expected ' + $.print(expected) + (not ? ' not' : '') + ' to be a function, got: ' + typeof(func);
18
+ }
19
+ }
20
+ }
21
+ })(jQuery);
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'j_walker'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
data/src/j_walker.js ADDED
@@ -0,0 +1,51 @@
1
+ var jWalker = (function($) {
2
+ var _routes = {};
3
+ var _default_action = "/jWalker.defaultAction/"; // An impossible name for a Rails action to have... right?
4
+
5
+ return {
6
+ route: function(controller_name, action_name, function_or_function_name) {
7
+ var action_name_object = {};
8
+
9
+ if (action_name == null)
10
+ action_name = _default_action;
11
+
12
+ action_name_object[action_name] = function_or_function_name;
13
+
14
+ var controller_name_object = _routes[controller_name];
15
+
16
+ if (controller_name_object == null) {
17
+ _routes[controller_name] = action_name_object;
18
+ } else {
19
+ jQuery.extend(_routes[controller_name], action_name_object);
20
+ }
21
+ },
22
+ cross: function(controller_name, action_name, direct_injection) {
23
+ var controller = _routes[controller_name];
24
+ if (controller != null) {
25
+ var action = controller[action_name];
26
+
27
+ if (action == null)
28
+ action = controller[_default_action];
29
+
30
+ if (action != null) {
31
+ action.call(direct_injection);
32
+ }
33
+ }
34
+ },
35
+ routes: function() {
36
+ return _routes;
37
+ },
38
+ clear: function() {
39
+ _routes = {};
40
+ },
41
+ isRouted: function(controller_name, action_name) {
42
+ var controller = _routes[controller_name];
43
+ if (controller == null)
44
+ return false;
45
+ var action = controller[action_name];
46
+ if (action == null)
47
+ return false;
48
+ return true;
49
+ }
50
+ }
51
+ })(jQuery);
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: j-walker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Henry Hsu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-21 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: yard
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: A way to run JavaScript based on Rails controller actions. This gives you the freedom to DRY up your JavaScript, or modularize code around REST resources, whatever you can think of!
47
+ email: henry@qlane.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.markdown
55
+ files:
56
+ - .document
57
+ - .gitignore
58
+ - .gitmodules
59
+ - LICENSE
60
+ - README.markdown
61
+ - Rakefile
62
+ - VERSION
63
+ - app/views/shared/_j_walker.html.erb
64
+ - j-walker.gemspec
65
+ - lib/j_walker.rb
66
+ - rails/init.rb
67
+ - spec/j_walker_spec.rb
68
+ - spec/javascripts/fixtures/j_walker.html
69
+ - spec/javascripts/j_walker_spec.js
70
+ - spec/javascripts/spec_helper.js
71
+ - spec/spec.opts
72
+ - spec/spec_helper.rb
73
+ - src/j_walker.js
74
+ has_rdoc: true
75
+ homepage: http://github.com/hsume2/j-walker
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Modularize your *.js, the Rails way. Jaywalk your Rails routes.
104
+ test_files:
105
+ - spec/j_walker_spec.rb
106
+ - spec/spec_helper.rb