opal-rails 0.2.0 → 0.2.1
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/Gemfile +2 -2
- data/README.md +49 -27
- data/lib/opal/rails/template_handler.rb +22 -2
- data/lib/opal/rails/version.rb +1 -1
- data/spec/integration/assigns_spec.rb +33 -0
- data/spec/integration/{opal_spec.rb → in_browser_specs_spec.rb} +1 -1
- data/test_app/app/controllers/application_controller.rb +15 -0
- data/test_app/app/views/application/with_assignments.js.opal +31 -0
- data/test_app/config/routes.rb +2 -0
- metadata +17 -14
data/Gemfile
CHANGED
@@ -4,8 +4,8 @@ source :rubygems
|
|
4
4
|
gemspec
|
5
5
|
|
6
6
|
|
7
|
-
gem 'opal-jquery', :git => 'git://github.com/
|
8
|
-
gem 'opal-spec', :git => 'git://github.com/
|
7
|
+
gem 'opal-jquery', :git => 'git://github.com/opal/opal-jquery.git', :require => false
|
8
|
+
gem 'opal-spec', :git => 'git://github.com/opal/opal-spec.git', :require => false
|
9
9
|
|
10
10
|
|
11
11
|
# Test app stuff
|
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# Opal Rails
|
2
2
|
|
3
|
+
[](http://travis-ci.org/elia/opal-rails)
|
4
|
+
[](https://codeclimate.com/github/elia/opal-rails)
|
5
|
+
|
3
6
|
_Rails (3.2+) bindings for [Opal JS](http://opalrb.org) engine._
|
4
7
|
|
5
8
|
|
9
|
+
|
6
10
|
## Installation
|
7
11
|
|
8
12
|
In your `Gemfile`
|
@@ -12,8 +16,10 @@ gem 'opal-rails'
|
|
12
16
|
```
|
13
17
|
|
14
18
|
|
19
|
+
|
15
20
|
## Usage
|
16
21
|
|
22
|
+
|
17
23
|
### Asset Pipeline
|
18
24
|
|
19
25
|
``` js
|
@@ -32,19 +38,33 @@ and then just use the `.rb` or `.opal` extensions:
|
|
32
38
|
```ruby
|
33
39
|
# app/assets/javascripts/hi-world.js.rb
|
34
40
|
|
35
|
-
puts "G'day world!"
|
41
|
+
puts "G'day world!" # check the console
|
36
42
|
```
|
37
43
|
|
38
44
|
|
39
45
|
|
46
|
+
|
40
47
|
### As a template
|
41
48
|
|
42
|
-
You can use it for your
|
49
|
+
You can use it for your views too, it even inherits instance and local variables from actions:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# app/controllers/posts_controller.rb
|
53
|
+
|
54
|
+
def create
|
55
|
+
@post = Post.create!(params[:post])
|
56
|
+
render type: :js, locals: {comments_html: render_to_string(@post.comments)}
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Each assign is filtered through JSON so it's reduced to basic types:
|
43
61
|
|
44
62
|
```ruby
|
45
|
-
# app/views/posts/
|
63
|
+
# app/views/posts/cerate.js.opal
|
46
64
|
|
47
|
-
|
65
|
+
Document['.post .title'].html = @post[:title]
|
66
|
+
Document['.post .body'].html = @post[:body]
|
67
|
+
Document['.post .comments'].html = comments_html
|
48
68
|
```
|
49
69
|
|
50
70
|
|
@@ -55,7 +75,9 @@ Of course you need to require `haml-rails` separately since its presence is not
|
|
55
75
|
```haml
|
56
76
|
-# app/views/posts/show.html.haml
|
57
77
|
|
58
|
-
%article
|
78
|
+
%article.post
|
79
|
+
%h1.title= post.title
|
80
|
+
.body= post.body
|
59
81
|
|
60
82
|
%a#show-comments Display Comments!
|
61
83
|
|
@@ -65,9 +87,9 @@ Of course you need to require `haml-rails` separately since its presence is not
|
|
65
87
|
|
66
88
|
:opal
|
67
89
|
Document.ready? do
|
68
|
-
|
69
|
-
|
70
|
-
false
|
90
|
+
Document['#show-comments'].on :click do
|
91
|
+
Document['.comments'].first.show
|
92
|
+
false
|
71
93
|
end
|
72
94
|
end
|
73
95
|
```
|
@@ -102,22 +124,22 @@ Then visit `/opal_spec` from your app and **reload at will**.
|
|
102
124
|
|
103
125
|
## License
|
104
126
|
|
105
|
-
|
106
|
-
|
107
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
108
|
-
of this software and associated documentation files (the "Software"), to deal
|
109
|
-
in the Software without restriction, including without limitation the rights
|
110
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
111
|
-
copies of the Software, and to permit persons to whom the Software is
|
112
|
-
furnished to do so, subject to the following conditions:
|
113
|
-
|
114
|
-
The above copyright notice and this permission notice shall be included in
|
115
|
-
all copies or substantial portions of the Software.
|
116
|
-
|
117
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
118
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
119
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
120
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
121
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
122
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
123
|
-
THE SOFTWARE.
|
127
|
+
© 2012 Elia Schito
|
128
|
+
|
129
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
130
|
+
of this software and associated documentation files (the "Software"), to deal
|
131
|
+
in the Software without restriction, including without limitation the rights
|
132
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
133
|
+
copies of the Software, and to permit persons to whom the Software is
|
134
|
+
furnished to do so, subject to the following conditions:
|
135
|
+
|
136
|
+
The above copyright notice and this permission notice shall be included in
|
137
|
+
all copies or substantial portions of the Software.
|
138
|
+
|
139
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
140
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
141
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
142
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
143
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
144
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
145
|
+
THE SOFTWARE.
|
@@ -1,10 +1,30 @@
|
|
1
1
|
module Opal
|
2
2
|
module Rails
|
3
3
|
class TemplateHandler
|
4
|
+
|
4
5
|
def self.call(template)
|
6
|
+
new.call(template)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def call(template)
|
5
11
|
escaped = template.source.gsub(':', '\:')
|
6
|
-
string = '%q:' + escaped + '
|
7
|
-
"Opal.parse(#{string})"
|
12
|
+
string = '%q:' + escaped + ':'
|
13
|
+
"Opal.parse('Object.new.instance_eval {' << #{assigns} << #{local_assigns} << #{string} << '}')"
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def local_assigns
|
19
|
+
<<-'RUBY'.strip
|
20
|
+
JSON.parse(local_assigns.to_json).map { |key, val| "#{key} = #{val.inspect};" }.join
|
21
|
+
RUBY
|
22
|
+
end
|
23
|
+
|
24
|
+
def assigns
|
25
|
+
<<-'RUBY'.strip
|
26
|
+
JSON.parse(@_assigns.to_json).map { |key, val| "@#{key} = #{val.inspect};" }.join
|
27
|
+
RUBY
|
8
28
|
end
|
9
29
|
end
|
10
30
|
end
|
data/lib/opal/rails/version.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require "execjs"
|
3
|
+
|
4
|
+
describe 'controller assignments' do
|
5
|
+
it 'are in the template' do
|
6
|
+
source = get_source_of '/application/with_assignments.js'
|
7
|
+
source.gsub!(/;\s*\Z/,'') # execjs eval doesn't like the trailing semicolon
|
8
|
+
assignments = opal_eval(source)
|
9
|
+
|
10
|
+
{
|
11
|
+
:number_var => 1234,
|
12
|
+
:string_var => 'hello',
|
13
|
+
:array_var => [1,'a'],
|
14
|
+
:hash_var => {:a => 1, :b => 2}.stringify_keys,
|
15
|
+
:object_var => {:contents => 'json representation'}.stringify_keys,
|
16
|
+
:local_var => 'i am local',
|
17
|
+
}.each_pair do |ivar, assignment|
|
18
|
+
assignments[ivar.to_s].should eq(assignment)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_source_of path
|
23
|
+
get path
|
24
|
+
response.should be_success
|
25
|
+
source = response.body
|
26
|
+
end
|
27
|
+
|
28
|
+
def opal_eval source
|
29
|
+
opal_source = get_source_of '/assets/opal.js'
|
30
|
+
context = ExecJS.compile opal_source
|
31
|
+
context.eval source
|
32
|
+
end
|
33
|
+
end
|
@@ -1,3 +1,18 @@
|
|
1
1
|
class ApplicationController < ActionController::Base
|
2
2
|
protect_from_forgery
|
3
|
+
|
4
|
+
def with_assignments
|
5
|
+
object = Object.new
|
6
|
+
def object.as_json options = {}
|
7
|
+
{:contents => 'json representation'}
|
8
|
+
end
|
9
|
+
|
10
|
+
@number_var = 1234
|
11
|
+
@string_var = 'hello'
|
12
|
+
@array_var = [1,'a']
|
13
|
+
@hash_var = {:a => 1, :b => 2}
|
14
|
+
@object_var = object
|
15
|
+
|
16
|
+
render :type => :js, :locals => { :local_var => 'i am local' }
|
17
|
+
end
|
3
18
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Fix to native which is broken on some basic types
|
2
|
+
class Hash
|
3
|
+
def to_native
|
4
|
+
%x{
|
5
|
+
var result = {}, map = #{self}.map, bucket, value;
|
6
|
+
|
7
|
+
for (var assoc in map) {
|
8
|
+
bucket = map[assoc];
|
9
|
+
value = bucket[1];
|
10
|
+
|
11
|
+
if (value.$to_native) {
|
12
|
+
result[assoc] = #{ `value`.to_native };
|
13
|
+
}
|
14
|
+
else {
|
15
|
+
result[assoc] = value;
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
return result;
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
return {
|
25
|
+
number_var: @number_var,
|
26
|
+
string_var: @string_var,
|
27
|
+
array_var: @array_var,
|
28
|
+
hash_var: @hash_var,
|
29
|
+
object_var: @object_var,
|
30
|
+
local_var: local_var
|
31
|
+
}.to_native
|
data/test_app/config/routes.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: opal
|
16
|
-
requirement: &
|
16
|
+
requirement: &70118596042580 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - =
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.3.22
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70118596042580
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &70118596041060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.2.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70118596041060
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70118596039100 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '2.4'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70118596039100
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec-rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &70118596037020 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '2.4'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70118596037020
|
58
58
|
description: Rails bindings for opal JS engine
|
59
59
|
email:
|
60
60
|
- elia@schito.me
|
@@ -87,7 +87,8 @@ files:
|
|
87
87
|
- lib/tasks/opal-rails_tasks.rake
|
88
88
|
- opal-rails.gemspec
|
89
89
|
- script/rails
|
90
|
-
- spec/integration/
|
90
|
+
- spec/integration/assigns_spec.rb
|
91
|
+
- spec/integration/in_browser_specs_spec.rb
|
91
92
|
- spec/opal/rails/processor_spec.rb
|
92
93
|
- spec/spec_helper.rb
|
93
94
|
- test_app/.gitignore
|
@@ -98,6 +99,7 @@ files:
|
|
98
99
|
- test_app/app/assets/javascripts/spec/example_spec.js.rb
|
99
100
|
- test_app/app/assets/stylesheets/application.css
|
100
101
|
- test_app/app/controllers/application_controller.rb
|
102
|
+
- test_app/app/views/application/with_assignments.js.opal
|
101
103
|
- test_app/app/views/layouts/application.html.erb
|
102
104
|
- test_app/config.ru
|
103
105
|
- test_app/config/application.rb
|
@@ -138,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
140
|
version: '0'
|
139
141
|
segments:
|
140
142
|
- 0
|
141
|
-
hash:
|
143
|
+
hash: 934058803805341785
|
142
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
145
|
none: false
|
144
146
|
requirements:
|
@@ -147,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
149
|
version: '0'
|
148
150
|
segments:
|
149
151
|
- 0
|
150
|
-
hash:
|
152
|
+
hash: 934058803805341785
|
151
153
|
requirements: []
|
152
154
|
rubyforge_project: opal-rails
|
153
155
|
rubygems_version: 1.8.17
|
@@ -155,6 +157,7 @@ signing_key:
|
|
155
157
|
specification_version: 3
|
156
158
|
summary: Rails bindings for opal JS engine
|
157
159
|
test_files:
|
158
|
-
- spec/integration/
|
160
|
+
- spec/integration/assigns_spec.rb
|
161
|
+
- spec/integration/in_browser_specs_spec.rb
|
159
162
|
- spec/opal/rails/processor_spec.rb
|
160
163
|
- spec/spec_helper.rb
|