execjs-rails 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -0
- data/lib/assets/javascripts/server.js +15 -0
- data/lib/execjs/rails/concern.rb +17 -0
- data/lib/execjs/rails/engine.rb +44 -0
- data/lib/execjs/rails/renderer.rb +39 -0
- data/lib/execjs/rails/template_resolver.rb +20 -0
- data/lib/execjs/rails/version.rb +5 -0
- data/lib/execjs/rails/view_helper.rb +15 -0
- data/lib/execjs/rails.rb +5 -0
- data/lib/execjs-rails.rb +1 -0
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20b99d427d6dd48dcb8e08bd8e9fbbd055adb87c
|
4
|
+
data.tar.gz: a207fbce620184b1280d37530ace7ead33361a1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 218addd8d037f808de2dfd41ac491dbe6f40541b65dc52fb797760c20d7f77a312c582421f99b758d5439d16949fda86a8bc330f0b4dc13f2f919920532999c6
|
7
|
+
data.tar.gz: 77ad4ced87fb69cebc8842ed06846fc131ed9b3f314f869076e1a2b2b31c8dbe182874823a88c38d32ea8beb7c6f7aabf66fa9354f9e0caec0eafa4a4ed6c933
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
execjs-rails
|
2
|
+
============
|
3
|
+
|
4
|
+
# JavaScript-based rendering in Rails
|
5
|
+
|
6
|
+
## WARNING: Early release. There will be many changes
|
7
|
+
|
8
|
+
## How to use it:
|
9
|
+
|
10
|
+
Include `execjs-rails` in your Gemfile.
|
11
|
+
|
12
|
+
Create a file named `app/assets/javascripts/server.js` which contains the following:
|
13
|
+
```javascript
|
14
|
+
function execjs_rails_handler(path, opts) {
|
15
|
+
// `path` looks like "users/show"
|
16
|
+
// `opts` is a data object passed in from Rails
|
17
|
+
}
|
18
|
+
```
|
19
|
+
|
20
|
+
Combine this approach with some fancy JavaScript UI frameworks and some smarty pants
|
21
|
+
asset bundles, and you'll have shared client and server views for extra DRYness
|
22
|
+
without sacrificing SEO.
|
23
|
+
|
24
|
+
Read the source for configuration options and more info.
|
@@ -0,0 +1,15 @@
|
|
1
|
+
function execjs_rails_handler(path, opts) {
|
2
|
+
var json = JSON.stringify(opts, null, " ");
|
3
|
+
var code = "execjs_rails_handler('"+path+"', "+json+");"
|
4
|
+
return "" +
|
5
|
+
"<h1>Hello, I'm execjs-rails!</h1>" +
|
6
|
+
"<h2>There are many ways to configure me.</h2>" +
|
7
|
+
"<p>Provide your own <code>server.js</code>.</p>" +
|
8
|
+
"<p>Or create an initializer to set<br><code>config.execjs_rails.server_javascripts = ['my_javascript.js']</code>.</p>" +
|
9
|
+
"<p>By default, the template handler and helper call the JavaScript function <code>execjs_rails_handler</code>.</p>" +
|
10
|
+
"<p>You can override the function name with<br><code>config.execjs_rails.handler_function_name = 'my_handler'</code></p>" +
|
11
|
+
"<p>This is the exact JavaScript code being called to generate this page:</p>" +
|
12
|
+
"<p><pre><code>"+code+"</code></pre></p>" +
|
13
|
+
"<p>For more configuration options, see <code>execjs-rails/lib/execjs/rails/engine.rb</code></p>" +
|
14
|
+
"";
|
15
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ExecJS
|
2
|
+
module Rails
|
3
|
+
module Concern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
|
7
|
+
append_view_path TemplateResolver.new
|
8
|
+
append_before_filter :reset_execjs_renderer
|
9
|
+
|
10
|
+
def reset_execjs_renderer
|
11
|
+
Renderer.reset!
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ExecJS
|
2
|
+
module Rails
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
|
5
|
+
config.execjs_rails = ActiveSupport::OrderedOptions.new
|
6
|
+
config.execjs_rails.memoize_context = ::Rails.env.production?
|
7
|
+
config.execjs_rails.on_error = lambda { |error| raise error }
|
8
|
+
config.execjs_rails.server_javascripts = ['server.js']
|
9
|
+
config.execjs_rails.handler_function_name = 'execjs_rails_handler'
|
10
|
+
|
11
|
+
config.execjs_rails.view_to_function_options = lambda do |view, options|
|
12
|
+
view.assigns.stringify_keys.merge!({
|
13
|
+
controller: view.controller_name,
|
14
|
+
action: view.action_name,
|
15
|
+
csrf_token: view.form_authenticity_token || nil,
|
16
|
+
current_user: view.current_user || nil
|
17
|
+
}).merge!(options.stringify_keys).as_json
|
18
|
+
end
|
19
|
+
|
20
|
+
initializer "execjs_rails.setup_view_helpers" do
|
21
|
+
ActiveSupport.on_load(:action_view) do
|
22
|
+
include ViewHelper
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
initializer "execjs_rails.setup_controller_concern" do
|
27
|
+
ActiveSupport.on_load(:action_controller) do
|
28
|
+
include Concern
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
config.after_initialize do |app|
|
33
|
+
app.config.execjs_rails.build_source ||= lambda do
|
34
|
+
app.config.execjs_rails.server_javascripts.map do |filename|
|
35
|
+
app.assets.resolve(filename)
|
36
|
+
app.assets[filename].to_s
|
37
|
+
end.join(";")
|
38
|
+
end
|
39
|
+
Renderer.setup!(app.config.execjs_rails)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ExecJS
|
2
|
+
module Rails
|
3
|
+
module Renderer
|
4
|
+
|
5
|
+
@@config = nil
|
6
|
+
@@context = nil
|
7
|
+
|
8
|
+
def self.setup!(config = {})
|
9
|
+
@@config = config
|
10
|
+
self.reset!
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.config
|
14
|
+
@@config
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.reset!
|
18
|
+
unless @@config.memoize_context? && !@@context.nil?
|
19
|
+
begin
|
20
|
+
@@context = ExecJS.compile(@@config.build_source.call())
|
21
|
+
rescue V8::Error => error
|
22
|
+
@@config.on_error.call(error)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.call(function, *args)
|
28
|
+
@@context.call(function, *args)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.render(path, opts = {})
|
32
|
+
output = self.call(@@config.handler_function_name, path, opts)
|
33
|
+
fail "ExecJS call to render function `#{function}` returned null" if output.nil?
|
34
|
+
output.html_safe
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ExecJS
|
2
|
+
module Rails
|
3
|
+
class TemplateResolver < ::ActionView::Resolver
|
4
|
+
|
5
|
+
def find_templates(name, prefix, partial, details)
|
6
|
+
path = "#{prefix}/#{name}"
|
7
|
+
[::ActionView::Template.new(
|
8
|
+
"<%= execjs_render('#{path}') %>",
|
9
|
+
"ExecJS - #{path}",
|
10
|
+
ActionView::Template.registered_template_handler(:erb),
|
11
|
+
{
|
12
|
+
format: Mime[:erb],
|
13
|
+
virtual_path: path
|
14
|
+
}
|
15
|
+
)]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ExecJS
|
2
|
+
module Rails
|
3
|
+
module ViewHelper
|
4
|
+
|
5
|
+
def execjs_call(function, *args)
|
6
|
+
Renderer.call(function, *args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def execjs_render(path, opts = {})
|
10
|
+
Renderer.render(path, Renderer.config.view_to_function_options.call(self, opts))
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/execjs/rails.rb
ADDED
data/lib/execjs-rails.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'execjs/rails'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: execjs-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Revelry Labs, LLC
|
@@ -44,7 +44,17 @@ email:
|
|
44
44
|
executables: []
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
|
-
files:
|
47
|
+
files:
|
48
|
+
- lib/assets/javascripts/server.js
|
49
|
+
- lib/execjs/rails/concern.rb
|
50
|
+
- lib/execjs/rails/engine.rb
|
51
|
+
- lib/execjs/rails/renderer.rb
|
52
|
+
- lib/execjs/rails/template_resolver.rb
|
53
|
+
- lib/execjs/rails/version.rb
|
54
|
+
- lib/execjs/rails/view_helper.rb
|
55
|
+
- lib/execjs/rails.rb
|
56
|
+
- lib/execjs-rails.rb
|
57
|
+
- README.md
|
48
58
|
homepage: https://github.com/revelrylabs/execjs-rails
|
49
59
|
licenses:
|
50
60
|
- MIT
|