rails-js-routes 0.2.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.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +3 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +127 -0
- data/MIT-LICENSE +20 -0
- data/README.md +161 -0
- data/Rakefile +25 -0
- data/app/assets/javascripts/routes.js +327 -0
- data/lib/rails/js/routes.rb +10 -0
- data/lib/rails/js/routes/engine.rb +12 -0
- data/lib/rails/js/routes/version.rb +7 -0
- data/lib/rails/js/routes/view_helpers.rb +25 -0
- data/rails-js-routes.gemspec +24 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +14 -0
- data/test/dummy/app/assets/javascripts/testing.js +2 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/controllers/testing_controller.rb +6 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/testing_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/layouts/application.html.erb +15 -0
- data/test/dummy/app/views/testing/index.html.erb +0 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +22 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +83 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +3 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/spec/javascripts/route_dispatch_spec.js +165 -0
- data/test/dummy/spec/javascripts/spec_helper.js +31 -0
- data/test/dummy/spec/teaspoon_env.rb +13 -0
- data/test/dummy/test/helpers/testing_helper_test.rb +4 -0
- data/test/teaspoon_env.rb +24 -0
- data/test/test_helper.rb +18 -0
- data/test/testing_controller_test.rb +12 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1cebb23dcc0242dba9c4c8de6a37f79737ef6199
|
4
|
+
data.tar.gz: f5437c7549d77798db7f435ada04920c62a58b7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0fccbf3b17c3c6c9f9517d618bbc2bba4a9dd96e34c9bdc220b359e4c0baa4d859f36081972f611e9a6c42c5298055e2c6568cfa08c4283948ad7e73c49a9ec3
|
7
|
+
data.tar.gz: 39a428a03ef6a41eaaf1a3e717e6e27bf3e772684b6dd3fd9664e7957a1fe07c9f0fbd696397ef287d25720ee0a13aed6c73466fc0f6ca7b54ceaeedfa94974c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in penis.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use debugger
|
14
|
+
# gem 'debugger'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails-js-routes (0.2.0)
|
5
|
+
rails (~> 4)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (4.1.8)
|
11
|
+
actionpack (= 4.1.8)
|
12
|
+
actionview (= 4.1.8)
|
13
|
+
mail (~> 2.5, >= 2.5.4)
|
14
|
+
actionpack (4.1.8)
|
15
|
+
actionview (= 4.1.8)
|
16
|
+
activesupport (= 4.1.8)
|
17
|
+
rack (~> 1.5.2)
|
18
|
+
rack-test (~> 0.6.2)
|
19
|
+
actionview (4.1.8)
|
20
|
+
activesupport (= 4.1.8)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubis (~> 2.7.0)
|
23
|
+
activemodel (4.1.8)
|
24
|
+
activesupport (= 4.1.8)
|
25
|
+
builder (~> 3.1)
|
26
|
+
activerecord (4.1.8)
|
27
|
+
activemodel (= 4.1.8)
|
28
|
+
activesupport (= 4.1.8)
|
29
|
+
arel (~> 5.0.0)
|
30
|
+
activesupport (4.1.8)
|
31
|
+
i18n (~> 0.6, >= 0.6.9)
|
32
|
+
json (~> 1.7, >= 1.7.7)
|
33
|
+
minitest (~> 5.1)
|
34
|
+
thread_safe (~> 0.1)
|
35
|
+
tzinfo (~> 1.1)
|
36
|
+
arel (5.0.1.20140414130214)
|
37
|
+
builder (3.2.2)
|
38
|
+
capybara (2.4.4)
|
39
|
+
mime-types (>= 1.16)
|
40
|
+
nokogiri (>= 1.3.3)
|
41
|
+
rack (>= 1.0.0)
|
42
|
+
rack-test (>= 0.5.4)
|
43
|
+
xpath (~> 2.0)
|
44
|
+
cliver (0.3.2)
|
45
|
+
coveralls (0.7.2)
|
46
|
+
multi_json (~> 1.3)
|
47
|
+
rest-client (= 1.6.7)
|
48
|
+
simplecov (>= 0.7)
|
49
|
+
term-ansicolor (= 1.2.2)
|
50
|
+
thor (= 0.18.1)
|
51
|
+
docile (1.1.5)
|
52
|
+
erubis (2.7.0)
|
53
|
+
hike (1.2.3)
|
54
|
+
i18n (0.6.11)
|
55
|
+
json (1.8.1)
|
56
|
+
mail (2.6.3)
|
57
|
+
mime-types (>= 1.16, < 3)
|
58
|
+
mime-types (2.4.3)
|
59
|
+
mini_portile (0.6.1)
|
60
|
+
minitest (5.4.3)
|
61
|
+
multi_json (1.10.1)
|
62
|
+
nokogiri (1.6.4.1)
|
63
|
+
mini_portile (~> 0.6.0)
|
64
|
+
poltergeist (1.5.1)
|
65
|
+
capybara (~> 2.1)
|
66
|
+
cliver (~> 0.3.1)
|
67
|
+
multi_json (~> 1.0)
|
68
|
+
websocket-driver (>= 0.2.0)
|
69
|
+
rack (1.5.2)
|
70
|
+
rack-test (0.6.2)
|
71
|
+
rack (>= 1.0)
|
72
|
+
rails (4.1.8)
|
73
|
+
actionmailer (= 4.1.8)
|
74
|
+
actionpack (= 4.1.8)
|
75
|
+
actionview (= 4.1.8)
|
76
|
+
activemodel (= 4.1.8)
|
77
|
+
activerecord (= 4.1.8)
|
78
|
+
activesupport (= 4.1.8)
|
79
|
+
bundler (>= 1.3.0, < 2.0)
|
80
|
+
railties (= 4.1.8)
|
81
|
+
sprockets-rails (~> 2.0)
|
82
|
+
railties (4.1.8)
|
83
|
+
actionpack (= 4.1.8)
|
84
|
+
activesupport (= 4.1.8)
|
85
|
+
rake (>= 0.8.7)
|
86
|
+
thor (>= 0.18.1, < 2.0)
|
87
|
+
rake (10.3.2)
|
88
|
+
rest-client (1.6.7)
|
89
|
+
mime-types (>= 1.16)
|
90
|
+
simplecov (0.9.1)
|
91
|
+
docile (~> 1.1.0)
|
92
|
+
multi_json (~> 1.0)
|
93
|
+
simplecov-html (~> 0.8.0)
|
94
|
+
simplecov-html (0.8.0)
|
95
|
+
sprockets (2.12.3)
|
96
|
+
hike (~> 1.2)
|
97
|
+
multi_json (~> 1.0)
|
98
|
+
rack (~> 1.0)
|
99
|
+
tilt (~> 1.1, != 1.3.0)
|
100
|
+
sprockets-rails (2.2.0)
|
101
|
+
actionpack (>= 3.0)
|
102
|
+
activesupport (>= 3.0)
|
103
|
+
sprockets (>= 2.8, < 4.0)
|
104
|
+
sqlite3 (1.3.10)
|
105
|
+
teaspoon (0.8.0)
|
106
|
+
railties (>= 3.2.5, < 5)
|
107
|
+
term-ansicolor (1.2.2)
|
108
|
+
tins (~> 0.8)
|
109
|
+
thor (0.18.1)
|
110
|
+
thread_safe (0.3.4)
|
111
|
+
tilt (1.4.1)
|
112
|
+
tins (0.13.2)
|
113
|
+
tzinfo (1.2.2)
|
114
|
+
thread_safe (~> 0.1)
|
115
|
+
websocket-driver (0.4.0)
|
116
|
+
xpath (2.0.0)
|
117
|
+
nokogiri (~> 1.3)
|
118
|
+
|
119
|
+
PLATFORMS
|
120
|
+
ruby
|
121
|
+
|
122
|
+
DEPENDENCIES
|
123
|
+
coveralls (~> 0.7)
|
124
|
+
poltergeist (~> 1.5)
|
125
|
+
rails-js-routes!
|
126
|
+
sqlite3 (~> 1.3)
|
127
|
+
teaspoon (~> 0.8)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Finn-Lennart Heemeyer
|
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.md
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
rails-js-routes
|
2
|
+
========
|
3
|
+
[](https://travis-ci.org/tonekk/rails-js-routes)
|
4
|
+
[](https://gemnasium.com/tonekk/rails-js-routes)
|
5
|
+
[](https://rubygems.org/gems/rails-js-routes)
|
6
|
+
[](http://tonekk.mit-license.org)
|
7
|
+
|
8
|
+
|
9
|
+
[Check out the blog post!](http://finn.heemeyer.net/2014/10/18/rails-js/)
|
10
|
+
|
11
|
+
NOTICE: We decided to rename this gem from ``rails-js`` to ``rails-js-routes`` as it was nearly impossible to find.
|
12
|
+
The name may be somewhat long, but for us it describes the functionality appropriate.
|
13
|
+
|
14
|
+
|
15
|
+
I worked on several Rails projects and there were many cases when it felt overkill to use a framework like ``Angular`` or ``Ember``, because javascript was only needed in some parts of the application.
|
16
|
+
But by not using these frameworks I had to think of a way to *structure* the code by myself.
|
17
|
+
I came up with the conclusion to organize the javascript code like the ruby code is organized, that means: __Split up in controllers, which are split up in actions.__
|
18
|
+
|
19
|
+
## How it works
|
20
|
+
* In ``rails-js-routes`` we are able to define ``controllers``, as well as ``helpers``.
|
21
|
+
* Controllers have ``actions`` which are executed when the matching rails action is executed.
|
22
|
+
* Helpers should be clear. Pretty much like the rails helpers. There are helper per controller and global helpers.
|
23
|
+
* The gem also provides a ``view helper`` to use in your layouts, which passes the routing information to ``rails-js-routes``.
|
24
|
+
|
25
|
+
#### You start by creating an app.
|
26
|
+
|
27
|
+
```js
|
28
|
+
//= require routes
|
29
|
+
|
30
|
+
/*
|
31
|
+
* app/assets/javascripts/application.js
|
32
|
+
*/
|
33
|
+
|
34
|
+
Rails.app({
|
35
|
+
// Define global helpers here
|
36
|
+
helper: function() {
|
37
|
+
}
|
38
|
+
},
|
39
|
+
// Put in initial data
|
40
|
+
{
|
41
|
+
foo: 'bar'
|
42
|
+
},
|
43
|
+
// A function that is executed before every action
|
44
|
+
function() {
|
45
|
+
console.log('beforeAll() executed');
|
46
|
+
});
|
47
|
+
|
48
|
+
```
|
49
|
+
#### Create controllers accordingly.
|
50
|
+
|
51
|
+
```js
|
52
|
+
|
53
|
+
/*
|
54
|
+
* app/assets/javascripts/controllers/users.js
|
55
|
+
*/
|
56
|
+
|
57
|
+
Rails.controller('users', {
|
58
|
+
// actions here
|
59
|
+
new: function(h) {
|
60
|
+
// execute helper
|
61
|
+
h.awesomeHelper();
|
62
|
+
},
|
63
|
+
|
64
|
+
index: function(h) {
|
65
|
+
// Do the same as in new()
|
66
|
+
this.index(h);
|
67
|
+
}
|
68
|
+
},
|
69
|
+
{
|
70
|
+
// helpers here
|
71
|
+
awesomeHelper: function() {
|
72
|
+
}
|
73
|
+
});
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
|
78
|
+
#### Use code from other controllers.
|
79
|
+
```js
|
80
|
+
|
81
|
+
/*
|
82
|
+
* app/assets/javascripts/controllers/admin/users.js
|
83
|
+
*/
|
84
|
+
|
85
|
+
Rails.controller('admin/users', {
|
86
|
+
new: function(h) {
|
87
|
+
// Execute action of foreign controller
|
88
|
+
R.action('users#new');
|
89
|
+
},
|
90
|
+
|
91
|
+
index: function(h) {
|
92
|
+
// Execute foreign helper with arguments
|
93
|
+
R.helper('users', 'awesomeHelper', 1, 2, 3);
|
94
|
+
|
95
|
+
// Execute global helper with arguments
|
96
|
+
R.global('helper', 1, 2, 3);
|
97
|
+
}
|
98
|
+
},{});
|
99
|
+
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
#### Global variables without the mess.
|
104
|
+
```js
|
105
|
+
/*
|
106
|
+
* Use R singleton everywhere
|
107
|
+
*/
|
108
|
+
|
109
|
+
// Setting
|
110
|
+
R('answer', 42);
|
111
|
+
// Multiple levels
|
112
|
+
R('a.global.variable', 'foo');
|
113
|
+
|
114
|
+
// Getting
|
115
|
+
console.log(R('a.global.variable'));
|
116
|
+
```
|
117
|
+
|
118
|
+
|
119
|
+
#### Define hooks for namespaces.
|
120
|
+
```js
|
121
|
+
/*
|
122
|
+
* app/assets/javascripts/namespaces/admin.js
|
123
|
+
*/
|
124
|
+
|
125
|
+
Rails.namespace('admin', function() {
|
126
|
+
// Will be executed on all admin routes (e.g. admin/users#new)
|
127
|
+
console.log('Hello Mr. Admin');
|
128
|
+
});
|
129
|
+
```
|
130
|
+
|
131
|
+
#### For more info...
|
132
|
+
...just read in ``app/assets/javascripts/routes.js``. I tried my best and documented nearly every line of code :)
|
133
|
+
|
134
|
+
Installing
|
135
|
+
==========
|
136
|
+
|
137
|
+
Install it as any other gem by putting this to your ``Gemfile``:
|
138
|
+
```ruby
|
139
|
+
gem 'rails-js-routes'
|
140
|
+
```
|
141
|
+
In your layouts, put this line into the ``head``:
|
142
|
+
```erb
|
143
|
+
<%= js_routes %>
|
144
|
+
```
|
145
|
+
Also make sure to include ``routes`` in your ``application.js``. If you are using [Turbolinks](https://github.com/rails/turbolinks), make sure to include ``routes`` after ``turbolinks``.
|
146
|
+
|
147
|
+
Testing
|
148
|
+
=======
|
149
|
+
|
150
|
+
Javascript tests are in ``test/dummy/spec/javascripts/rails-js-routes_spec.js``.
|
151
|
+
They are executed using ``rake teaspoon`` (by using the [teaspoon runner](https://github.com/modeset/teaspoon)).
|
152
|
+
I'm still trying to get [travis to execute the teaspoon tests properly...](https://travis-ci.org/tonekk/rails-js-routes/builds/37740824)
|
153
|
+
|
154
|
+
Contributing
|
155
|
+
============
|
156
|
+
|
157
|
+
[Fork](https://github.com/tonekk/rails-js-routes/fork) -> Commit -> Pull Request
|
158
|
+
|
159
|
+
This project uses the [Airbnb Styleguide](https://github.com/airbnb/javascript) as our coding guideline.
|
160
|
+
We think this is a good piece of work and worth spreading.
|
161
|
+
In case of any doubts refer to the style used around.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.libs << 'test'
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = false
|
16
|
+
end
|
17
|
+
|
18
|
+
# teaspoon must be executed inside the dummy rails app
|
19
|
+
task :teaspoon do
|
20
|
+
Dir.chdir('test/dummy/')
|
21
|
+
sh 'bundle exec rake teaspoon'
|
22
|
+
end
|
23
|
+
|
24
|
+
# add teaspoon to default tasks
|
25
|
+
task default: :test
|
@@ -0,0 +1,327 @@
|
|
1
|
+
(function(window, document) {
|
2
|
+
|
3
|
+
/*
|
4
|
+
* All the global stuff (helpers, data, hooks) is stored
|
5
|
+
* locally in this closure so we cannot do anything wrong
|
6
|
+
*/
|
7
|
+
var controllers = {},
|
8
|
+
globalHelpers = {},
|
9
|
+
namespaceHooks = {},
|
10
|
+
data = {},
|
11
|
+
beforeAll = null;
|
12
|
+
|
13
|
+
/*
|
14
|
+
* Singleton that stores functions to create your apps' structure
|
15
|
+
*/
|
16
|
+
Rails = window.Rails || {};
|
17
|
+
|
18
|
+
/*
|
19
|
+
* For testing. We want to start each test with emtpiness.
|
20
|
+
*/
|
21
|
+
Rails.clear = function() {
|
22
|
+
controllers = {};
|
23
|
+
globalHelpers = {};
|
24
|
+
namespaceHooks = {};
|
25
|
+
data = {};
|
26
|
+
beforeAll = null;
|
27
|
+
};
|
28
|
+
|
29
|
+
/*
|
30
|
+
* Creates a Rails application (the javascript part!)
|
31
|
+
*
|
32
|
+
* Pass global helper and initial data as params
|
33
|
+
*/
|
34
|
+
Rails.app = function(helpers, initialData, beforeHook) {
|
35
|
+
|
36
|
+
/*
|
37
|
+
* Store everything in our local variables
|
38
|
+
*/
|
39
|
+
globalHelpers = helpers;
|
40
|
+
data = initialData;
|
41
|
+
beforeAll = beforeHook;
|
42
|
+
|
43
|
+
/*
|
44
|
+
* Set event listener to execute controllers' javascript
|
45
|
+
*
|
46
|
+
* There might be cases where we do not want to execute
|
47
|
+
* DOM-related js (e.g. javascript unit-tests).
|
48
|
+
* Set Rails.config.test to disable action execution.
|
49
|
+
*/
|
50
|
+
listener = function(event) {
|
51
|
+
|
52
|
+
if (Rails.config && Rails.config.test) {
|
53
|
+
return;
|
54
|
+
}
|
55
|
+
|
56
|
+
Rails.execute();
|
57
|
+
};
|
58
|
+
|
59
|
+
document.addEventListener("DOMContentLoaded", listener);
|
60
|
+
|
61
|
+
/*
|
62
|
+
* Add support to Turbolinks page changes
|
63
|
+
*/
|
64
|
+
if (typeof Turbolinks !== 'undefined') {
|
65
|
+
document.addEventListener('page:load', listener);
|
66
|
+
}
|
67
|
+
};
|
68
|
+
|
69
|
+
/*
|
70
|
+
* Adds a controller.
|
71
|
+
* Controllers are used to apply javascript in rails routes.
|
72
|
+
* Example:
|
73
|
+
*
|
74
|
+
* Rails.controller('users', {
|
75
|
+
*
|
76
|
+
* new: function() {
|
77
|
+
* // Executed on users/new
|
78
|
+
* }
|
79
|
+
* });
|
80
|
+
*/
|
81
|
+
Rails.controller = function(name, actions, helpers) {
|
82
|
+
|
83
|
+
/*
|
84
|
+
* Should not be able to define controller (and helpers) twice
|
85
|
+
*/
|
86
|
+
if (controllers[name]) {
|
87
|
+
throw ['Controller \'', name,
|
88
|
+
'\' is already defined! Please use a namespace'].join('');
|
89
|
+
}
|
90
|
+
|
91
|
+
/*
|
92
|
+
* Store controller & helpers in our local closure
|
93
|
+
*/
|
94
|
+
controllers[name] = actions;
|
95
|
+
controllers[name].helpers = helpers;
|
96
|
+
},
|
97
|
+
|
98
|
+
/*
|
99
|
+
* Execute the actual controller action.
|
100
|
+
* e.g. will execute controller.users.index()
|
101
|
+
* when on #users/index
|
102
|
+
*/
|
103
|
+
Rails.execute = function() {
|
104
|
+
|
105
|
+
/*
|
106
|
+
* Make sure rails.js has been properly added to the layout
|
107
|
+
*/
|
108
|
+
if (!(this.config && this.config.controller && this.config.action)) {
|
109
|
+
|
110
|
+
throw ['No action or controller given.',
|
111
|
+
'It seems like you forgot to add rails.js to your layouts\' header.'].join(' ');
|
112
|
+
}
|
113
|
+
|
114
|
+
var controllerName = this.config.controller,
|
115
|
+
namespaceHook,
|
116
|
+
controller;
|
117
|
+
|
118
|
+
/*
|
119
|
+
* Execute beforeAll() hook if defined
|
120
|
+
*/
|
121
|
+
if (beforeAll && typeof(beforeAll) == 'function') {
|
122
|
+
beforeAll();
|
123
|
+
}
|
124
|
+
|
125
|
+
/*
|
126
|
+
* Take care of namespace (e.g. admin/users)
|
127
|
+
*/
|
128
|
+
if (this.config.namespace) {
|
129
|
+
controllerName = this.config.namespace + '/' + controllerName;
|
130
|
+
|
131
|
+
/*
|
132
|
+
* Execute namespace hook if we defined one
|
133
|
+
*/
|
134
|
+
namespaceHook = namespaceHooks[this.config.namespace];
|
135
|
+
if (namespaceHook && typeof(namespaceHook) == 'function') {
|
136
|
+
namespaceHook();
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
/*
|
141
|
+
* Find controller and execute method
|
142
|
+
*/
|
143
|
+
var controller = controllers[controllerName];
|
144
|
+
if (controller && controller[this.config.action] &&
|
145
|
+
typeof(controller[this.config.action]) == 'function') {
|
146
|
+
|
147
|
+
controller[this.config.action](controller.helpers);
|
148
|
+
}
|
149
|
+
};
|
150
|
+
|
151
|
+
/*
|
152
|
+
* Defines a namespace hook which will be executed when
|
153
|
+
* we are in the namespace of that hook.
|
154
|
+
*
|
155
|
+
* For example:
|
156
|
+
* When we define a namespace for 'admin'
|
157
|
+
* the function will be executed on all controllers in that namespace.
|
158
|
+
* (e.g. Admin::UsersController, Admin::SettingsController, etc...)
|
159
|
+
*/
|
160
|
+
Rails.namespace = function(name, block) {
|
161
|
+
|
162
|
+
/*
|
163
|
+
* Should not be able to define controller (and helpers) twice
|
164
|
+
*/
|
165
|
+
if (namespaceHooks[name]) {
|
166
|
+
throw ['Namespace \'', name, '\' is already defined!'].join('');
|
167
|
+
}
|
168
|
+
|
169
|
+
namespaceHooks[name] = block;
|
170
|
+
};
|
171
|
+
|
172
|
+
|
173
|
+
/*
|
174
|
+
* Global Singleton to store variables and functions
|
175
|
+
* that you need all over your javascript codebase.
|
176
|
+
*
|
177
|
+
* Also contains global helpers, so to prevent conflicts
|
178
|
+
* we save global variables by using this singleton as a function.
|
179
|
+
*
|
180
|
+
* R('foo'); // Gets window.R.data.foo
|
181
|
+
* R('foo', 'bar'); // Sets window.R.data.foo
|
182
|
+
*/
|
183
|
+
R = function(key, val) {
|
184
|
+
|
185
|
+
/*
|
186
|
+
* It's possible to go multiple layers with one call, e.g.
|
187
|
+
* R('foo.bar.baz');
|
188
|
+
*
|
189
|
+
* Or you can set things at paths which are not there:
|
190
|
+
* // foo and bar will be created as empty objects
|
191
|
+
* R('foo.bar.baz', 'foo');
|
192
|
+
*
|
193
|
+
*/
|
194
|
+
var steps = key.split('.'),
|
195
|
+
step = data,
|
196
|
+
i;
|
197
|
+
|
198
|
+
if (arguments.length == 2) {
|
199
|
+
for (i = 0; i < steps.length-1; i++) {
|
200
|
+
if (!step[steps[i]]) {
|
201
|
+
step[steps[i]] = {};
|
202
|
+
}
|
203
|
+
|
204
|
+
step = step[steps[i]];
|
205
|
+
}
|
206
|
+
|
207
|
+
step[steps[steps.length -1]] = val;
|
208
|
+
|
209
|
+
} else {
|
210
|
+
for (i = 0; i < steps.length; i++) {
|
211
|
+
step = step[steps[i]];
|
212
|
+
if (!step) {
|
213
|
+
break;
|
214
|
+
}
|
215
|
+
|
216
|
+
}
|
217
|
+
return step;
|
218
|
+
}
|
219
|
+
};
|
220
|
+
|
221
|
+
/*
|
222
|
+
* Execute a helper of a foreign controller
|
223
|
+
*
|
224
|
+
* e.g.
|
225
|
+
* Rails.helper('admin/users', 'method', [arg1, arg2, arg3]);
|
226
|
+
* or
|
227
|
+
* Rails.helper('entities', 'method', arg1, arg2, arg3);
|
228
|
+
*/
|
229
|
+
R.helper = function(controllerName, helperName) {
|
230
|
+
|
231
|
+
var controller,
|
232
|
+
helper,
|
233
|
+
context,
|
234
|
+
args = Array.prototype.slice.call(arguments, 2);
|
235
|
+
|
236
|
+
/*
|
237
|
+
* Treat 3rd argument as arguments for helper if it is an Array
|
238
|
+
*/
|
239
|
+
if (args.length == 1 && args[0] instanceof Array) {
|
240
|
+
args = args[0];
|
241
|
+
}
|
242
|
+
|
243
|
+
/*
|
244
|
+
* Choose global helper if controllerName is not given
|
245
|
+
*/
|
246
|
+
if (controllerName) {
|
247
|
+
controller = controllers[controllerName];
|
248
|
+
helper = controller.helpers[helperName];
|
249
|
+
} else {
|
250
|
+
helper = globalHelpers[helperName];
|
251
|
+
}
|
252
|
+
|
253
|
+
/*
|
254
|
+
* Make sure our helper exists
|
255
|
+
*/
|
256
|
+
if (!(helper && typeof(helper) == 'function')) {
|
257
|
+
if (controllerName) {
|
258
|
+
throw ['Helper \'', helperName, '\' not defined for controller \'',
|
259
|
+
controllerName, '\''].join('');
|
260
|
+
} else {
|
261
|
+
throw ['Global helper \'', helperName, '\' not defined'].join('');
|
262
|
+
}
|
263
|
+
}
|
264
|
+
|
265
|
+
/*
|
266
|
+
* Choose context, either some controllers' helpers or
|
267
|
+
* the globalHelpers object
|
268
|
+
*/
|
269
|
+
context = controller ? controller.helpers : globalHelpers;
|
270
|
+
|
271
|
+
/*
|
272
|
+
* Execute helper in context with arguments
|
273
|
+
*/
|
274
|
+
return helper.apply(context, args);
|
275
|
+
};
|
276
|
+
|
277
|
+
/*
|
278
|
+
* Execute a global helper
|
279
|
+
*
|
280
|
+
* Global helpers are defined when the Rails.app is created
|
281
|
+
*/
|
282
|
+
R.global = function(helperName) {
|
283
|
+
return this.helper(undefined, helperName, Array.prototype.slice.call(arguments, 1));
|
284
|
+
};
|
285
|
+
|
286
|
+
/*
|
287
|
+
* Executes a controller action
|
288
|
+
* (to execute the same javascript in a different controller)
|
289
|
+
*/
|
290
|
+
R.action = function(action) {
|
291
|
+
|
292
|
+
/*
|
293
|
+
* Arguments are either controller and action or the
|
294
|
+
* familliar rails syntax:
|
295
|
+
*
|
296
|
+
* R.action('users', 'index');
|
297
|
+
* R.action('users#index');
|
298
|
+
*/
|
299
|
+
var controller,
|
300
|
+
controllerName = arguments[0],
|
301
|
+
actionName = arguments[1];
|
302
|
+
|
303
|
+
if (action.indexOf('#') !== -1) {
|
304
|
+
controllerName = action.split('#')[0];
|
305
|
+
actionName = action.split('#')[1];
|
306
|
+
}
|
307
|
+
|
308
|
+
/*
|
309
|
+
* Make sure controller and action exist
|
310
|
+
*/
|
311
|
+
controller = controllers[controllerName];
|
312
|
+
|
313
|
+
if (!controller) {
|
314
|
+
throw ['Attempting to call action \'', controllerName, '#', actionName,
|
315
|
+
'\', but Controller \'', controllerName, '\' is not defined!'].join('');
|
316
|
+
} else if (!controller[actionName] || !(typeof(controller[actionName]) == 'function')) {
|
317
|
+
throw ['Attempting to call action \'', controllerName, '#', actionName,
|
318
|
+
'\', but Action \'', actionName, '\' is not defined!'].join('');
|
319
|
+
}
|
320
|
+
|
321
|
+
/*
|
322
|
+
* Execute controller action with helpers as argument
|
323
|
+
*/
|
324
|
+
controller[actionName](controller.helper);
|
325
|
+
};
|
326
|
+
|
327
|
+
})(window, window.document);
|