dynamic_styles 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/dynamic_styles.gemspec +24 -0
- data/lib/dynamic_styles.rb +4 -0
- data/lib/dynamic_styles/asset_compiler.rb +39 -0
- data/lib/dynamic_styles/mount.rb +22 -0
- data/lib/dynamic_styles/stylesheet.rb +75 -0
- data/lib/dynamic_styles/version.rb +3 -0
- data/spec/stylesheet_spec.rb +76 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf88a1d69f770729207432861e58206560fd50eb
|
4
|
+
data.tar.gz: 665e5a90e1190b73ef1aaa5b1676ba14e03efc12
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7d0da25b2896086a11b5ad4b39cb2c0d95a6511cf98d5e07e1c3f2775b331ab2d2b98958d0c8495f173b21fc61224eb98a5994700cfce8369ce0ccbebb69d0e
|
7
|
+
data.tar.gz: 370792e8a85d17f3546be84874e9689c84055906ac7e4fa4bebdfd99b6361b7ecadb3570b0ca73fc8e2b7f588fcb5f1b70945f9405de711bbba322c322496e4b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Igor Davydov
|
2
|
+
|
3
|
+
MIT License
|
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
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# DynamicStyles
|
2
|
+
|
3
|
+
This little gem add ability for your AR model to have a custom stylesheet, which plays nicely with asset pipeline.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dynamic_styles'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dynamic_styles
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Let's say you have a multi-tennant app, and tennants have the ability to modify their styles, which are attributes of Layout AR model.
|
22
|
+
|
23
|
+
Just add mount_stylesheet to your Layout model and you'll be able to use layout.scss.erb (the template name is the same as your model) which is gets rendered to assets/stylesheets/layout/filename.css. Filename is created with Layout model timestamp, so this template will be re-rendered when Layout instance gets updated.
|
24
|
+
|
25
|
+
In the template you will have 'layout' variable you can use to get the attributes needed. You will need to have some method like current_layout with the instance of you Layout model. To insert link to custom stylesheet in the application.html.erb file just use
|
26
|
+
|
27
|
+
```
|
28
|
+
<link href="<%= @current_layout.dynamic_stylesheet.path %>" media="screen" rel="stylesheet">
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dynamic_styles/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dynamic_styles"
|
8
|
+
spec.version = DynamicStyles::VERSION
|
9
|
+
spec.authors = ["Igor Davydov"]
|
10
|
+
spec.email = ["iskiche@gmail.com"]
|
11
|
+
spec.description = "Use dynamic stylesheets compatible with asset pipeline"
|
12
|
+
spec.summary = "Dynamic stylesheets for rails apps"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module DynamicStyles
|
2
|
+
class AssetCompiler
|
3
|
+
def initialize(asset_path, scss_path=nil)
|
4
|
+
@asset_path = asset_path
|
5
|
+
@scss_path = Rails.root.join('app', 'assets', 'stylesheets', scss_path)
|
6
|
+
@env = Rails.application.assets.instance_variable_get('@environment') if precompilation?
|
7
|
+
end
|
8
|
+
|
9
|
+
def precompilation?
|
10
|
+
!Rails.application.config.assets.compile
|
11
|
+
end
|
12
|
+
|
13
|
+
def compiled?
|
14
|
+
if precompilation?
|
15
|
+
@env.find_asset(@asset_path).present?
|
16
|
+
else
|
17
|
+
File.exists?(@scss_path) && !File.zero?(@scss_path)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def digest
|
22
|
+
@env.find_asset(@asset_path).digest
|
23
|
+
end
|
24
|
+
|
25
|
+
def manifest_path
|
26
|
+
File.join(Rails.public_path, Rails.application.config.assets.prefix)
|
27
|
+
end
|
28
|
+
|
29
|
+
def compile!
|
30
|
+
if precompilation?
|
31
|
+
Sprockets::Manifest.new(@env, self.manifest_path).compile(@asset_path)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def write_template!(string)
|
36
|
+
File.open(@scss_path, 'w') { |f| f.write(string) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DynamicStyles
|
2
|
+
module Mount
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def mount_stylesheet(options = {})
|
10
|
+
include DynamicStyles::Mount::LocalInstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module LocalInstanceMethods
|
15
|
+
def dynamic_stylesheet
|
16
|
+
DynamicStyles::Stylesheet.new(self)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Base.send :include, DynamicStyles::Mount
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
3
|
+
module DynamicStyles
|
4
|
+
class Stylesheet
|
5
|
+
|
6
|
+
delegate :precompilation?, :digest, :compiled?, :compile!, :write_template!, :to => :compiler
|
7
|
+
|
8
|
+
def initialize(layout)
|
9
|
+
@layout = layout
|
10
|
+
end
|
11
|
+
|
12
|
+
def compiler
|
13
|
+
AssetCompiler.new(asset_path, scss_path)
|
14
|
+
end
|
15
|
+
|
16
|
+
def public_path
|
17
|
+
precompilation? ? digested_path : undigested_path
|
18
|
+
end
|
19
|
+
|
20
|
+
def base_path
|
21
|
+
"#{template_name.pluralize}/#{base_filename}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def asset_path
|
25
|
+
"#{base_path}.css"
|
26
|
+
end
|
27
|
+
|
28
|
+
def scss_path
|
29
|
+
"#{base_path}.scss"
|
30
|
+
end
|
31
|
+
|
32
|
+
def undigested_path
|
33
|
+
"/assets/#{base_path}.css"
|
34
|
+
end
|
35
|
+
|
36
|
+
def digested_path
|
37
|
+
"/assets/#{base_path}-#{digest}.css"
|
38
|
+
end
|
39
|
+
|
40
|
+
def template_name
|
41
|
+
@layout.class.to_s.downcase.split('::').last
|
42
|
+
end
|
43
|
+
|
44
|
+
def base_filename
|
45
|
+
[
|
46
|
+
@layout.id,
|
47
|
+
@layout.updated_at.to_s(:number),
|
48
|
+
template_name
|
49
|
+
].join('-')
|
50
|
+
end
|
51
|
+
|
52
|
+
def path
|
53
|
+
compile
|
54
|
+
public_path
|
55
|
+
end
|
56
|
+
|
57
|
+
def compile
|
58
|
+
unless compiled?
|
59
|
+
write_template!(render_template)
|
60
|
+
compile!
|
61
|
+
# cleanup
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def render_template
|
66
|
+
class_name = @layout.class.to_s
|
67
|
+
controller = (class_name.pluralize + "Controller").constantize
|
68
|
+
controller.new.render_to_string template_name,
|
69
|
+
formats: [:scss],
|
70
|
+
layout: false,
|
71
|
+
locals: { :"#{class_name.downcase}" => @layout }
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'dynamic_styles/asset_compiler'
|
2
|
+
require 'dynamic_styles/stylesheet'
|
3
|
+
|
4
|
+
module DynamicStyles
|
5
|
+
describe Stylesheet do
|
6
|
+
let(:site) { double 'Site' }
|
7
|
+
let(:stylesheet) { Stylesheet.new(site) }
|
8
|
+
let(:compiler) { double 'AssetCompiler'}
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
stylesheet.stub(:base_filename) {'base_name'}
|
12
|
+
stylesheet.stub(:template_name) {'site'}
|
13
|
+
AssetCompiler.stub(:new) { @compiler }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "#base_filename is timestamped with parent model" do
|
17
|
+
stylesheet.unstub(:base_filename)
|
18
|
+
site.stub(:id) {'-1'}
|
19
|
+
site.stub(:updated_at) { Time.new 12345 }
|
20
|
+
stylesheet.base_filename.should == "#{site.id}-123450101000000-site"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "#asset_path returns relative path from assets dir" do
|
24
|
+
stylesheet.asset_path.should == "sites/#{stylesheet.base_filename}.css"
|
25
|
+
end
|
26
|
+
|
27
|
+
context "assets not precompiled" do
|
28
|
+
before :each do
|
29
|
+
stylesheet.stub(:precompilation?) { false }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "#public_path returns usual asset path in development" do
|
33
|
+
stylesheet.public_path.should == "/assets/sites/#{stylesheet.base_filename}.css"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "assets are precompiled" do
|
38
|
+
before :each do
|
39
|
+
stylesheet.stub(:precompilation?) { true }
|
40
|
+
end
|
41
|
+
|
42
|
+
it "#public_filename is digested" do
|
43
|
+
compiler = double 'AssetCompiler'
|
44
|
+
AssetCompiler.stub(:new) { compiler }
|
45
|
+
compiler.stub(:digest) { 987654321 }
|
46
|
+
stylesheet.public_path.should == "/assets/sites/#{stylesheet.base_filename}-987654321.css"
|
47
|
+
end
|
48
|
+
it "asset is recompiled if parent changed" do
|
49
|
+
stylesheet.stub(:render_template) { 'asdf'}
|
50
|
+
stylesheet.stub(:compiled?) { false }
|
51
|
+
stylesheet.should_receive(:write_template!)
|
52
|
+
stylesheet.should_receive(:compile!)
|
53
|
+
stylesheet.compile
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "compilation:" do
|
58
|
+
before(:each) do
|
59
|
+
@compiler = double 'AssetCompiler'
|
60
|
+
AssetCompiler.stub(:new) { @compiler }
|
61
|
+
end
|
62
|
+
|
63
|
+
it "#compile! compiles with manifest" do
|
64
|
+
@compiler.should_receive(:compile!)
|
65
|
+
stylesheet.compile!
|
66
|
+
end
|
67
|
+
|
68
|
+
it "#render_template renders and saves to file" do
|
69
|
+
@compiler.should_receive(:write_template!)
|
70
|
+
stylesheet.write_template!
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_styles
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Davydov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.13'
|
55
|
+
description: Use dynamic stylesheets compatible with asset pipeline
|
56
|
+
email:
|
57
|
+
- iskiche@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- dynamic_styles.gemspec
|
68
|
+
- lib/dynamic_styles.rb
|
69
|
+
- lib/dynamic_styles/asset_compiler.rb
|
70
|
+
- lib/dynamic_styles/mount.rb
|
71
|
+
- lib/dynamic_styles/stylesheet.rb
|
72
|
+
- lib/dynamic_styles/version.rb
|
73
|
+
- spec/stylesheet_spec.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Dynamic stylesheets for rails apps
|
98
|
+
test_files:
|
99
|
+
- spec/stylesheet_spec.rb
|