Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Turn Serenade for Rails into Serenade for Ruby

- Allows usage with Ruby on Rails asset pipeline (serenade/rails)
- Allows usage with Sprockets-based apps e.g. sinatra, middleman (serenade/sprockets)
- Allows usage of the Serenade renderer from any Ruby library
  • Loading branch information...
commit 6fa421be468e9abf7eb2576e94a1a7a09b33383f 1 parent 221deeb
Kim Burgestrand Burgestrand authored
2  .rspec
... ... @@ -0,0 +1,2 @@
  1 +-fp
  2 +--colour
3  Gemfile
... ... @@ -1,4 +1,3 @@
1   -source 'http://rubygems.org'
  1 +source "https://rubygems.org"
2 2
3   -# Specify your gem's dependencies in serenade_rails.gemspec
4 3 gemspec
80 README.md
Source Rendered
... ... @@ -1,54 +1,83 @@
1   -# Serenade.js for Ruby on Rails
  1 +# Serenade.js for Ruby
2 2
3   -[Serenade.js] is a JavaScript client side MVC framework. This gem makes it
4   -easy to use Serenade.js with the Ruby on Rails Asset Pipeline in Rails
5   -3.1 and later.
  3 +[Serenade.js] is a JavaScript client side MVC framework. This gem makes Serenade.js
  4 +available for usage with [Sprockets], or the asset pipeline in Ruby on Rails version
  5 +3.1 or later.
6 6
7   -Add it to the assets group in your Gemfile:
  7 +[serenade.js]: https://github.com/elabs/serenade.js
  8 +[sprockets]: http://rubygems.org/gems/sprockets
  9 +
  10 +## Installation
  11 +
  12 +To use Serenade with Rails, add Serenade to the assets group in your Gemfile:
8 13
9   -``` ruby
  14 +```
10 15 group :assets do
11   - gem 'serenade_rails'
  16 + gem "serenade", :require => "serenade/rails"
12 17 end
13 18 ```
14 19
15   -You can now require Serenade in your `application.js` file, or wherever
16   -you prefer:
  20 +You can now require Serenade in your `app/assets/javascripts/application.js` file:
17 21
18   -``` javascript
  22 +```
19 23 //= require serenade
20 24 ```
21 25
22   -Serenade should now be loaded and ready. See the [Serenade README][readme] for
23   -examples.
  26 +Serenade should now be loaded and ready. See the [Serenade README] for examples.
  27 +
  28 +[Serenade README]: https://github.com/elabs/serenade.js/blob/master/README.md
24 29
25   -## Views
  30 +### Using Serenade with Sprockets
26 31
27   -You can also easily use views from the asset pipeline. Just use the extension
28   -`.serenade` on your views, for example, place something like this in
29   -`app/assets/javascripts/test.serenade`:
  32 +If you are not using Rails, you can still use Serenade with just Sprockets:
30 33
31 34 ```
32   -h1 "Hello world"
  35 +gem "serenade", :require => "serenade/sprockets"
  36 +```
  37 +
  38 +You’ll also need to register the Serenade asset path with your Sprockets environment.
  39 +
  40 +```
  41 +sprockets.append_path Serenade::ASSET_PATH
  42 +```
  43 +
  44 +## Serenade.js views and the asset pipeline
  45 +
  46 +Serenade.js allows Sprockets or the Rails asset pipeline to compile your Serenade
  47 +views for you, just use the extension `.serenade` on any asset file. Your views
  48 +will be precompiled server-side before being served to the client.
  49 +
  50 +For example, if you create the following view in `app/assets/javascripts/test.serenade`:
  51 +
  52 +```
  53 +h1 "Hello " @name
33 54 ```
34 55
35 56 Now you can render this view:
36 57
37   -``` javascript
38   -document.appendChild(Serenade.render('test'));
  58 +```
  59 +var model = new Serenade.Model({ name: "Vega" })
  60 +document.body.appendChild(Serenade.render('test', model));
