wbzyl-sinatra-static-assets 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *~
3
+ \#*
4
+ .\#*
5
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Wlodek Bzyl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,81 @@
1
+ # Static Assets Sinatra Extension
2
+
3
+ To install it, run:
4
+
5
+ sudo gem install wbzyl-sinatra-static-assets -s http://gems.github.com
6
+
7
+ To use, include it in a Sinatra application:
8
+
9
+ require 'rubygems'
10
+ gem 'wbzyl-sinatra-static-assets'
11
+ require 'sinatra/static_assets'
12
+
13
+ If you're subclassing *Sinatra::Base*, then you need to call helpers manually:
14
+
15
+ class MyApp < Sinatra::Base
16
+ helpers Sinatra::StaticAssets
17
+ # ...
18
+ end
19
+
20
+
21
+ ## Passenger: 8.7. How to fix broken images/CSS/JavaScript URIs in sub-URI deployments
22
+
23
+ Some people experience broken images and other broken static assets
24
+ when they deploy their application to a sub-URI
25
+ (i.e. http://mysite.com/railsapp/). The reason for this usually is
26
+ that you used a static URI for your image in the views. This means
27
+ your img source probably refers to something like /images/foo.jpg. The
28
+ leading slash means that it's an absolute URI: you're telling the
29
+ browser to always load http://mysite.com/images/foo.jpg no matter
30
+ what. The problem is that the image is actually at
31
+ http://mysite.com/railsapp/images/foo.jpg. There are two ways to fix
32
+ this.
33
+
34
+ The second and highly recommended way is to always use Rails helper
35
+ methods to output tags for static assets. These helper methods
36
+ automatically take care of prepending the base URI that you've
37
+ deployed the application to. For images there is `image_tag`, for
38
+ JavaScript there is `javascript_include_tag` and for CSS there is
39
+ `stylesheet_link_tag`. In the above example you would simply remove the
40
+ `<img>` HTML tag and replace it with inline Ruby like this:
41
+
42
+ <%= image_tag("foo.jpg") %>
43
+
44
+ This will generate the proper image tag to
45
+
46
+ $RAILS_ROOT/public/images/foo.jpg
47
+
48
+ so that your images will always work
49
+ no matter what sub-URI you've deployed to.
50
+
51
+ These helper methods are more valuable than you may think. For example
52
+ they also append a timestamp to the URI to better facilitate HTTP
53
+ caching. For more information, please refer to the Rails API docs.
54
+
55
+
56
+ ## Deploying to a sub URI
57
+
58
+ Add to */etc/hosts*:
59
+
60
+ 127.0.0.1 localhost.localdomain localhost sinatra.local
61
+
62
+ Sym link the application:
63
+
64
+ ln -s ~/public_git/forks/sinatra-static-assets/examples/app1/public /srv/www/sinatra/app1
65
+ ln -s ~/public_git/forks/sinatra-static-assets/examples/app2/public /srv/www/sinatra/app2
66
+
67
+ Add to */etc/httpd/conf.d/sinatra.conf*
68
+
69
+ <VirtualHost *:80>
70
+ ServerName sinatra.local
71
+ DocumentRoot /srv/www/sinatra
72
+
73
+ RackBaseURI /app1
74
+ RackBaseURI /app2
75
+ </VirtualHost>
76
+
77
+ Goto:
78
+
79
+ http://sinatra.rails/app1/
80
+
81
+ Note the terminating slash.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require "rake/clean"
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |s|
10
+ s.name = "sinatra-static-assets"
11
+ s.summary = "Sinatra extension providing helper methods to output tags for static assets."
12
+ s.email = "matwb@univ.gda.pl"
13
+ s.homepage = "http://github.com/wbzyl/sinatra-static-assets"
14
+ s.description = s.summary
15
+ s.authors = ["Włodek Bzyl"]
16
+
17
+ s.add_dependency 'emk-sinatra-url-for', '>=0.2.1'
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available."
21
+ puts "Install it with:"
22
+ puts " sudo gem install technicalpickles-jeweler -s http://gems.github.com"
23
+ end
24
+
25
+ Rake::TestTask.new(:test) do |t|
26
+ t.libs << 'lib' << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+ desc 'Install the package as a gem.'
32
+ task :install => [:clean, :build] do
33
+ gem = Dir['pkg/*.gem'].last
34
+ sh "sudo gem install --no-rdoc --no-ri --local #{gem}"
35
+ end
36
+
37
+ task :default => :test
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 2
3
+ :major: 0
4
+ :minor: 0
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'sinatra'
5
+
6
+ gem 'sinatra-static-assets'
7
+ require 'sinatra/static_assets'
8
+
9
+ get "/" do
10
+ @title = "Tatra Mountains, Błyszcz (2159 m)"
11
+ erb :index
12
+ end
@@ -0,0 +1,9 @@
1
+ # run with: thin --rackup config.ru -p 4567 start
2
+
3
+ require 'app'
4
+
5
+ # default with passenger
6
+
7
+ use Rack::Static, :urls => ["/stylesheets", "/javascripts", "/images"], :root => "public"
8
+
9
+ run Sinatra::Application
@@ -0,0 +1 @@
1
+ /* app1.js */
@@ -0,0 +1,14 @@
1
+ html {
2
+ margin: 0;
3
+ padding: 0;
4
+ background: #ABA418 url(src/bronzed_olive.png);
5
+ }
6
+
7
+ body {
8
+ width: 600px;
9
+ margin: 1em auto;
10
+ padding: 1em 2em;
11
+ border: black solid 1px;
12
+ background-color: #D1C704;
13
+ font: normal 14px/1.6 Arial, Helvetica, sans-serif;
14
+ }
File without changes
@@ -0,0 +1,3 @@
1
+ <h2><%= @title %></h2>
2
+
3
+ <p><%= image_tag "/images/tatry1.jpg", :alt => @title, :title => @title %></p>
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
5
+
6
+ <link rel="stylesheet" href="/stylesheets/app1.css" type="text/css" media="screen" charset="utf-8">
7
+ <script src="/javascripts/app1.js" type="text/javascript"></script>
8
+
9
+ <title><%= @title %></title>
10
+ </head>
11
+
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'sinatra'
5
+
6
+ gem 'sinatra-static-assets'
7
+ require 'sinatra/static_assets'
8
+
9
+ get "/" do
10
+ @title = "Tatra Mountains, Dolina Gąsienicowa (1500 m)"
11
+ erb :index
12
+ end
@@ -0,0 +1,9 @@
1
+ # run with: thin --rackup config.ru -p 4567 start
2
+
3
+ require 'app'
4
+
5
+ # default with passenger
6
+
7
+ use Rack::Static, :urls => ["/stylesheets", "/javascripts", "/images"], :root => "public"
8
+
9
+ run Sinatra::Application
@@ -0,0 +1 @@
1
+ /* app1.js */
File without changes
@@ -0,0 +1,3 @@
1
+ <h2><%= @title %></h2>
2
+
3
+ <p><%= image_tag "/images/tatry2.jpg", :alt => @title, :title => @title %></p>
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
5
+
6
+ <link rel="stylesheet" href="/stylesheets/app2.css" type="text/css" media="screen" charset="utf-8">
7
+ <script src="/javascripts/app2.js" type="text/javascript"></script>
8
+
9
+ <title><%= @title %></title>
10
+ </head>
11
+
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,50 @@
1
+ require 'sinatra/base'
2
+
3
+ gem 'emk-sinatra-url-for'
4
+ require 'sinatra/url_for'
5
+
6
+ module Sinatra
7
+ module StaticAssets
8
+
9
+ def image_tag(source, options = {})
10
+ options[:src] = url_for(source)
11
+ tag("img", options)
12
+ end
13
+
14
+ def stylesheet_link_tag(*sources)
15
+ # TODO
16
+ end
17
+
18
+ def javascript_include_tag(*sources)
19
+ # TODO
20
+ end
21
+
22
+ private
23
+
24
+ def tag(name, options = nil, open = false)
25
+ "<#{name}#{tag_options(options) if options}#{open ? ">" : " />"}"
26
+ end
27
+
28
+ def tag_options(options)
29
+ unless options.empty?
30
+ attrs = []
31
+ attrs = options.map { |key, value| %(#{key}="#{value}") }
32
+ " #{attrs.sort * ' '}" unless attrs.empty?
33
+ end
34
+ end
35
+
36
+ # html_.. -> url_for..
37
+ # <link .. /?> ?
38
+ def stylesheet_tag(source, options)
39
+ tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen",
40
+ "href" => html_escape(path_to_stylesheet(source)) }.merge(options), false)
41
+ end
42
+
43
+ def javascript_src_tag(source, options)
44
+ # append onto tag </script>
45
+ end
46
+
47
+ end
48
+
49
+ helpers StaticAssets
50
+ end
@@ -0,0 +1,68 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{sinatra-static-assets}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["W\305\202odek Bzyl"]
9
+ s.date = %q{2009-05-22}
10
+ s.description = %q{Sinatra extension providing helper methods to output tags for static assets.}
11
+ s.email = %q{matwb@univ.gda.pl}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README.markdown"
15
+ ]
16
+ s.files = [
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "examples/app1/app.rb",
23
+ "examples/app1/config.ru",
24
+ "examples/app1/public/images/tatry1.jpg",
25
+ "examples/app1/public/javascripts/app1.js",
26
+ "examples/app1/public/stylesheets/app1.css",
27
+ "examples/app1/public/stylesheets/src/bronzed_olive.png",
28
+ "examples/app1/tmp/always_restart.txt",
29
+ "examples/app1/views/index.erb",
30
+ "examples/app1/views/layout.erb",
31
+ "examples/app2/app.rb",
32
+ "examples/app2/config.ru",
33
+ "examples/app2/public/images/tatry2.jpg",
34
+ "examples/app2/public/javascripts/app2.js",
35
+ "examples/app2/public/stylesheets/src/bronzed_olive.png",
36
+ "examples/app2/tmp/always_restart.txt",
37
+ "examples/app2/views/index.erb",
38
+ "examples/app2/views/layout.erb",
39
+ "lib/sinatra/static_assets.rb",
40
+ "sinatra-static-assets.gemspec",
41
+ "test/sinatra_static_assets_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/wbzyl/sinatra-static-assets}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.3}
48
+ s.summary = %q{Sinatra extension providing helper methods to output tags for static assets.}
49
+ s.test_files = [
50
+ "test/test_helper.rb",
51
+ "test/sinatra_static_assets_test.rb",
52
+ "examples/app2/app.rb",
53
+ "examples/app1/app.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<emk-sinatra-url-for>, [">= 0.2.1"])
62
+ else
63
+ s.add_dependency(%q<emk-sinatra-url-for>, [">= 0.2.1"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<emk-sinatra-url-for>, [">= 0.2.1"])
67
+ end
68
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class SinatraRDiscountTest < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def rdiscount_app(&block)
7
+ mock_app {
8
+ set :views, File.dirname(__FILE__) + '/views'
9
+ helpers Sinatra::RDiscount
10
+ set :show_exceptions, false
11
+ get '/', &block
12
+ }
13
+ get '/'
14
+ end
15
+
16
+ def test_renders_inline_strings
17
+ rdiscount_app { rdiscount 'hello world' }
18
+ assert last_response.ok?
19
+ assert_equal "<p>hello world</p>\n", last_response.body
20
+ end
21
+
22
+ def test_renders_inline_erb_string
23
+ rdiscount_app { rdiscount '{%= 1 + 1 %}' }
24
+ assert last_response.ok?
25
+ assert_equal "<p>2</p>\n", last_response.body
26
+ end
27
+
28
+ def test_renders_files_in_views_path
29
+ rdiscount_app { rdiscount :hello }
30
+ assert last_response.ok?
31
+ assert_equal "<h1>hello world</h1>\n", last_response.body
32
+ end
33
+
34
+ def test_takes_locals_option
35
+ rdiscount_app {
36
+ locals = {:foo => 'Bar'}
37
+ rdiscount "{%= foo %}", :locals => locals
38
+ }
39
+ assert last_response.ok?
40
+ assert_equal "<p>Bar</p>\n", last_response.body
41
+ end
42
+
43
+ def test_renders_with_inline_layouts
44
+ rdiscount_app {
45
+ rdiscount 'Sparta', :layout => 'THIS. IS. <%= yield.upcase %>'
46
+ }
47
+ assert last_response.ok?
48
+ assert_equal "THIS. IS. <P>SPARTA</P>\n", last_response.body
49
+ end
50
+
51
+ def test_renders_with_file_layouts
52
+ rdiscount_app {
53
+ rdiscount 'hello world', :layout => :layout2
54
+ }
55
+ assert last_response.ok?
56
+ assert_equal "erb layout\n<p>hello world</p>\n\n", last_response.body
57
+ end
58
+
59
+ def test_renders_erb_with_blocks
60
+ mock_app {
61
+ set :views, File.dirname(__FILE__) + '/views'
62
+ helpers Sinatra::RDiscount
63
+
64
+ def container
65
+ yield
66
+ end
67
+ def is;
68
+ "THIS. IS. SPARTA!"
69
+ end
70
+
71
+ get '/' do
72
+ rdiscount '{% container do %} {%= is %} {% end %}'
73
+ end
74
+ }
75
+
76
+ get '/'
77
+ assert last_response.ok?
78
+ assert_equal "<p> THIS. IS. SPARTA! </p>\n", last_response.body
79
+ end
80
+
81
+ def test_raises_error_if_template_not_found
82
+ mock_app {
83
+ set :views, File.dirname(__FILE__) + '/views'
84
+ helpers Sinatra::RDiscount
85
+ set :show_exceptions, false
86
+
87
+ get('/') { rdiscount :no_such_template }
88
+ }
89
+ assert_raise(Errno::ENOENT) { get('/') }
90
+ end
91
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'sinatra/rdiscount'
7
+
8
+ require 'rdiscount'
9
+
10
+ class Test::Unit::TestCase
11
+ include Rack::Test::Methods
12
+
13
+ attr_reader :app
14
+
15
+ # Sets up a Sinatra::Base subclass defined with the block
16
+ # given. Used in setup or individual spec methods to establish
17
+ # the application.
18
+ def mock_app(base=Sinatra::Base, &block)
19
+ @app = Sinatra.new(base, &block)
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbzyl-sinatra-static-assets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - "W\xC5\x82odek Bzyl"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: emk-sinatra-url-for
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.1
24
+ version:
25
+ description: Sinatra extension providing helper methods to output tags for static assets.
26
+ email: matwb@univ.gda.pl
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .gitignore
36
+ - LICENSE
37
+ - README.markdown
38
+ - Rakefile
39
+ - VERSION.yml
40
+ - examples/app1/app.rb
41
+ - examples/app1/config.ru
42
+ - examples/app1/public/images/tatry1.jpg
43
+ - examples/app1/public/javascripts/app1.js
44
+ - examples/app1/public/stylesheets/app1.css
45
+ - examples/app1/public/stylesheets/src/bronzed_olive.png
46
+ - examples/app1/tmp/always_restart.txt
47
+ - examples/app1/views/index.erb
48
+ - examples/app1/views/layout.erb
49
+ - examples/app2/app.rb
50
+ - examples/app2/config.ru
51
+ - examples/app2/public/images/tatry2.jpg
52
+ - examples/app2/public/javascripts/app2.js
53
+ - examples/app2/public/stylesheets/src/bronzed_olive.png
54
+ - examples/app2/tmp/always_restart.txt
55
+ - examples/app2/views/index.erb
56
+ - examples/app2/views/layout.erb
57
+ - lib/sinatra/static_assets.rb
58
+ - sinatra-static-assets.gemspec
59
+ - test/sinatra_static_assets_test.rb
60
+ - test/test_helper.rb
61
+ has_rdoc: false
62
+ homepage: http://github.com/wbzyl/sinatra-static-assets
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.2.0
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Sinatra extension providing helper methods to output tags for static assets.
87
+ test_files:
88
+ - test/test_helper.rb
89
+ - test/sinatra_static_assets_test.rb
90
+ - examples/app2/app.rb
91
+ - examples/app1/app.rb