js_erb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,6 @@
1
+ = JsErb - Changelog
2
+
3
+ == js_erb v0.1.0 released 2010/05/08
4
+
5
+ * initial release
6
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [Kristijan Sedlak]
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.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ = JsErb - Javascript renderer for Ruby on Rails
2
+
3
+ Today's projects contain more and more javascript code. Thus, from the design perspective,
4
+ javascript should be located under the RAILS/app directory instead of RAILS/public. Javascript
5
+ usually produces at least a small piece of HTML code thus the Javascript templating is needed.
6
+ Sites also require translations. Finally there are cases when you really need to put some
7
+ ruby code into javascript. These are the features of the JsErb plugin.
8
+
9
+ == Installation (Rails 3 ready)
10
+
11
+ Add this line to your Gemfile
12
+
13
+ gem 'js_erb'
14
+
15
+ Move public/javascripts directory to app/javascripts. Files with *.jserb extension can contain
16
+ ERB-styled ruby code (e.g. <%= var %>).
17
+
18
+ Create a RAILS/config/locales/en.js.yml file
19
+
20
+ # Javascript translations
21
+ en:
22
+ welcome: 'Welcome'
23
+
24
+ Create a configuration file RAILS/config/js_erb.yml
25
+
26
+ # Source paths
27
+ path:
28
+ views: app/views
29
+ source: app/javascripts
30
+ compiled: public/javascripts/compiled
31
+ locales: config/locales
32
+
33
+ # List of Haml, ERB or HTML files (base on path.source path)
34
+ views:
35
+ - users/_user.html.haml
36
+ - users/_line.html.erb
37
+
38
+ You have to change the views list of the configuration file. Note also you can use ruby code
39
+ inside the yml file (e.g. <%= var %>).
40
+
41
+ At the end set javascript_include_tag (add javascript files into the <head>).
42
+
43
+ == JsErb with Jammit
44
+
45
+ Best practice is to use a packager to optimize project's javascript and stylesheets. You can
46
+ easily connect JsErb plugin with {Jammit}[http://documentcloud.github.com/jammit/] assets
47
+ packager. To integrate JsErb with Jammit you simply define JsErb's generated compiled files
48
+ (at RAILS/public/javascripts/compiled) in to Jammit's assets.yml file.
49
+
50
+ == JsErb caching
51
+
52
+ The plugin will always compile files in the development or testing environment. In other
53
+ environments (e.g. production) it will compile files only if the compiled directory is
54
+ not present.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the comp_js plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the comp_js plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'JsErb'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,104 @@
1
+ module JsErb
2
+
3
+ class Controller < ActionView::Base
4
+
5
+ #
6
+ # Runs the compile process.
7
+ #
8
+ def compile!
9
+ if defined?(Rails)
10
+ if (Rails.env.development? || Rails.env.test? || !File.exists?(JsErb.config['path']['compiled']))
11
+ Rails.logger.info '[JsErb] Compiling javascript ...';
12
+ prepare_path
13
+ compile_source_files
14
+ compile_locales
15
+ compile_views
16
+ Rails.logger.info '[JsErb] Done!';
17
+ true
18
+ end
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ #
25
+ # Enshures thath the directories exist.
26
+ #
27
+ def prepare_path
28
+ FileUtils.mkdir_p(JsErb.config['path']['source'])
29
+ FileUtils.mkdir_p(JsErb.config['path']['compiled'])
30
+ FileUtils.mkdir_p(File.join(JsErb.config['path']['compiled'], 'locales'))
31
+ FileUtils.mkdir_p(File.join(JsErb.config['path']['compiled'], 'views'))
32
+ end
33
+
34
+ #
35
+ # Renders app/javascripts.
36
+ #
37
+ def compile_source_files
38
+ Dir.glob("#{JsErb.config['path']['source']}/*.jserb").each do |file|
39
+ name = File.basename(file, '.jserb')
40
+ content = ERB.new(IO.read(file)).result
41
+ File.open("#{JsErb.config['path']['compiled']}/#{name}.js" , "wb") do |f|
42
+ f.write(content)
43
+ end
44
+ end
45
+
46
+ Dir.glob("#{JsErb.config['path']['source']}/*.js").each do |file|
47
+ FileUtils.cp(file, JsErb.config['path']['compiled'])
48
+ end
49
+ end
50
+
51
+ #
52
+ # Renders config/locales.
53
+ #
54
+ def compile_locales
55
+ Dir.glob("#{JsErb.config['path']['locales']}/*.js.yml").each do |file|
56
+ name = File.basename(file, '.js.yml')
57
+ content = YAML.load(ERB.new(File.read(file)).result).to_json
58
+ File.open(File.join(JsErb.config['path']['compiled'], 'locales', "#{name}.js"), "wb") do |f|
59
+ f.write("var i18n = #{content};")
60
+ end
61
+ end
62
+ end
63
+
64
+ #
65
+ # Renders app/views.
66
+ #
67
+ def compile_views
68
+ helper = ActionView::Base.new
69
+ list = {}
70
+
71
+ JsErb.config['views'].each do |file|
72
+ ext = File.extname(file)
73
+ source = File.read(File.join(JsErb.config['path']['views'], file))
74
+ key = path_key(file)
75
+
76
+ case ext.downcase
77
+ when '.haml'
78
+ list[key] = Haml::Engine.new(source).render(helper)
79
+ when '.erb'
80
+ list[key] = ERB.new(source).result(binding)
81
+ else
82
+ list[key] = source
83
+ end
84
+ end
85
+
86
+ File.open(File.join(JsErb.config['path']['compiled'], 'views', 'jserb.js'), "wb") do |f|
87
+ f.write("var jserb = #{list.to_json};")
88
+ end
89
+ end
90
+
91
+ #
92
+ # Return the path as a key.
93
+ #
94
+ def path_key(file)
95
+ ext = File.extname(file)
96
+ key = file[0..-ext.length-1]
97
+ key = key.split(/\/|\\/).join('.').gsub('._', '.')
98
+ ext = File.extname(key)
99
+ key = key[0..-ext.length-1] if ext == '.html'
100
+ key
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,9 @@
1
+ module JsErb
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/js_erb.rb ADDED
@@ -0,0 +1,29 @@
1
+ module JsErb
2
+ mattr_accessor :config
3
+
4
+ #
5
+ # Runs the compile process.
6
+ #
7
+ def self.compile!
8
+ self.config = YAML.load(ERB.new(File.read(File.join(Rails.root, 'config', 'js_erb.yml'))).result)
9
+ JsErb::Controller.new.compile!
10
+ end
11
+
12
+ end
13
+
14
+ #
15
+ # Load dependencies
16
+ #
17
+ %w( js_erb/controller
18
+ js_erb/version
19
+ ).each do |lib|
20
+ require File.join(File.dirname(__FILE__), lib)
21
+ end
22
+
23
+ #
24
+ # This will add a before_filter to the ActionController which will
25
+ # run the compile process.
26
+ #
27
+ ActionController::Base.class_eval do
28
+ append_before_filter { JsErb.compile! }
29
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class JsErbTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js_erb
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kristijan Sedlak
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-05 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Javascript should be located under the RAILS/app directory instead of RAILS/public. Javascript usually produces at least a small piece of HTML code thus the Javascript templating is needed. Sites also require translations. Finally there are cases when you really need to put some ruby code into javascript. These are the features of the JsErb plugin.
23
+ email: xpepermint@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - MIT-LICENSE
31
+ - CHANGELOG.rdoc
32
+ files:
33
+ - Rakefile
34
+ - lib/js_erb/controller.rb
35
+ - lib/js_erb/version.rb
36
+ - lib/js_erb.rb
37
+ - test/js_erb_test.rb
38
+ - test/test_helper.rb
39
+ - README.rdoc
40
+ - MIT-LICENSE
41
+ - CHANGELOG.rdoc
42
+ has_rdoc: true
43
+ homepage: http://github.com/xpepermint/js_erb
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.rdoc
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: JsErb - Javascript renderer for Ruby on Rails.
78
+ test_files: []
79
+