active_assets 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +78 -38
- data/active_assets.gemspec +3 -1
- data/lib/active_assets/active_sprites/chunky_png_runner.rb +119 -0
- data/lib/active_assets/active_sprites/configurable.rb +14 -0
- data/lib/active_assets/active_sprites/railtie.rb +9 -0
- data/lib/active_assets/active_sprites/{runner.rb → rmagick_runner.rb} +10 -11
- data/lib/active_assets/active_sprites/sprite_piece.rb +5 -3
- data/lib/active_assets/active_sprites/sprite_stylesheet.rb +2 -2
- data/lib/active_assets/active_sprites/sprites.rb +20 -2
- data/lib/active_assets/active_sprites.rb +3 -1
- data/test/active_assets/active_sprites/sprite_test.rb +5 -2
- data/test/fixtures/rails_root/config/application.rb +1 -0
- data/test/fixtures/rails_root/config/sprites.rb +2 -2
- data/test/fixtures/rails_root/public/images/sprite_images/sprite3/1.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite3/2.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Argentina.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Australia.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Austria.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Belgium.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Brazil.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Cameroon.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Canada.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Chile.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/China.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Denmark.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Finland.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/France.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Germany.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Greece.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Hong Kong.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Hungary.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Iceland.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/India.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Indonesia.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Ireland.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Israel.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Italy.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Japan.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Malaysia.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Mexico.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/New Zealand.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Norway.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Pakistan.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Palestine.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Peru.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Poland.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Portugal.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Puerto Rico.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Romania.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Russia.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Saudi Arabia.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Singapore.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/South Africa.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/South Korea.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Spain.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Sweden.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Switzerland.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Thailand.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/The Netherlands.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Turkey.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/UK.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/USA.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Ukraine.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Uruguay.png +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Venezuela.png +0 -0
- metadata +153 -123
- data/test/fixtures/rails_root/public/images/sprite_images/sprite3/1.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite3/2.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Argentina.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Australia.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Austria.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Belgium.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Brazil.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Cameroon.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Canada.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Chile.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/China.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Denmark.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Finland.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/France.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Germany.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Greece.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Hong Kong.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Hungary.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Iceland.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/India.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Indonesia.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Ireland.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Israel.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Italy.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Japan.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Malaysia.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Mexico.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/New Zealand.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Norway.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Pakistan.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Palestine.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Peru.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Poland.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Portugal.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Puerto Rico.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Romania.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Russia.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Saudi Arabia.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Singapore.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/South Africa.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/South Korea.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Spain.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Sweden.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Switzerland.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Thailand.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/The Netherlands.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Turkey.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/UK.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/USA.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Ukraine.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Uruguay.gif +0 -0
- data/test/fixtures/rails_root/public/images/sprite_images/sprite4/Venezuela.gif +0 -0
data/README.md
CHANGED
@@ -3,12 +3,30 @@ Active Assets
|
|
3
3
|
|
4
4
|
A Railtie that provides a full asset management system, including support for development and deployment. This includes building sprites, concatenating javascript and css via expansion definitions. Active Assets includes two libraries, Active Expansions and Active Sprites.
|
5
5
|
|
6
|
+
gem install rmagick
|
7
|
+
... OR ...
|
8
|
+
gem install oily_png
|
9
|
+
... OR ...
|
10
|
+
gem install chunky_png
|
11
|
+
|
6
12
|
gem install active_assets
|
7
13
|
|
8
14
|
Gemfile
|
9
15
|
-------
|
10
16
|
|
17
|
+
...
|
11
18
|
gem 'active_assets'
|
19
|
+
...
|
20
|
+
group :development do
|
21
|
+
...
|
22
|
+
gem 'rmagick'
|
23
|
+
... OR ...
|
24
|
+
gem 'oily_png'
|
25
|
+
... OR ...
|
26
|
+
gem 'chunky_png'
|
27
|
+
...
|
28
|
+
end
|
29
|
+
...
|
12
30
|
|
13
31
|
In your rails app
|
14
32
|
-----------------
|
@@ -19,7 +37,67 @@ In your rails app
|
|
19
37
|
require 'active_assets/railtie'
|
20
38
|
...
|
21
39
|
|
40
|
+
You can also include only ActiveSprites or only ActiveExpansions in your application
|
41
|
+
|
42
|
+
### application.rb
|
43
|
+
...
|
44
|
+
require 'rails/all'
|
45
|
+
require 'active\_assets/active\_expansions/railtie'
|
46
|
+
... OR ...
|
47
|
+
require 'active\_assets/active\_sprites/railtie'
|
48
|
+
...
|
49
|
+
|
22
50
|
## The dsls
|
51
|
+
|
52
|
+
### Introduction to Active Sprites
|
53
|
+
|
54
|
+
ActiveSprites allows you to generate sprites within your Rails apps with `rake sprites`! All you need is rmagick and you are on your way. If you don't have rmagick installed, ActiveSprites will just fail silently when you try to generate the sprites. Store the images that make up your sprites in your rails project, use the dsl below to tell ActiveSprites which images to include in your sprites, the css selector the corresponds to each image in the sprite, the location to write the sprite, and the location to write the stylesheet.
|
55
|
+
|
56
|
+
#### config/sprites.rb
|
57
|
+
Rails.application.sprites do
|
58
|
+
sprite 'sprites/world_flags.png' => 'sprites/world_flags.css'
|
59
|
+
_"sprite_images/world_flags/Argentina.gif" => ".flags.argentina"
|
60
|
+
_"sprite_images/world_flags/Australia.gif" => ".flags.australia"
|
61
|
+
...
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
#### To generate
|
66
|
+
rake sprites
|
67
|
+
|
68
|
+
or
|
69
|
+
|
70
|
+
Rails.application.sprites.generate!
|
71
|
+
|
72
|
+
### More on Active Sprites
|
73
|
+
|
74
|
+
It is possible to add all of the world flags! Haha, see the following example,
|
75
|
+
|
76
|
+
Rails.application.sprites do
|
77
|
+
sprite :world_flags
|
78
|
+
Dir[Rails.root.join('public/images/sprite_images/world_flags/*.{png,gif,jpg}')].each do |path|
|
79
|
+
image_path = path[%r{^.*/public/images/(.*)$}, 1]
|
80
|
+
klass_name = ".flag.#{File.basename(image_path, File.extname(image_path)).split(' ').join('_')}"
|
81
|
+
|
82
|
+
sp image_path => klass_name
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
`_` and `sp` are aliases for `sprite_piece`
|
87
|
+
|
88
|
+
Also, you will notice that I gave a symbol for the sprite instead of a mapping. This will assume that you wish to store your sprite at `path/to/your/public/images/sprites/world_flags.png` and you wish to store your stylesheet at `path/to/your/public/stylesheets/sprites/world_flags.css`.
|
89
|
+
|
90
|
+
#### ActiveSprites configuration
|
91
|
+
Rmagick is used by default. To switch to `oily_png`, which is a c extension for `chunky_png` and has the same api as `chunky_png`, apply the configuration below.
|
92
|
+
|
93
|
+
##### config/application.rb
|
94
|
+
|
95
|
+
...
|
96
|
+
config.active_sprites.sprite_backend = :chunky_png
|
97
|
+
...
|
98
|
+
|
99
|
+
In summary, if `rmagick` is installed, it will used by default. If `oily_png` is installed AND the `sprite_backend` is set to `:chunky_png` (see below), then `oliy_png` will be used. If `oily_png` is not installed but `chunky_png` is, AND the `sprite_backend` is set to `:chunky_png`, then `chunky_png` will be used.
|
100
|
+
|
23
101
|
### Introduction to Active Expansions
|
24
102
|
|
25
103
|
ActiveExpansions allow you to register Rails javascript and stylesheet expansions via a simple dsl. Addionally, the assets in the expansion are concatenated when appropriate and the expansion delivers the concatenated (or 'cached') assets' path in the appropriate environments. Also, files can be specified as deploy only or only for a specific environment. For example, you may wish to include jQuery or Prototype src files in development and use minified libraries from cdn sources in production. This is supported.
|
@@ -91,26 +169,6 @@ ActiveExpansions allow you to register Rails javascript and stylesheet expansion
|
|
91
169
|
end
|
92
170
|
end
|
93
171
|
|
94
|
-
### Introduction to Active Sprites
|
95
|
-
|
96
|
-
ActiveSprites allows you to generate sprites within your Rails apps with `rake sprites`! All you need is rmagick and you are on your way. If you don't have rmagick installed, ActiveSprites will just fail silently when you try to generate the sprites. Store the images that make up your sprites in your rails project, use the dsl below to tell ActiveSprites which images to include in your sprites, the css selector the corresponds to each image in the sprite, the location to write the sprite, and the location to write the stylesheet.
|
97
|
-
|
98
|
-
#### config/sprites.rb
|
99
|
-
Rails.application.sprites do
|
100
|
-
sprite 'sprites/world_flags.png' => 'sprites/world_flags.css'
|
101
|
-
_"sprite_images/world_flags/Argentina.gif" => ".flags.argentina"
|
102
|
-
_"sprite_images/world_flags/Australia.gif" => ".flags.australia"
|
103
|
-
...
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
#### To generate
|
108
|
-
rake sprites
|
109
|
-
|
110
|
-
or
|
111
|
-
|
112
|
-
Rails.application.sprites.generate!
|
113
|
-
|
114
172
|
### More on Active Expansions
|
115
173
|
You can specify certain assets only be used in a deployment setting or only be used in a production setting. The example below illustrates how to include a library in development and the same library from a cdn in production. The net result will be that the library will not get cached (concatenated) along with all of the other files because it is specified for use only in development and test. Hence, the cache file will only be comprised of the other assets in the expansion. Similarly, the cdn resort will not get cached either but instead will be used directly. The resulting expansion in production will include two paths, the cdn url to jquery and the path the cache file, in this case, public/{javascript,stylesheets}/cache/global.{js,css}.
|
116
174
|
|
@@ -145,21 +203,3 @@ To enable your application to cache assets when the application is initialized,
|
|
145
203
|
...
|
146
204
|
config.active_expansions.precache_assets = true
|
147
205
|
...
|
148
|
-
|
149
|
-
### More on Active Sprites
|
150
|
-
|
151
|
-
It is possible to add all of the world flags! Haha, see the following example,
|
152
|
-
|
153
|
-
Rails.application.sprites do
|
154
|
-
sprite :world_flags
|
155
|
-
Dir[Rails.root.join('public/images/sprite_images/world_flags/*.{png,gif,jpg}')].each do |path|
|
156
|
-
image_path = path[%r{^.*/public/images/(.*)$}, 1]
|
157
|
-
klass_name = ".flag.#{File.basename(image_path, File.extname(image_path)).split(' ').join('_')}"
|
158
|
-
|
159
|
-
sp image_path => klass_name
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
`_` and `sp` are aliases for `sprite_piece`
|
164
|
-
|
165
|
-
Also, you will notice that I gave a symbol for the sprite instead of a mapping. This will assume that you wish to store your sprite at `path/to/your/public/images/sprites/world_flags.png` and you wish to store your stylesheet at `path/to/your/public/stylesheets/sprites/world_flags.css`.
|
data/active_assets.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "active_assets"
|
4
|
-
s.version = '0.2.
|
4
|
+
s.version = '0.2.3'
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ["Sam Woodard"]
|
7
7
|
s.email = ["sam@wildfireapp.com"]
|
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
|
19
|
+
s.add_development_dependency 'oily_png'
|
20
|
+
s.add_development_dependency 'chunky_png'
|
19
21
|
s.add_development_dependency 'rmagick'
|
20
22
|
s.add_development_dependency 'css_parser', '~>1.1.5'
|
21
23
|
s.add_development_dependency "rails", "~>3.0.3"
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'action_view'
|
3
|
+
require 'rack/mount'
|
4
|
+
require 'action_view'
|
5
|
+
require 'fileutils'
|
6
|
+
begin
|
7
|
+
require 'oily_png'
|
8
|
+
rescue LoadError
|
9
|
+
require 'chunky_png'
|
10
|
+
end
|
11
|
+
|
12
|
+
module ActiveAssets
|
13
|
+
module ActiveSprites
|
14
|
+
class ChunkyPngRunner
|
15
|
+
class AssetContext < ActionView::Base
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(sprites)
|
19
|
+
@sprites = if ENV['SPRITE']
|
20
|
+
sprites.select do |name, sprite|
|
21
|
+
ENV['SPRITE'].split(',').map(&:strip).any? do |sp|
|
22
|
+
# were going to be very forgiving
|
23
|
+
name == sp ||
|
24
|
+
name == sp.to_sym ||
|
25
|
+
name == ::Rack::Mount::Utils.normalize_path(sp)
|
26
|
+
end
|
27
|
+
end.map(&:last)
|
28
|
+
else
|
29
|
+
sprites.values
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate!(railtie = Rails.application, debug = ENV['DEBUG'])
|
34
|
+
p "Engine Class Name: #{railtie.class.name}" if debug
|
35
|
+
|
36
|
+
context = setup_context(railtie)
|
37
|
+
|
38
|
+
@sprites.each do |sprite|
|
39
|
+
next if sprite.sprite_pieces.empty?
|
40
|
+
sprite_path = sanitize_asset_path(context.image_path(sprite.path))
|
41
|
+
p "Sprite Path: #{sprite_path}" if debug
|
42
|
+
sprite_stylesheet_path = sanitize_asset_path(context.stylesheet_path(sprite.stylesheet_path))
|
43
|
+
p "Sprite Stylesheet Path: #{sprite_stylesheet_path}" if debug
|
44
|
+
|
45
|
+
orientation = sprite.orientation.to_s
|
46
|
+
sprite_pieces = sprite.sprite_pieces
|
47
|
+
|
48
|
+
begin
|
49
|
+
width, height = 0, 0
|
50
|
+
image_list = []
|
51
|
+
|
52
|
+
sprite_pieces.each do |sp|
|
53
|
+
sprite_piece_path = File.join(railtie.config.paths.public.to_a.first, sanitize_asset_path(context.image_path(sp.path)))
|
54
|
+
sp_image = ChunkyPNG::Image.from_file(sprite_piece_path)
|
55
|
+
image_list << sp_image
|
56
|
+
sp.details = SpritePiece::Details.new(
|
57
|
+
sprite.url.present? ? sprite.url : sprite_path,
|
58
|
+
orientation == Sprite::Orientation::VERTICAL ? 0 : width,
|
59
|
+
orientation == Sprite::Orientation::VERTICAL ? height : 0,
|
60
|
+
sp_image.width,
|
61
|
+
sp_image.height
|
62
|
+
)
|
63
|
+
|
64
|
+
width = orientation == Sprite::Orientation::HORIZONTAL ? width + sp_image.width : [width, sp_image.width].max
|
65
|
+
height = orientation == Sprite::Orientation::VERTICAL ? height + sp_image.height : [height, sp_image.height].max
|
66
|
+
end
|
67
|
+
|
68
|
+
@sprite = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
69
|
+
|
70
|
+
image_list.each_with_index do |image, i|
|
71
|
+
@sprite.replace(image, sprite_pieces[i].details.x, sprite_pieces[i].details.y)
|
72
|
+
end
|
73
|
+
|
74
|
+
stylesheet = SpriteStylesheet.new(sprite_pieces)
|
75
|
+
stylesheet.write File.join(railtie.config.paths.public.to_a.first, sprite_stylesheet_path)
|
76
|
+
write File.join(railtie.config.paths.public.to_a.first, sprite_path), sprite.quality
|
77
|
+
ensure
|
78
|
+
finish
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
def write(path, quality = nil)
|
85
|
+
FileUtils.mkdir_p(File.dirname(path))
|
86
|
+
@sprite.save(path)
|
87
|
+
end
|
88
|
+
|
89
|
+
def finish
|
90
|
+
@sprite = nil
|
91
|
+
end
|
92
|
+
|
93
|
+
def sanitize_asset_path(path)
|
94
|
+
path.split('?').first
|
95
|
+
end
|
96
|
+
|
97
|
+
def setup_context(railtie)
|
98
|
+
unless railtie.config.respond_to?(:action_controller)
|
99
|
+
railtie.config.action_controller = ActiveSupport::OrderedOptions.new
|
100
|
+
|
101
|
+
paths = railtie.config.paths
|
102
|
+
options = railtie.config.action_controller
|
103
|
+
|
104
|
+
options.assets_dir ||= paths.public.to_a.first
|
105
|
+
options.javascripts_dir ||= paths.public.javascripts.to_a.first
|
106
|
+
options.stylesheets_dir ||= paths.public.stylesheets.to_a.first
|
107
|
+
|
108
|
+
ActiveSupport.on_load(:action_controller) do
|
109
|
+
options.each { |k,v| send("#{k}=", v) }
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
controller = ActionController::Base.new
|
114
|
+
AssetContext.new(railtie.config.action_controller, {}, controller)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ActiveAssets
|
4
|
+
module ActiveSprites
|
5
|
+
module Configurable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
config_accessor :sprite_backend
|
10
|
+
self.sprite_backend = :rmagick if sprite_backend.nil?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -6,6 +6,8 @@ require 'active_assets/active_sprites'
|
|
6
6
|
module ActiveAssets
|
7
7
|
module ActiveSprites
|
8
8
|
class Railtie < Rails::Railtie
|
9
|
+
config.active_sprites = ActiveSupport::OrderedOptions.new
|
10
|
+
|
9
11
|
rake_tasks do
|
10
12
|
Dir[File.expand_path("../../../tasks/active_sprites/*.rake", __FILE__)].each {|f| load f}
|
11
13
|
end
|
@@ -21,6 +23,13 @@ module ActiveAssets
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
26
|
+
initializer 'active_sprites-set-configs' do
|
27
|
+
options = config.active_sprites
|
28
|
+
ActiveSupport.on_load(:active_sprites) do
|
29
|
+
options.each { |k,v| send("#{k}=", v) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
24
33
|
private
|
25
34
|
def load_sprite_definition(config_path)
|
26
35
|
load File.join(config_path, 'sprites.rb') if File.exists?(File.join(config_path, 'sprites.rb'))
|
@@ -7,11 +7,10 @@ require 'fileutils'
|
|
7
7
|
|
8
8
|
module ActiveAssets
|
9
9
|
module ActiveSprites
|
10
|
-
class
|
10
|
+
class RmagickRunner
|
11
11
|
class AssetContext < ActionView::Base
|
12
12
|
end
|
13
|
-
|
14
|
-
|
13
|
+
|
15
14
|
include Magick
|
16
15
|
|
17
16
|
DEFAULT_SPRITE = Image.new(0,0).freeze
|
@@ -19,13 +18,13 @@ module ActiveAssets
|
|
19
18
|
def initialize(sprites)
|
20
19
|
@sprites = if ENV['SPRITE']
|
21
20
|
sprites.select do |name, sprite|
|
22
|
-
ENV['SPRITE'].split(',').map(&:
|
21
|
+
ENV['SPRITE'].split(',').map(&:strip).any? do |sp|
|
23
22
|
# were going to be very forgiving
|
24
23
|
name == sp ||
|
25
24
|
name == sp.to_sym ||
|
26
25
|
name == ::Rack::Mount::Utils.normalize_path(sp)
|
27
26
|
end
|
28
|
-
end
|
27
|
+
end.map(&:last)
|
29
28
|
else
|
30
29
|
sprites.values
|
31
30
|
end
|
@@ -55,12 +54,12 @@ module ActiveAssets
|
|
55
54
|
offset = 0
|
56
55
|
|
57
56
|
image_list.each_with_index do |image, i|
|
58
|
-
sprite_pieces[i].details =
|
57
|
+
sprite_pieces[i].details = SpritePiece::Details.new(
|
59
58
|
sprite.url.present? ? sprite.url : sprite_path,
|
60
|
-
orientation == Sprite::Orientation::VERTICAL ? 0 :
|
61
|
-
orientation == Sprite::Orientation::VERTICAL ?
|
62
|
-
|
63
|
-
|
59
|
+
orientation == Sprite::Orientation::VERTICAL ? 0 : offset,
|
60
|
+
orientation == Sprite::Orientation::VERTICAL ? offset : 0,
|
61
|
+
image.columns,
|
62
|
+
image.rows
|
64
63
|
)
|
65
64
|
offset += orientation == Sprite::Orientation::VERTICAL ? image.rows : image.columns
|
66
65
|
end
|
@@ -74,7 +73,7 @@ module ActiveAssets
|
|
74
73
|
|
75
74
|
@sprite.strip!
|
76
75
|
|
77
|
-
stylesheet = SpriteStylesheet.new(
|
76
|
+
stylesheet = SpriteStylesheet.new(sprite_pieces)
|
78
77
|
stylesheet.write File.join(railtie.config.paths.public.to_a.first, sprite_stylesheet_path)
|
79
78
|
write File.join(railtie.config.paths.public.to_a.first, sprite_path), sprite.quality
|
80
79
|
ensure
|
@@ -18,6 +18,8 @@ module ActiveAssets
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
Details = Struct.new(:sprite_path, :x, :y, :width, :height)
|
22
|
+
|
21
23
|
GEOMETRY_PROPS = [:x, :y, :width, :height]
|
22
24
|
attr_reader(*GEOMETRY_PROPS)
|
23
25
|
attr_accessor :details
|
@@ -35,9 +37,9 @@ module ActiveAssets
|
|
35
37
|
<<-CSS
|
36
38
|
#{css_selector}
|
37
39
|
{
|
38
|
-
width:#{width || details.width};
|
39
|
-
height:#{height || details.height};
|
40
|
-
background:url('#{details.sprite_path}') no-repeat #{x || details.x} #{y || details.y};
|
40
|
+
width:#{width || "#{details.width}px"};
|
41
|
+
height:#{height || "#{details.height}px"};
|
42
|
+
background:url('#{details.sprite_path}') no-repeat #{x || "#{-details.x}px"} #{y || "#{-details.y}px"};
|
41
43
|
display:block;
|
42
44
|
}
|
43
45
|
CSS
|
@@ -3,8 +3,8 @@ require 'fileutils'
|
|
3
3
|
module ActiveAssets
|
4
4
|
module ActiveSprites
|
5
5
|
class SpriteStylesheet
|
6
|
-
def initialize(
|
7
|
-
@
|
6
|
+
def initialize(sprite_pieces)
|
7
|
+
@sprite_pieces = sprite_pieces
|
8
8
|
end
|
9
9
|
|
10
10
|
def write(path)
|
@@ -1,6 +1,12 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
1
3
|
module ActiveAssets
|
2
4
|
module ActiveSprites
|
3
5
|
class Sprites
|
6
|
+
include ActiveSupport::Configurable
|
7
|
+
include Configurable
|
8
|
+
ActiveSupport.run_load_hooks(:active_sprites, self)
|
9
|
+
|
4
10
|
def initialize
|
5
11
|
@sprites = Hash.new do |sprites, name|
|
6
12
|
sprites[name] = Sprite.new
|
@@ -45,8 +51,20 @@ module ActiveAssets
|
|
45
51
|
|
46
52
|
def generate!(railtie = Rails.application)
|
47
53
|
begin
|
48
|
-
|
49
|
-
|
54
|
+
case sprite_backend
|
55
|
+
when :rmagick
|
56
|
+
require 'rmagick'
|
57
|
+
RmagickRunner.new(@sprites).generate!(railtie)
|
58
|
+
when :chunky_png
|
59
|
+
begin
|
60
|
+
require 'oily_png'
|
61
|
+
ChunkyPngRunner.new(@sprites).generate!(railtie)
|
62
|
+
rescue LoadError
|
63
|
+
require 'chunky_png'
|
64
|
+
ChunkyPngRunner.new(@sprites).generate!(railtie)
|
65
|
+
raise
|
66
|
+
end
|
67
|
+
end
|
50
68
|
rescue LoadError
|
51
69
|
end
|
52
70
|
end
|
@@ -7,8 +7,10 @@ module ActiveAssets
|
|
7
7
|
autoload :SpritePiece
|
8
8
|
autoload :Sprite
|
9
9
|
autoload :Sprites
|
10
|
+
autoload :Configurable
|
10
11
|
autoload :SpriteStylesheet
|
11
|
-
autoload :
|
12
|
+
autoload :RmagickRunner
|
13
|
+
autoload :ChunkyPngRunner
|
12
14
|
|
13
15
|
def self.load_engine_tasks(engine_class)
|
14
16
|
desc "Generate sprites"
|
@@ -7,6 +7,9 @@ class SpriteTest < Test::Unit::TestCase
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def teardown
|
10
|
+
FileUtils.rm_rf(Rails.root.join('public/images/sprites'))
|
11
|
+
FileUtils.rm_rf(Rails.root.join('public/stylesheets/sprites'))
|
12
|
+
|
10
13
|
tear_down_assets
|
11
14
|
end
|
12
15
|
|
@@ -71,8 +74,8 @@ class SpriteTest < Test::Unit::TestCase
|
|
71
74
|
def test_url
|
72
75
|
Rails.application.sprites do
|
73
76
|
sprite :foobar, :url => "activecodebase.com" do
|
74
|
-
_"sprite_images/sprite3/1.
|
75
|
-
_"sprite_images/sprite3/2.
|
77
|
+
_"sprite_images/sprite3/1.png" => ".klass_1"
|
78
|
+
_"sprite_images/sprite3/2.png" => ".klass_2"
|
76
79
|
end
|
77
80
|
end
|
78
81
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Rails.application.sprites do
|
2
2
|
sprite 'sprites/3.png' => 'sprites/3.css' do
|
3
|
-
_"sprite_images/sprite3/1.
|
4
|
-
_"sprite_images/sprite3/2.
|
3
|
+
_"sprite_images/sprite3/1.png" => ".klass_1"
|
4
|
+
_"sprite_images/sprite3/2.png" => ".klass_2"
|
5
5
|
end
|
6
6
|
|
7
7
|
sprite 'sprites/4.png' do
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|