coffee-views 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/coffee-views.gemspec +3 -1
- data/lib/coffee_views.rb +3 -56
- data/lib/coffee_views/rails.rb +41 -0
- data/lib/coffee_views/slim.rb +20 -0
- data/lib/coffee_views/version.rb +3 -0
- data/spec/dummy/app/controllers/events_controller.rb +2 -0
- data/spec/dummy/app/views/events/index.html.slim +2 -0
- data/spec/dummy/config/application.rb +1 -0
- data/spec/views/events_spec.rb +18 -0
- metadata +30 -15
data/coffee-views.gemspec
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/lib/coffee_views/version'
|
1
2
|
require 'date'
|
2
3
|
|
3
4
|
Gem::Specification.new do |s|
|
4
5
|
s.name = 'coffee-views'
|
5
|
-
s.version =
|
6
|
+
s.version = CoffeeViews::VERSION
|
6
7
|
s.date = Date.today.to_s
|
7
8
|
s.authors = ['Yury Korolev']
|
8
9
|
s.email = 'yury.korolev@gmail.com'
|
@@ -22,5 +23,6 @@ Gem::Specification.new do |s|
|
|
22
23
|
s.add_development_dependency "sass-rails"
|
23
24
|
s.add_development_dependency "coffee-rails"
|
24
25
|
s.add_development_dependency "uglifier"
|
26
|
+
s.add_development_dependency "slim"
|
25
27
|
|
26
28
|
end
|
data/lib/coffee_views.rb
CHANGED
@@ -1,56 +1,3 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
|
5
|
-
module CoffeeViews
|
6
|
-
module Rails
|
7
|
-
class TemplateHandler
|
8
|
-
def self.erb_handler
|
9
|
-
@@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.preprocess source
|
13
|
-
return "" unless source
|
14
|
-
|
15
|
-
source.gsub! /<%==(.*?)%>/, '`<%==\1%>`'
|
16
|
-
source.gsub! /<%=([^=].*?)%>/, '`<%==(\1).to_json%>`'
|
17
|
-
source.gsub! /<%([^=].*?)%>/, '`<%\1%>`'
|
18
|
-
source.gsub! /#\{==(.*?)\}/, '#{`<%==\1%>`}'
|
19
|
-
source.gsub! /#\{=([^=].*?)\}/, '#{`<%==(\1).to_json%>`}'
|
20
|
-
source
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.compile_coffee(source)
|
24
|
-
source = preprocess(source)
|
25
|
-
::CoffeeScript.compile(source)
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.call(template)
|
29
|
-
source = compile_coffee(template.source)
|
30
|
-
# TODO: find how to set source back to template without instance_variable_set
|
31
|
-
template.instance_variable_set :@source, source
|
32
|
-
|
33
|
-
erb_handler.call(template)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
ActiveSupport.on_load(:action_view) do
|
39
|
-
ActionView::Template.register_template_handler :coffee, CoffeeViews::Rails::TemplateHandler
|
40
|
-
end
|
41
|
-
|
42
|
-
if defined?(Slim)
|
43
|
-
class CoffeeViewEngine < Slim::EmbeddedEngine::ERBEngine
|
44
|
-
def on_slim_embedded(engine, body)
|
45
|
-
source = CoffeeViews::Rails::TemplateHandler.compile_coffee(collect_text(body))
|
46
|
-
source = Temple::ERB::Parser.new.call(source)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
Slim::EmbeddedEngine.register :coffeeview,
|
51
|
-
Slim::EmbeddedEngine::TagEngine,
|
52
|
-
:tag => :script,
|
53
|
-
:attributes => { :type => 'text/javascript' },
|
54
|
-
:engine => CoffeeViewEngine
|
55
|
-
end
|
56
|
-
end
|
1
|
+
require 'coffee_views/rails'
|
2
|
+
require 'coffee_views/slim'
|
3
|
+
require 'coffee_views/version'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'coffee-rails'
|
3
|
+
require 'coffee-script'
|
4
|
+
|
5
|
+
module CoffeeViews
|
6
|
+
module Rails
|
7
|
+
class TemplateHandler
|
8
|
+
def self.erb_handler
|
9
|
+
@@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.preprocess source
|
13
|
+
return "" unless source
|
14
|
+
|
15
|
+
source.gsub! /<%==(.*?)%>/, '`<%==\1%>`'
|
16
|
+
source.gsub! /<%=([^=].*?)%>/, '`<%==(\1).to_json%>`'
|
17
|
+
source.gsub! /<%([^=].*?)%>/, '`<%\1%>`'
|
18
|
+
source.gsub! /#\{==(.*?)\}/, '#{`<%==\1%>`}'
|
19
|
+
source.gsub! /#\{=([^=].*?)\}/, '#{`<%==(\1).to_json%>`}'
|
20
|
+
source
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.compile_coffee(source)
|
24
|
+
source = preprocess(source)
|
25
|
+
::CoffeeScript.compile(source)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.call(template)
|
29
|
+
source = compile_coffee(template.source)
|
30
|
+
# TODO: find how to set source back to template without instance_variable_set
|
31
|
+
template.instance_variable_set :@source, source
|
32
|
+
|
33
|
+
erb_handler.call(template)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
ActiveSupport.on_load(:action_view) do
|
39
|
+
ActionView::Template.register_template_handler :coffee, CoffeeViews::Rails::TemplateHandler
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
ActiveSupport.on_load(:action_view) do
|
3
|
+
if Object.const_defined?(:Slim)
|
4
|
+
|
5
|
+
module CoffeeViews
|
6
|
+
class CoffeeViewEngine < Slim::EmbeddedEngine::ERBEngine
|
7
|
+
def on_slim_embedded(engine, body)
|
8
|
+
source = CoffeeViews::Rails::TemplateHandler.compile_coffee(Slim::CollectText.new.call(body))
|
9
|
+
source = Temple::ERB::Parser.new.call(source)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Slim::EmbeddedEngine.register :coffeeview,
|
14
|
+
::Slim::EmbeddedEngine::TagEngine,
|
15
|
+
:tag => :script,
|
16
|
+
:attributes => { :type => 'text/javascript' },
|
17
|
+
:engine => CoffeeViewEngine
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/views/events_spec.rb
CHANGED
@@ -15,6 +15,24 @@ JAVASCRIPT
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
describe "events/index.html.slim" do
|
19
|
+
it "adds embeded engine for slim" do
|
20
|
+
assign :first_name, "Yury"
|
21
|
+
assign :last_name, "'Korolev'"
|
22
|
+
|
23
|
+
render
|
24
|
+
|
25
|
+
expected = <<-JAVASCRIPT
|
26
|
+
<script type="text/javascript">(function() {
|
27
|
+
alert("hello, " + "Yury" + " " + 'Korolev');
|
28
|
+
}).call(this);
|
29
|
+
</script>
|
30
|
+
JAVASCRIPT
|
31
|
+
|
32
|
+
rendered.should == expected.strip
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
18
36
|
describe "events/show.js.coffee" do
|
19
37
|
it "converts to json" do
|
20
38
|
assign :event, {name: "Birth day", date: Date.new(1983, 02, 15)}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coffee-views
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-10-04 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70162754300580 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70162754300580
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: coffee-rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &70162754300160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70162754300160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70162754299700 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70162754299700
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec-rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &70162754299140 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '2.6'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70162754299140
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: sass-rails
|
60
|
-
requirement: &
|
60
|
+
requirement: &70162754378700 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70162754378700
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: coffee-rails
|
71
|
-
requirement: &
|
71
|
+
requirement: &70162754378060 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70162754378060
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: uglifier
|
82
|
-
requirement: &
|
82
|
+
requirement: &70162754377640 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,18 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70162754377640
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: slim
|
93
|
+
requirement: &70162754377140 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70162754377140
|
91
102
|
description: Allows you to create .js.coffee views
|
92
103
|
email: yury.korolev@gmail.com
|
93
104
|
executables: []
|
@@ -102,6 +113,9 @@ files:
|
|
102
113
|
- coffee-views.gemspec
|
103
114
|
- lib/coffee-views.rb
|
104
115
|
- lib/coffee_views.rb
|
116
|
+
- lib/coffee_views/rails.rb
|
117
|
+
- lib/coffee_views/slim.rb
|
118
|
+
- lib/coffee_views/version.rb
|
105
119
|
- spec/dummy/Rakefile
|
106
120
|
- spec/dummy/app/assets/javascripts/application.js
|
107
121
|
- spec/dummy/app/assets/stylesheets/application.css
|
@@ -110,6 +124,7 @@ files:
|
|
110
124
|
- spec/dummy/app/helpers/application_helper.rb
|
111
125
|
- spec/dummy/app/mailers/.gitkeep
|
112
126
|
- spec/dummy/app/models/.gitkeep
|
127
|
+
- spec/dummy/app/views/events/index.html.slim
|
113
128
|
- spec/dummy/app/views/events/index.js.coffee
|
114
129
|
- spec/dummy/app/views/events/show.js.coffee
|
115
130
|
- spec/dummy/app/views/layouts/application.html.erb
|