cwninja-dynamic_sass 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +19 -0
- data/app/controllers/dynamic_sass/stylesheets_controller.rb +24 -0
- data/config/routes.rb +3 -0
- data/rails/init.rb +3 -0
- data/test/dynamic_sass_test.rb +57 -0
- data/test/test_helper.rb +33 -0
- metadata +80 -0
data/README.markdown
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Dynamic SASS
|
2
|
+
|
3
|
+
Dynamic SASS is a Rails Engine (so only rails 2.3 and above) that renders your SASS templates on each request.
|
4
|
+
This is handy for platforms like heroku which have a read only file system, but run behind a caching proxy server anyway.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
1. Install the plugin, you should know how to do that by now.
|
9
|
+
2. Make sure it loads immediately after haml (it has to undo some of HAML's work).
|
10
|
+
|
11
|
+
## Enforcing the load order
|
12
|
+
|
13
|
+
Add the following line into your environment.rb near the end:
|
14
|
+
|
15
|
+
config.plugins = [:haml, :dynamic_sass, :all]
|
16
|
+
|
17
|
+
And your done.
|
18
|
+
|
19
|
+
You should ensure that all generated css is removed when testing, and is not generated during testing.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module DynamicSass
|
2
|
+
class StylesheetsController < ActionController::Base
|
3
|
+
def show
|
4
|
+
stylesheet = params[:stylesheet]
|
5
|
+
unless stylesheet.present? and stylesheet =~ /^[0-9a-z\-_]+$/
|
6
|
+
render :text => "Bad stylesheet.", :status => :bad_request
|
7
|
+
return
|
8
|
+
end
|
9
|
+
|
10
|
+
sass_path = File.join(Rails.root, "public", "stylesheets", "sass", "#{stylesheet}.sass")
|
11
|
+
|
12
|
+
unless File.exists?(sass_path)
|
13
|
+
render :text => "No such stylesheet here.", :status => :not_found
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
if stale?(:last_modified => File.mtime(sass_path).utc)
|
18
|
+
expires_in 24.hours, :public => true
|
19
|
+
sass_engine = Sass::Engine.new(File.read(sass_path))
|
20
|
+
render :text => sass_engine.render, :content_type => "text/css"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/config/routes.rb
ADDED
data/rails/init.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class DynamicSassTest < ActionController::TestCase
|
4
|
+
tests DynamicSass::StylesheetsController
|
5
|
+
|
6
|
+
PUBLIC_PATH = File.expand_path(File.dirname(__FILE__) + "/../public")
|
7
|
+
SASS_PATH = "#{PUBLIC_PATH}/stylesheets/sass"
|
8
|
+
|
9
|
+
should_route :get, "/stylesheets/foobar.css", {:controller => "dynamic_sass/stylesheets", :action => "show", :stylesheet => "foobar"}
|
10
|
+
|
11
|
+
context "we have a stylesheet template" do
|
12
|
+
setup do
|
13
|
+
FileUtils.mkdir_p SASS_PATH
|
14
|
+
File.open("#{SASS_PATH}/test.sass", "w"){|f| f << "div\n :foo bar\n"}
|
15
|
+
end
|
16
|
+
|
17
|
+
teardown do
|
18
|
+
FileUtils.rm PUBLIC_PATH + "/stylesheets/sass/test.sass"
|
19
|
+
FileUtils.rmdir PUBLIC_PATH + "/stylesheets/sass"
|
20
|
+
FileUtils.rmdir PUBLIC_PATH + "/stylesheets"
|
21
|
+
FileUtils.rmdir PUBLIC_PATH
|
22
|
+
end
|
23
|
+
|
24
|
+
context "and it is requested" do
|
25
|
+
setup do
|
26
|
+
get :show, :stylesheet => "test"
|
27
|
+
end
|
28
|
+
|
29
|
+
should_render_without_layout
|
30
|
+
should_respond_with :success
|
31
|
+
|
32
|
+
should "render a stylesheet" do
|
33
|
+
assert_equal "div {\n foo: bar; }\n", @response.body
|
34
|
+
end
|
35
|
+
|
36
|
+
should "have correct cache control headers" do
|
37
|
+
assert_equal "max-age=86400, public", @response["Cache-Control"]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "and another stylesheet is requested" do
|
42
|
+
setup do
|
43
|
+
get :show, :stylesheet => "foo"
|
44
|
+
end
|
45
|
+
|
46
|
+
should_respond_with :missing
|
47
|
+
end
|
48
|
+
|
49
|
+
context "and an invalid stylesheet is requested" do
|
50
|
+
setup do
|
51
|
+
get :show, :stylesheet => "foo bar"
|
52
|
+
end
|
53
|
+
|
54
|
+
should_respond_with :bad_request
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
RAILS_ENV = ENV["RAILS_ENV"] = "test"
|
2
|
+
RAILS_ROOT = File.join(File.dirname(__FILE__), "..")
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
|
6
|
+
gem "rails"
|
7
|
+
require "initializer"
|
8
|
+
|
9
|
+
Rails::Initializer
|
10
|
+
|
11
|
+
module Rails
|
12
|
+
def vendor_rails?
|
13
|
+
false
|
14
|
+
end
|
15
|
+
class Initializer
|
16
|
+
def load_environment
|
17
|
+
end
|
18
|
+
end
|
19
|
+
extend self
|
20
|
+
end
|
21
|
+
|
22
|
+
require "active_support"
|
23
|
+
require "action_controller"
|
24
|
+
require "haml"
|
25
|
+
require "sass"
|
26
|
+
|
27
|
+
Rails::Initializer.run(:set_load_path)
|
28
|
+
|
29
|
+
require 'test_help'
|
30
|
+
require "shoulda"
|
31
|
+
require "dynamic_sass/stylesheets_controller"
|
32
|
+
require File.join(File.dirname(__FILE__), *%w[.. config routes])
|
33
|
+
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cwninja-dynamic_sass
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Lea
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-28 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: haml
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: shoulda
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: contrib@tomlea.co.uk
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.markdown
|
43
|
+
files:
|
44
|
+
- README.markdown
|
45
|
+
- test/dynamic_sass_test.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
- rails/init.rb
|
48
|
+
- config/routes.rb
|
49
|
+
- app/controllers
|
50
|
+
- app/controllers/dynamic_sass
|
51
|
+
- app/controllers/dynamic_sass/stylesheets_controller.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://cwninja.com/
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --main
|
57
|
+
- README.markdown
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.2.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 2
|
78
|
+
summary: Plugin to generate SASS on the fly instead of before each request.
|
79
|
+
test_files: []
|
80
|
+
|