39 61 ```
40 62
41   -Couldn't be simpler!
  63 +And, like in all Serenade.js views, you can change your model and see the DOM
  64 +update itself accordingly:
42 65
43   -If the path to your view starts with `views`, that initial part is stripped
44   -off, so you could have placed the above view in
45   -`app/assets/javascripts/views/test.serenade` without changing the code.
  66 +```
  67 +model.name = "Mercedes" // HTML now says <h1>Hello Mercedes</h1>
  68 +```
  69 +
  70 +### A note about the view path
  71 +
  72 +If the path to your view starts with `views`, that initial part is stripped off,
  73 +so you could have placed the above view in `app/assets/javascripts/views/test.serenade`
  74 +without changing the code.
46 75
47 76 ## License
48 77
49 78 (The MIT License)
50 79
51   -Copyright (c) 2012 Jonas Nicklas
  80 +Copyright (c) 2012 Jonas Nicklas, Kim Burgestrand
52 81
53 82 Permission is hereby granted, free of charge, to any person obtaining
54 83 a copy of this software and associated documentation files (the
@@ -68,6 +97,3 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68 97 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69 98 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70 99 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71   -
72   -[readme]: https://github.com/elabs/serenade.js/blob/master/README.md
73   -[serenade.js]: https://github.com/elabs/serenade.js
14 Rakefile
... ... @@ -1,2 +1,12 @@
1   -#!/usr/bin/env rake
2   -require "bundler/gem_tasks"
  1 +begin
  2 + require "bundler/gem_tasks"
  3 +rescue LoadError
  4 + # Only gem pushers need bundler tasks.
  5 +end
  6 +
  7 +require "rspec/core/rake_task"
  8 +RSpec::Core::RakeTask.new("spec") do |task|
  9 + task.ruby_opts = "-W2"
  10 +end
  11 +
  12 +task :default => :spec
0  lib/assets/javascripts/serenade.js → assets/javascripts/serenade.js
File renamed without changes
2  lib/serenade.rb
... ... @@ -0,0 +1,2 @@
  1 +require "serenade/version"
  2 +require "serenade/renderer"
7 lib/serenade/rails.rb
... ... @@ -0,0 +1,7 @@
  1 +require "serenade/sprockets"
  2 +require "rails/engine"
  3 +
  4 +module Serenade
  5 + class Engine < ::Rails::Engine
  6 + end
  7 +end
26 lib/serenade/renderer.rb
... ... @@ -0,0 +1,26 @@
  1 +require "execjs"
  2 +require "multi_json"
  3 +
  4 +module Serenade
  5 + ASSET_PATH = File.expand_path("../../assets/javascripts", File.dirname(__FILE__))
  6 + SERENADEJS_PATH = File.join(ASSET_PATH, "serenade.js")
  7 +
  8 + class Renderer
  9 + attr_reader :name, :content
  10 +
  11 + def initialize(name, content)
  12 + @name = name
  13 + @content = content
  14 + end
  15 +
  16 + def parse
  17 + context = ExecJS.compile(File.read(SERENADEJS_PATH))
  18 + code = "Serenade.view(#{MultiJson.dump(content)}).parse()"
  19 + context.eval(code)
  20 + end
  21 +
  22 + def render
  23 + "Serenade.view(#{MultiJson.dump(name)}, #{MultiJson.dump(parse)});"
  24 + end
  25 + end
  26 +end
4 lib/serenade/sprockets.rb
... ... @@ -0,0 +1,4 @@
  1 +require "serenade/template"
  2 +require "sprockets"
  3 +
  4 +Sprockets.register_engine(".serenade", Serenade::Template)
18 lib/serenade/template.rb
... ... @@ -0,0 +1,18 @@
  1 +require "serenade"
  2 +require "tilt"
  3 +
  4 +module Serenade
  5 + class Template < Tilt::Template
  6 + def self.default_mime_type
  7 + "application/javascript"
  8 + end
  9 +
  10 + def prepare
  11 + end
  12 +
  13 + def evaluate(scope, locals, &block)
  14 + name = scope.logical_path.gsub(/^views\//, "")
  15 + Serenade::Renderer.new(name, data.dup).render
  16 + end
  17 + end
  18 +end
3  lib/serenade/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module Serenade
  2 + VERSION = "0.1.0"
  3 +end
7 lib/serenade_rails.rb
... ... @@ -1,7 +0,0 @@
1   -require 'serenade_rails/version'
2   -require 'serenade_rails/processor'
3   -require 'serenade_rails/engine'
4   -require 'serenade_rails/template'
5   -
6   -module SerenadeRails
7   -end
4 lib/serenade_rails/engine.rb
... ... @@ -1,4 +0,0 @@
1   -module SerenadeRails
2   - class Engine < ::Rails::Engine
3   - end
4   -end
21 lib/serenade_rails/processor.rb
... ... @@ -1,21 +0,0 @@
1   -require 'sprockets'
2   -require 'sprockets/engines'
3   -require 'tilt'
4   -
5   -module SerenadeRails
6   - class Processor < Tilt::Template
7   - def self.default_mime_type
8   - 'application/javascript'
9   - end
10   -
11   - def prepare
12   - end
13   -
14   - def evaluate(scope, locals, &block)
15   - name = scope.logical_path.gsub(/^views\//, "")
16   - SerenadeRails::Template.new(name, data.dup).render
17   - end
18   - end
19   -end
20   -
21   -Sprockets.register_engine '.serenade', SerenadeRails::Processor
25 lib/serenade_rails/template.rb
... ... @@ -1,25 +0,0 @@
1   -require 'execjs'
2   -require 'active_support/json'
3   -
4   -module SerenadeRails
5   - SERENADE_PATH = File.expand_path('../assets/javascripts/serenade.js', File.dirname(__FILE__))
6   -
7   - class Template
8   - attr_reader :name, :content
9   -
10   - def initialize(name, content)
11   - @name = name
12   - @content = content
13   - end
14   -
15   - def parse
16   - context = ExecJS.compile(File.read(SERENADE_PATH))
17   - code = "Serenade.view(#{content.to_json}).parse()"
18   - context.eval(code)
19   - end
20   -
21   - def render
22   - "Serenade.view(#{name.to_json}, #{parse.to_json});"
23   - end
24   - end
25   -end
3  lib/serenade_rails/version.rb
... ... @@ -1,3 +0,0 @@
1   -module SerenadeRails
2   - VERSION = "0.2.1"
3   -end
27 serenade.gemspec
... ... @@ -0,0 +1,27 @@
  1 +# -*- encoding: utf-8 -*-
  2 +lib = File.expand_path('../lib', __FILE__)
  3 +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
  4 +require "serenade/version"
  5 +
  6 +Gem::Specification.new do |gem|
  7 + gem.name = "serenade"
  8 + gem.version = Serenade::VERSION
  9 +
  10 + gem.authors = ["Jonas Nicklas", "Kim Burgestrand"]
  11 + gem.email = ["jnicklas@gmail.com", "kim@burgestrand.se"]
  12 + gem.license = "MIT License"
  13 +
  14 + gem.summary = "Serenade.js for Ruby, Rails, and Sprockets"
  15 + gem.homepage = "https://github.com/elabs/serenade"
  16 + gem.description = <<-TEXT
  17 +- Use serenade.js with the Rails asset pipeline.
  18 +- Use serenade.js with any sprockets application (middlemanapp, sinatra).
  19 +- Prerender serenade.js views server-side, before it even reaches the client.
  20 +TEXT
  21 +
  22 + gem.add_dependency "execjs", ">= 0.3.0"
  23 + gem.add_dependency "multi_json"
  24 + gem.add_development_dependency "rspec", "~> 2.0"
  25 + gem.add_development_dependency "sprockets", "~> 2.0"
  26 + gem.add_development_dependency "rails", "~> 3.1"
  27 +end
21 serenade_rails.gemspec
... ... @@ -1,21 +0,0 @@
1   -# -*- encoding: utf-8 -*-
2   -require File.expand_path('../lib/serenade_rails/version', __FILE__)
3   -
4   -Gem::Specification.new do |gem|
5   - gem.authors = ["Jonas Nicklas"]
6   - gem.email = ["jonas.nicklas@gmail.com"]
7   - gem.description = %q{Use Serenade.js within the Rails asset pipeline}
8   - gem.summary = %q{Serenade.js for Rails}
9   - gem.homepage = ""
10   -
11   - gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12   - gem.files = `git ls-files`.split("\n")
13   - gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14   - gem.name = "serenade_rails"
15   - gem.require_paths = ["lib"]
16   - gem.version = SerenadeRails::VERSION
17   -
18   - gem.add_dependency "rails", "~> 3.1"
19   - gem.add_dependency "execjs", ">= 0.3.0"
20   - gem.add_development_dependency "rspec", "~> 2.0"
21   -end
16 spec/serenade_spec.rb
... ... @@ -0,0 +1,16 @@
  1 +require "serenade"
  2 +
  3 +describe Serenade do
  4 + specify { defined?(Serenade::VERSION).should_not be_nil }
  5 +
  6 + describe Serenade::Renderer do
  7 + describe "#parse" do
  8 + it "returns a parsed Sereande template" do
  9 + result = Serenade::Renderer.new("foo", 'h1 "Hello world"').parse
  10 + result["name"].should eq "h1"
  11 + result["children"][0]["type"].should eq "text"
  12 + result["children"][0]["value"].should eq "Hello world"
  13 + end
  14 + end
  15 + end
  16 +end
14 spec/sprockets_spec.rb
... ... @@ -0,0 +1,14 @@
  1 +require "serenade/sprockets"
  2 +
  3 +describe "Serenade sprockets integration" do
  4 + it "registers the .serenade template engine" do
  5 + Sprockets.engines[".serenade"].should eq Serenade::Template
  6 + end
  7 +
  8 + it "allows you to require serenade in your assets" do
  9 + env = Sprockets::Environment.new
  10 + env.append_path Serenade::ASSET_PATH
  11 +
  12 + File.exists?(env.resolve "serenade").should be_true
  13 + end
  14 +end
12 spec/template_spec.rb
... ... @@ -1,12 +0,0 @@
1   -require "serenade_rails/template"
2   -
3   -describe SerenadeRails::Template do
4   - describe "#parse" do
5   - it "returns a parsed Sereande template" do
6   - result = SerenadeRails::Template.new("foo", 'h1 "Hello world"').parse
7   - result["name"].should == "h1"
8   - result["children"][0]["type"].should == "text"
9   - result["children"][0]["value"].should == "Hello world"
10   - end
11   - end
12   -end

0 comments on commit 6fa421b

Please sign in to comment.
Something went wrong with that request. Please try again.