render_execjs 0.0.1 → 0.0.2
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.
- data/.gitignore +2 -1
- data/Appraisals +7 -0
- data/README.md +59 -0
- data/Rakefile +8 -0
- data/gemfiles/rails31.gemfile +7 -0
- data/gemfiles/rails31.gemfile.lock +124 -0
- data/gemfiles/rails32.gemfile +7 -0
- data/gemfiles/rails32.gemfile.lock +122 -0
- data/lib/generators/render_execjs/install_generator.rb +12 -0
- data/lib/generators/templates/render_execjs.rb +22 -0
- data/lib/render_execjs/configuration.rb +40 -0
- data/lib/render_execjs/railtie.rb +15 -0
- data/lib/render_execjs/renderer.rb +40 -0
- data/lib/render_execjs/version.rb +1 -1
- data/lib/render_execjs.rb +9 -2
- data/render_execjs.gemspec +5 -2
- data/spec/configuration_spec.rb +94 -0
- data/spec/helper.rb +29 -0
- data/spec/install_spec.rb +19 -0
- data/spec/renderer_spec.rb +79 -0
- data/spec/support/helpers.rb +82 -0
- metadata +104 -52
data/.gitignore
CHANGED
data/Appraisals
ADDED
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# render_execjs #
|
2
|
+
|
3
|
+
Render executable JavaScript within Rails.
|
4
|
+
|
5
|
+
## Installation ##
|
6
|
+
|
7
|
+
Add `render_execjs` to your `Gemfile`
|
8
|
+
|
9
|
+
gem 'render_execjs', '~> 0.0.1'
|
10
|
+
# gem 'coffee-script' # Uncomment this line if you'd like to render executable CoffeeScript
|
11
|
+
|
12
|
+
Run the `render_execjs:install` generator to create the optional configuration initializer file
|
13
|
+
|
14
|
+
rails generate render_execjs:install
|
15
|
+
|
16
|
+
## Usage ##
|
17
|
+
|
18
|
+
In your controller:
|
19
|
+
|
20
|
+
render execjs: "return 'some JavaScript';"
|
21
|
+
|
22
|
+
It also supports CoffeeScript!
|
23
|
+
|
24
|
+
render execcs: "return 'some CoffeeScript'"
|
25
|
+
|
26
|
+
That's it!
|
27
|
+
|
28
|
+
Note that it will render whatever is returned from JavaScript.
|
29
|
+
|
30
|
+
## Why would I want to render JavaScript from Rails? ##
|
31
|
+
|
32
|
+
Make sure that you're logged into your Twitter, now click on this link: [http://twitter.com/tazsingh](http://twitter.com/tazsingh)
|
33
|
+
Notice how it loads your Twitter first, then the page that you've requested?
|
34
|
+
|
35
|
+
It has to initialize the JavaScript environment before it can render the page you've requested.
|
36
|
+
A solution to this, in Rails, is to use something like Mustache to render the same structure but with different logic;
|
37
|
+
thus giving the browser the page you've requested as the base to initialize from.
|
38
|
+
However this means duplication of logic.
|
39
|
+
|
40
|
+
Wouldn't it be nice to write logic and structure once, using a pretty templating system, that works on both client and server?
|
41
|
+
|
42
|
+
This is the problem that `render_execjs` is attempting to solve. Note that this is an experiment and suggestions are welcome!
|
43
|
+
|
44
|
+
## Testing ##
|
45
|
+
|
46
|
+
`render_execjs` currently uses [Appraisal](https://github.com/thoughtbot/appraisal) to test against multiple versions of Rails.
|
47
|
+
Install it with: `rake appraisal:install`
|
48
|
+
|
49
|
+
Now you can run:
|
50
|
+
|
51
|
+
rake appraisal spec # To run specs against both Rails 3.1 and 3.2
|
52
|
+
rake appraisal:rails31 spec # To test against Rails 3.1
|
53
|
+
rake appraisal:rails32 spec # To test against Rails 3.2
|
54
|
+
|
55
|
+
Note that the testing system is currently undergoing an overhaul, so the above will change.
|
56
|
+
|
57
|
+
## License ##
|
58
|
+
|
59
|
+
MIT
|
data/Rakefile
CHANGED
@@ -0,0 +1,124 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/tsingh/RubymineProjects/render_execjs
|
3
|
+
specs:
|
4
|
+
render_execjs (0.0.1)
|
5
|
+
execjs
|
6
|
+
rails (>= 3.1.3)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionmailer (3.1.3)
|
12
|
+
actionpack (= 3.1.3)
|
13
|
+
mail (~> 2.3.0)
|
14
|
+
actionpack (3.1.3)
|
15
|
+
activemodel (= 3.1.3)
|
16
|
+
activesupport (= 3.1.3)
|
17
|
+
builder (~> 3.0.0)
|
18
|
+
erubis (~> 2.7.0)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
rack (~> 1.3.5)
|
21
|
+
rack-cache (~> 1.1)
|
22
|
+
rack-mount (~> 0.8.2)
|
23
|
+
rack-test (~> 0.6.1)
|
24
|
+
sprockets (~> 2.0.3)
|
25
|
+
activemodel (3.1.3)
|
26
|
+
activesupport (= 3.1.3)
|
27
|
+
builder (~> 3.0.0)
|
28
|
+
i18n (~> 0.6)
|
29
|
+
activerecord (3.1.3)
|
30
|
+
activemodel (= 3.1.3)
|
31
|
+
activesupport (= 3.1.3)
|
32
|
+
arel (~> 2.2.1)
|
33
|
+
tzinfo (~> 0.3.29)
|
34
|
+
activeresource (3.1.3)
|
35
|
+
activemodel (= 3.1.3)
|
36
|
+
activesupport (= 3.1.3)
|
37
|
+
activesupport (3.1.3)
|
38
|
+
multi_json (~> 1.0)
|
39
|
+
appraisal (0.4.0)
|
40
|
+
bundler
|
41
|
+
rake
|
42
|
+
arel (2.2.1)
|
43
|
+
builder (3.0.0)
|
44
|
+
coffee-script (2.2.0)
|
45
|
+
coffee-script-source
|
46
|
+
execjs
|
47
|
+
coffee-script-source (1.2.0)
|
48
|
+
diff-lcs (1.1.3)
|
49
|
+
erubis (2.7.0)
|
50
|
+
execjs (1.3.0)
|
51
|
+
multi_json (~> 1.0)
|
52
|
+
hike (1.2.1)
|
53
|
+
i18n (0.6.0)
|
54
|
+
json (1.6.5)
|
55
|
+
json (1.6.5-java)
|
56
|
+
mail (2.3.0)
|
57
|
+
i18n (>= 0.4.0)
|
58
|
+
mime-types (~> 1.16)
|
59
|
+
treetop (~> 1.4.8)
|
60
|
+
mime-types (1.17.2)
|
61
|
+
multi_json (1.0.4)
|
62
|
+
polyglot (0.3.3)
|
63
|
+
rack (1.3.6)
|
64
|
+
rack-cache (1.1)
|
65
|
+
rack (>= 0.4)
|
66
|
+
rack-mount (0.8.3)
|
67
|
+
rack (>= 1.0.0)
|
68
|
+
rack-ssl (1.3.2)
|
69
|
+
rack
|
70
|
+
rack-test (0.6.1)
|
71
|
+
rack (>= 1.0)
|
72
|
+
rails (3.1.3)
|
73
|
+
actionmailer (= 3.1.3)
|
74
|
+
actionpack (= 3.1.3)
|
75
|
+
activerecord (= 3.1.3)
|
76
|
+
activeresource (= 3.1.3)
|
77
|
+
activesupport (= 3.1.3)
|
78
|
+
bundler (~> 1.0)
|
79
|
+
railties (= 3.1.3)
|
80
|
+
railties (3.1.3)
|
81
|
+
actionpack (= 3.1.3)
|
82
|
+
activesupport (= 3.1.3)
|
83
|
+
rack-ssl (~> 1.3.2)
|
84
|
+
rake (>= 0.8.7)
|
85
|
+
rdoc (~> 3.4)
|
86
|
+
thor (~> 0.14.6)
|
87
|
+
rake (0.9.2.2)
|
88
|
+
rdoc (3.12)
|
89
|
+
json (~> 1.4)
|
90
|
+
rspec (2.8.0)
|
91
|
+
rspec-core (~> 2.8.0)
|
92
|
+
rspec-expectations (~> 2.8.0)
|
93
|
+
rspec-mocks (~> 2.8.0)
|
94
|
+
rspec-core (2.8.0)
|
95
|
+
rspec-expectations (2.8.0)
|
96
|
+
diff-lcs (~> 1.1.2)
|
97
|
+
rspec-mocks (2.8.0)
|
98
|
+
rspec-rails (2.8.1)
|
99
|
+
actionpack (>= 3.0)
|
100
|
+
activesupport (>= 3.0)
|
101
|
+
railties (>= 3.0)
|
102
|
+
rspec (~> 2.8.0)
|
103
|
+
sprockets (2.0.3)
|
104
|
+
hike (~> 1.2)
|
105
|
+
rack (~> 1.0)
|
106
|
+
tilt (~> 1.1, != 1.3.0)
|
107
|
+
thor (0.14.6)
|
108
|
+
tilt (1.3.3)
|
109
|
+
treetop (1.4.10)
|
110
|
+
polyglot
|
111
|
+
polyglot (>= 0.3.1)
|
112
|
+
tzinfo (0.3.31)
|
113
|
+
|
114
|
+
PLATFORMS
|
115
|
+
java
|
116
|
+
ruby
|
117
|
+
|
118
|
+
DEPENDENCIES
|
119
|
+
appraisal
|
120
|
+
coffee-script
|
121
|
+
rails (= 3.1.3)
|
122
|
+
render_execjs!
|
123
|
+
rspec
|
124
|
+
rspec-rails
|
@@ -0,0 +1,122 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/tsingh/RubymineProjects/render_execjs
|
3
|
+
specs:
|
4
|
+
render_execjs (0.0.1)
|
5
|
+
execjs
|
6
|
+
rails (>= 3.1.3)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionmailer (3.2.1)
|
12
|
+
actionpack (= 3.2.1)
|
13
|
+
mail (~> 2.4.0)
|
14
|
+
actionpack (3.2.1)
|
15
|
+
activemodel (= 3.2.1)
|
16
|
+
activesupport (= 3.2.1)
|
17
|
+
builder (~> 3.0.0)
|
18
|
+
erubis (~> 2.7.0)
|
19
|
+
journey (~> 1.0.1)
|
20
|
+
rack (~> 1.4.0)
|
21
|
+
rack-cache (~> 1.1)
|
22
|
+
rack-test (~> 0.6.1)
|
23
|
+
sprockets (~> 2.1.2)
|
24
|
+
activemodel (3.2.1)
|
25
|
+
activesupport (= 3.2.1)
|
26
|
+
builder (~> 3.0.0)
|
27
|
+
activerecord (3.2.1)
|
28
|
+
activemodel (= 3.2.1)
|
29
|
+
activesupport (= 3.2.1)
|
30
|
+
arel (~> 3.0.0)
|
31
|
+
tzinfo (~> 0.3.29)
|
32
|
+
activeresource (3.2.1)
|
33
|
+
activemodel (= 3.2.1)
|
34
|
+
activesupport (= 3.2.1)
|
35
|
+
activesupport (3.2.1)
|
36
|
+
i18n (~> 0.6)
|
37
|
+
multi_json (~> 1.0)
|
38
|
+
appraisal (0.4.0)
|
39
|
+
bundler
|
40
|
+
rake
|
41
|
+
arel (3.0.0)
|
42
|
+
builder (3.0.0)
|
43
|
+
coffee-script (2.2.0)
|
44
|
+
coffee-script-source
|
45
|
+
execjs
|
46
|
+
coffee-script-source (1.2.0)
|
47
|
+
diff-lcs (1.1.3)
|
48
|
+
erubis (2.7.0)
|
49
|
+
execjs (1.3.0)
|
50
|
+
multi_json (~> 1.0)
|
51
|
+
hike (1.2.1)
|
52
|
+
i18n (0.6.0)
|
53
|
+
journey (1.0.1)
|
54
|
+
json (1.6.5)
|
55
|
+
json (1.6.5-java)
|
56
|
+
mail (2.4.1)
|
57
|
+
i18n (>= 0.4.0)
|
58
|
+
mime-types (~> 1.16)
|
59
|
+
treetop (~> 1.4.8)
|
60
|
+
mime-types (1.17.2)
|
61
|
+
multi_json (1.0.4)
|
62
|
+
polyglot (0.3.3)
|
63
|
+
rack (1.4.1)
|
64
|
+
rack-cache (1.1)
|
65
|
+
rack (>= 0.4)
|
66
|
+
rack-ssl (1.3.2)
|
67
|
+
rack
|
68
|
+
rack-test (0.6.1)
|
69
|
+
rack (>= 1.0)
|
70
|
+
rails (3.2.1)
|
71
|
+
actionmailer (= 3.2.1)
|
72
|
+
actionpack (= 3.2.1)
|
73
|
+
activerecord (= 3.2.1)
|
74
|
+
activeresource (= 3.2.1)
|
75
|
+
activesupport (= 3.2.1)
|
76
|
+
bundler (~> 1.0)
|
77
|
+
railties (= 3.2.1)
|
78
|
+
railties (3.2.1)
|
79
|
+
actionpack (= 3.2.1)
|
80
|
+
activesupport (= 3.2.1)
|
81
|
+
rack-ssl (~> 1.3.2)
|
82
|
+
rake (>= 0.8.7)
|
83
|
+
rdoc (~> 3.4)
|
84
|
+
thor (~> 0.14.6)
|
85
|
+
rake (0.9.2.2)
|
86
|
+
rdoc (3.12)
|
87
|
+
json (~> 1.4)
|
88
|
+
rspec (2.8.0)
|
89
|
+
rspec-core (~> 2.8.0)
|
90
|
+
rspec-expectations (~> 2.8.0)
|
91
|
+
rspec-mocks (~> 2.8.0)
|
92
|
+
rspec-core (2.8.0)
|
93
|
+
rspec-expectations (2.8.0)
|
94
|
+
diff-lcs (~> 1.1.2)
|
95
|
+
rspec-mocks (2.8.0)
|
96
|
+
rspec-rails (2.8.1)
|
97
|
+
actionpack (>= 3.0)
|
98
|
+
activesupport (>= 3.0)
|
99
|
+
railties (>= 3.0)
|
100
|
+
rspec (~> 2.8.0)
|
101
|
+
sprockets (2.1.2)
|
102
|
+
hike (~> 1.2)
|
103
|
+
rack (~> 1.0)
|
104
|
+
tilt (~> 1.1, != 1.3.0)
|
105
|
+
thor (0.14.6)
|
106
|
+
tilt (1.3.3)
|
107
|
+
treetop (1.4.10)
|
108
|
+
polyglot
|
109
|
+
polyglot (>= 0.3.1)
|
110
|
+
tzinfo (0.3.31)
|
111
|
+
|
112
|
+
PLATFORMS
|
113
|
+
java
|
114
|
+
ruby
|
115
|
+
|
116
|
+
DEPENDENCIES
|
117
|
+
appraisal
|
118
|
+
coffee-script
|
119
|
+
rails (= 3.2.1)
|
120
|
+
render_execjs!
|
121
|
+
rspec
|
122
|
+
rspec-rails
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module RenderExecjs
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../../templates', __FILE__)
|
5
|
+
|
6
|
+
desc 'Installs RenderExecJS'
|
7
|
+
def copy_initializer
|
8
|
+
template "render_execjs.rb", "config/initializers/render_execjs.rb"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
RenderExecJS.configure do
|
2
|
+
# Add your RenderExecJS configuration here
|
3
|
+
|
4
|
+
# Execute JS within environment from Sprockets
|
5
|
+
# compile_with :sprockets_require_tag
|
6
|
+
|
7
|
+
# Execute JS within environment from File
|
8
|
+
# compile_with File.read("")
|
9
|
+
|
10
|
+
# Execute JS within environment from String
|
11
|
+
# compile_with <<-JS
|
12
|
+
# // do stuff
|
13
|
+
# JS
|
14
|
+
end
|
15
|
+
|
16
|
+
# Alternate setter syntax
|
17
|
+
# RenderExecJS.compile_with # value
|
18
|
+
# or
|
19
|
+
# RenderExecJS.compile_with = # value
|
20
|
+
|
21
|
+
# Alternate getter syntax
|
22
|
+
# RenderExecJS.compile_with # => value
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module RenderExecJS
|
2
|
+
module Configuration
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
add_config :environment, :with => "RenderExecJS::Renderer.environment"
|
7
|
+
add_setter :coffeescript_environment, :with => "RenderExecJS::Renderer.coffeescript_environment"
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def configure &block
|
12
|
+
instance_exec &block
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def add_config name, options = {}
|
17
|
+
options[:with] ||= "@@#{name}"
|
18
|
+
|
19
|
+
class_eval <<-RUBY
|
20
|
+
class << self
|
21
|
+
def #{name} new_#{name} #{'= nil' unless options[:setter]}
|
22
|
+
#{options[:with]} = new_#{name} #{"unless new_#{name}.nil?" unless options[:setter]}
|
23
|
+
#{options[:with] unless options[:setter]}
|
24
|
+
end
|
25
|
+
|
26
|
+
def #{name}= new_#{name}
|
27
|
+
#{options[:with]} = new_#{name}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBY
|
31
|
+
|
32
|
+
send "#{name}=", nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_setter name, options = {}
|
36
|
+
add_config name, {:setter => true}.merge(options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module RenderExecJS
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer "render_execjs.setup" do
|
4
|
+
ActionController::Renderers.add :execjs do |obj, options|
|
5
|
+
RenderExecJS::Renderer.exec obj
|
6
|
+
end
|
7
|
+
|
8
|
+
if defined? CoffeeScript
|
9
|
+
ActionController::Renderers.add :execcs do |obj, options|
|
10
|
+
RenderExecJS::Renderer.exec obj, :coffee_script => true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module RenderExecJS
|
2
|
+
class Renderer
|
3
|
+
class << self
|
4
|
+
def environment
|
5
|
+
@environment
|
6
|
+
end
|
7
|
+
|
8
|
+
def environment= new_environment
|
9
|
+
@context = nil
|
10
|
+
@environment = new_environment
|
11
|
+
end
|
12
|
+
|
13
|
+
def coffeescript_environment= new_environment
|
14
|
+
if defined? CoffeeScript
|
15
|
+
@context = nil
|
16
|
+
@environment = CoffeeScript.compile new_environment, :bare => true
|
17
|
+
elsif !new_environment.blank?
|
18
|
+
puts "Can't find CoffeeScript. Did you forget to add it to your Gemfile?"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def exec js, options = {}
|
23
|
+
if options[:coffee_script]
|
24
|
+
js = CoffeeScript.compile js, :bare => true
|
25
|
+
end
|
26
|
+
|
27
|
+
context.exec js
|
28
|
+
end
|
29
|
+
|
30
|
+
def call func, *args
|
31
|
+
context.call func, *args
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def context
|
36
|
+
@context ||= ExecJS.compile environment
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/render_execjs.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
require "execjs"
|
4
|
+
|
5
|
+
require "render_execjs/configuration"
|
6
|
+
require "render_execjs/renderer"
|
7
|
+
require "render_execjs/railtie"
|
1
8
|
require "render_execjs/version"
|
2
9
|
|
3
10
|
module RenderExecJS
|
4
|
-
|
5
|
-
end
|
11
|
+
include Configuration
|
12
|
+
end
|
data/render_execjs.gemspec
CHANGED
@@ -18,8 +18,11 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.
|
22
|
-
s.
|
21
|
+
s.add_runtime_dependency "rails", ">= 3.1.3"
|
22
|
+
s.add_runtime_dependency "execjs"
|
23
23
|
|
24
24
|
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rspec-rails"
|
26
|
+
s.add_development_dependency "coffee-script"
|
27
|
+
s.add_development_dependency "appraisal"
|
25
28
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module RenderExecJS
|
4
|
+
describe Configuration do
|
5
|
+
it "should support a configure block" do
|
6
|
+
expect do
|
7
|
+
RenderExecJS.configure do
|
8
|
+
end
|
9
|
+
end.should_not raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should get and set the environment in the configure block" do
|
13
|
+
expect do
|
14
|
+
RenderExecJS.configure do
|
15
|
+
self.environment = "'the environment!'"
|
16
|
+
end
|
17
|
+
end.should_not raise_error
|
18
|
+
|
19
|
+
RenderExecJS.configure do
|
20
|
+
self.environment.should == "'the environment!'"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should allow no 'self.' or equal sign when setting the environment in a configure block" do
|
25
|
+
expect do
|
26
|
+
RenderExecJS.configure do
|
27
|
+
environment "'some environment'"
|
28
|
+
end
|
29
|
+
end.should_not raise_error
|
30
|
+
|
31
|
+
RenderExecJS.configure do
|
32
|
+
self.environment.should == "'some environment'"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should allow configuration of the environment outside of a configuration block" do
|
37
|
+
expect do
|
38
|
+
RenderExecJS.environment = "'a environment!'"
|
39
|
+
end.should_not raise_error
|
40
|
+
|
41
|
+
RenderExecJS.environment.should == "'a environment!'"
|
42
|
+
|
43
|
+
expect do
|
44
|
+
RenderExecJS.environment "'another environment!'"
|
45
|
+
end.should_not raise_error
|
46
|
+
|
47
|
+
RenderExecJS.environment.should == "'another environment!'"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should be a proxy to Renderer for the environment" do
|
51
|
+
RenderExecJS.environment = "'some env'"
|
52
|
+
RenderExecJS::Renderer.environment.should == "'some env'"
|
53
|
+
|
54
|
+
RenderExecJS.configure do
|
55
|
+
environment "'another env'"
|
56
|
+
end
|
57
|
+
|
58
|
+
RenderExecJS::Renderer.environment.should == "'another env'"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should only set coffeescript_environment" do
|
62
|
+
expect do
|
63
|
+
RenderExecJS.coffeescript_environment = '"#{"a"} coffee #{"environment"}"'
|
64
|
+
end.should_not raise_error
|
65
|
+
|
66
|
+
expect do
|
67
|
+
RenderExecJS.coffeescript_environment '"#{"a"} coffeescript #{"environment"}"'
|
68
|
+
end.should_not raise_error
|
69
|
+
|
70
|
+
expect do
|
71
|
+
RenderExecJS.configure do
|
72
|
+
coffeescript_environment '"#{"another"} coffee #{"environment"}"'
|
73
|
+
end
|
74
|
+
end.should_not raise_error
|
75
|
+
|
76
|
+
expect do
|
77
|
+
RenderExecJS.coffeescript_environment
|
78
|
+
end.should raise_error
|
79
|
+
|
80
|
+
expect do
|
81
|
+
RenderExecJS.configure do
|
82
|
+
self.coffeescript_environment
|
83
|
+
end
|
84
|
+
end.should raise_error
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should proxy coffeescript_environment to Renderer" do
|
88
|
+
RenderExecJS::Renderer.environment = nil
|
89
|
+
|
90
|
+
RenderExecJS.coffeescript_environment = '"#{"some"} coffeescript #{"environment"}"'
|
91
|
+
RenderExecJS::Renderer.environment.should_not == nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'rails'
|
5
|
+
require 'render_execjs'
|
6
|
+
require 'coffee-script'
|
7
|
+
|
8
|
+
require 'tmpdir'
|
9
|
+
require 'fileutils'
|
10
|
+
|
11
|
+
$gem_options = {}
|
12
|
+
possible_dev_dependencies = %w[rails execjs coffeescript]
|
13
|
+
Bundler.load.specs.each do |s|
|
14
|
+
if possible_dev_dependencies.include? s.name
|
15
|
+
$gem_options[s.name] = {
|
16
|
+
:version => s.version.to_s
|
17
|
+
}.tap do |hash|
|
18
|
+
hash[:path] = s.full_gem_path if File.exists?("#{s.full_gem_path}/#{s.name}.gemspec")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
|
24
|
+
require f
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec.configure do |config|
|
28
|
+
config.include RenderExecJS::Helpers
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe "Install Generator" do
|
4
|
+
before :all do
|
5
|
+
create_rails_project
|
6
|
+
end
|
7
|
+
|
8
|
+
after :all do
|
9
|
+
delete_rails_project
|
10
|
+
end
|
11
|
+
|
12
|
+
it "adds the configuration file" do
|
13
|
+
file = "#{rails_project_dir}/config/initializers/render_execjs.rb"
|
14
|
+
|
15
|
+
File.exists?(file).should == false
|
16
|
+
run_cmd "rails generate render_execjs:install"
|
17
|
+
File.exists?(file).should == true
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module RenderExecJS
|
4
|
+
describe Renderer do
|
5
|
+
it "should execute simple JavaScript" do
|
6
|
+
Renderer.exec("return 'hello!';").should == "hello!"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should execute JavaScript in the context of environment" do
|
10
|
+
RenderExecJS.environment = "var a = 'hi there';"
|
11
|
+
|
12
|
+
Renderer.exec("return a;").should == "hi there"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should render correctly when environment is changed" do
|
16
|
+
RenderExecJS.environment = "var a = 'hi there';"
|
17
|
+
Renderer.exec("return a;").should == "hi there"
|
18
|
+
|
19
|
+
RenderExecJS.environment = "var b = 'oh hai';"
|
20
|
+
Renderer.exec("return b;").should == "oh hai"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should call a function in the environment" do
|
24
|
+
RenderExecJS.environment = <<-JS
|
25
|
+
function add_three(first, second, third) {
|
26
|
+
return first + second + third;
|
27
|
+
}
|
28
|
+
JS
|
29
|
+
|
30
|
+
Renderer.call("add_three", 1, 2, 3).should == 6
|
31
|
+
Renderer.call("add_three", "lol", "cat", "!").should == "lolcat!"
|
32
|
+
Renderer.exec("return add_three(
|
33
|
+
add_three('l','o','l'),
|
34
|
+
add_three('c','a','t'),
|
35
|
+
add_three('!',1,'!'));").should == "lolcat!1!"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should raise an error when a function is not in the environment" do
|
39
|
+
RenderExecJS.environment = <<-JS
|
40
|
+
function add_three(first, second, third) {
|
41
|
+
return first + second + third;
|
42
|
+
}
|
43
|
+
JS
|
44
|
+
|
45
|
+
expect do
|
46
|
+
Renderer.call "add_four", 1, 2, 3, 4
|
47
|
+
end.to raise_error ExecJS::ProgramError
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise an error when calling something that isn't a function" do
|
51
|
+
RenderExecJS.environment = <<-JS
|
52
|
+
var b = 'hai';
|
53
|
+
JS
|
54
|
+
|
55
|
+
expect do
|
56
|
+
Renderer.call "b", 1, 2, 3
|
57
|
+
end.to raise_error ExecJS::ProgramError
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should compile and execute CoffeeScript" do
|
61
|
+
RenderExecJS.environment <<-JS
|
62
|
+
function hello() {
|
63
|
+
return 'hi';
|
64
|
+
}
|
65
|
+
JS
|
66
|
+
|
67
|
+
Renderer.exec("return hello() is hello() is hello()", :coffee_script => true).should == true
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should compile and execute a CoffeeScript environment" do
|
71
|
+
RenderExecJS.coffeescript_environment <<-CS
|
72
|
+
hello = ->
|
73
|
+
'hi'
|
74
|
+
CS
|
75
|
+
|
76
|
+
Renderer.call("hello").should == "hi"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module RenderExecJS
|
2
|
+
module Helpers
|
3
|
+
class ExecutionError < StandardError
|
4
|
+
attr_accessor :output
|
5
|
+
|
6
|
+
def initialize message, output = nil
|
7
|
+
super message
|
8
|
+
|
9
|
+
self.output = output
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
"#{super}\nOutput was:\n#{output}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def base_dir
|
18
|
+
File.expand_path '../../..', __FILE__
|
19
|
+
end
|
20
|
+
|
21
|
+
def rails_project_dir
|
22
|
+
@@tmpdir ||= Dir.mktmpdir 'tmp', base_dir
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_rails_project
|
26
|
+
Dir.chdir(rails_project_dir) do
|
27
|
+
run_cmd "rails new ."
|
28
|
+
File.delete "#{rails_project_dir}/public/index.html"
|
29
|
+
|
30
|
+
working_gems = {
|
31
|
+
"rails" => {
|
32
|
+
:version => "4.0.0.beta"
|
33
|
+
}
|
34
|
+
}.merge $gem_options
|
35
|
+
|
36
|
+
File.open "Gemfile", "w" do |f|
|
37
|
+
gemfile = <<-GEMFILE
|
38
|
+
source 'http://rubygems.org'
|
39
|
+
|
40
|
+
gem 'rails', '~> #{working_gems["rails"][:version]}'
|
41
|
+
gem 'sqlite3'
|
42
|
+
gem 'render_execjs', :path => '#{base_dir}'
|
43
|
+
GEMFILE
|
44
|
+
|
45
|
+
f.write(gemfile.tap do |g|
|
46
|
+
working_gems.delete "rails"
|
47
|
+
working_gems.each do |gem, options|
|
48
|
+
g << "gem '#{gem}', '~> #{options[:version]}'\n"
|
49
|
+
end
|
50
|
+
end)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete_rails_project
|
56
|
+
FileUtils.remove_entry_secure rails_project_dir
|
57
|
+
@@tmpdir = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
def run_cmd cmd, working_directory = rails_project_dir, clean_env = false, gemfile = "Gemfile", env = {}
|
61
|
+
env["BUNDLE_GEMFILE"] = "#{working_directory}/#{gemfile}" if clean_env
|
62
|
+
todo = Proc.new do
|
63
|
+
r, w = IO.pipe
|
64
|
+
Kernel.spawn env, cmd, :out =>w , :err => w, :chdir => working_directory
|
65
|
+
w.close
|
66
|
+
Process.wait
|
67
|
+
output = r.read
|
68
|
+
r.close
|
69
|
+
unless $?.exitstatus == 0
|
70
|
+
raise ExecutionError, "Command failed with exit status #{$?.exitstatus}: #{cmd}", output
|
71
|
+
end
|
72
|
+
$last_ouput = output
|
73
|
+
end
|
74
|
+
|
75
|
+
if clean_env
|
76
|
+
Bundler.with_clean_env &todo
|
77
|
+
else
|
78
|
+
todo.call
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
metadata
CHANGED
@@ -1,92 +1,144 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_execjs
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Tasveer Singh
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-02-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rails
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70360431236400 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
23
21
|
version: 3.1.3
|
24
22
|
type: :runtime
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: execjs
|
28
23
|
prerelease: false
|
29
|
-
|
24
|
+
version_requirements: *70360431236400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: execjs
|
27
|
+
requirement: &70360431235300 !ruby/object:Gem::Requirement
|
30
28
|
none: false
|
31
|
-
requirements:
|
32
|
-
- -
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
35
33
|
type: :runtime
|
36
|
-
|
37
|
-
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70360431235300
|
36
|
+
- !ruby/object:Gem::Dependency
|
38
37
|
name: rspec
|
38
|
+
requirement: &70360431234620 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
39
45
|
prerelease: false
|
40
|
-
|
46
|
+
version_requirements: *70360431234620
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec-rails
|
49
|
+
requirement: &70360431233340 !ruby/object:Gem::Requirement
|
41
50
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
46
55
|
type: :development
|
47
|
-
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70360431233340
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: coffee-script
|
60
|
+
requirement: &70360431232580 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70360431232580
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: &70360431248040 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70360431248040
|
48
80
|
description: Execute server side JavaScript and render the result with Rails.
|
49
|
-
email:
|
81
|
+
email:
|
50
82
|
- taz@zenapsis.com
|
51
83
|
executables: []
|
52
|
-
|
53
84
|
extensions: []
|
54
|
-
|
55
85
|
extra_rdoc_files: []
|
56
|
-
|
57
|
-
files:
|
86
|
+
files:
|
58
87
|
- .gitignore
|
88
|
+
- Appraisals
|
59
89
|
- Gemfile
|
90
|
+
- README.md
|
60
91
|
- Rakefile
|
92
|
+
- gemfiles/rails31.gemfile
|
93
|
+
- gemfiles/rails31.gemfile.lock
|
94
|
+
- gemfiles/rails32.gemfile
|
95
|
+
- gemfiles/rails32.gemfile.lock
|
96
|
+
- lib/generators/render_execjs/install_generator.rb
|
97
|
+
- lib/generators/templates/render_execjs.rb
|
61
98
|
- lib/render_execjs.rb
|
99
|
+
- lib/render_execjs/configuration.rb
|
100
|
+
- lib/render_execjs/railtie.rb
|
101
|
+
- lib/render_execjs/renderer.rb
|
62
102
|
- lib/render_execjs/version.rb
|
63
103
|
- render_execjs.gemspec
|
64
|
-
|
104
|
+
- spec/configuration_spec.rb
|
105
|
+
- spec/helper.rb
|
106
|
+
- spec/install_spec.rb
|
107
|
+
- spec/renderer_spec.rb
|
108
|
+
- spec/support/helpers.rb
|
109
|
+
homepage: ''
|
65
110
|
licenses: []
|
66
|
-
|
67
111
|
post_install_message:
|
68
112
|
rdoc_options: []
|
69
|
-
|
70
|
-
require_paths:
|
113
|
+
require_paths:
|
71
114
|
- lib
|
72
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
116
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version:
|
78
|
-
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: -461247744510695759
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
125
|
none: false
|
80
|
-
requirements:
|
81
|
-
- -
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
version:
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
segments:
|
131
|
+
- 0
|
132
|
+
hash: -461247744510695759
|
84
133
|
requirements: []
|
85
|
-
|
86
134
|
rubyforge_project: render_execjs
|
87
|
-
rubygems_version: 1.8.
|
135
|
+
rubygems_version: 1.8.15
|
88
136
|
signing_key:
|
89
137
|
specification_version: 3
|
90
138
|
summary: Render JavaScript with Rails
|
91
|
-
test_files:
|
92
|
-
|
139
|
+
test_files:
|
140
|
+
- spec/configuration_spec.rb
|
141
|
+
- spec/helper.rb
|
142
|
+
- spec/install_spec.rb
|
143
|
+
- spec/renderer_spec.rb
|
144
|
+
- spec/support/helpers.rb
|