retina_rails 1.0.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -3
- data/.travis.yml +16 -4
- data/README.md +57 -29
- data/UPGRADING +7 -0
- data/UPGRADING.md +60 -0
- data/lib/retina_rails.rb +11 -7
- data/lib/retina_rails/extensions.rb +16 -0
- data/lib/retina_rails/extensions/carrierwave.rb +25 -0
- data/lib/retina_rails/extensions/paperclip.rb +23 -0
- data/lib/retina_rails/helpers.rb +42 -12
- data/lib/retina_rails/processors.rb +14 -0
- data/lib/retina_rails/processors/carrierwave.rb +49 -0
- data/lib/retina_rails/processors/paperclip.rb +30 -0
- data/lib/retina_rails/strategies.rb +6 -7
- data/lib/retina_rails/strategies/carrierwave.rb +56 -50
- data/lib/retina_rails/strategies/paperclip.rb +53 -55
- data/lib/retina_rails/version.rb +1 -1
- data/retina_rails.gemspec +11 -4
- data/spec/fixtures/db/retina_rails.sqlite3 +0 -0
- data/spec/helpers_spec.rb +87 -13
- data/spec/spec_helper.rb +10 -4
- data/spec/strategies/carrierwave_spec.rb +117 -154
- data/spec/strategies/paperclip_spec.rb +77 -104
- data/spec/support/carrierwave.rb +15 -0
- data/spec/support/file_string_io.rb +2 -0
- data/spec/support/paperclip.rb +9 -0
- data/spec/support/rails.rb +0 -11
- data/spec/support/schema.rb +8 -2
- metadata +53 -52
- data/lib/retina_rails/deprecation/carrierwave.rb +0 -23
- data/lib/retina_rails/deprecation/paperclip.rb +0 -23
- data/lib/retina_rails/exception.rb +0 -15
- data/spec/deprecation/carrierwave_spec.rb +0 -13
- data/spec/deprecation/paperclip_spec.rb +0 -14
- data/spec/fixtures/images/avatar.with.dots.jpeg +0 -0
- data/spec/fixtures/manifest.yml +0 -10
- data/vendor/assets/javascripts/retina.js +0 -113
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab138b302000f7fe80c74791698ab63eb3767f9f
|
4
|
+
data.tar.gz: fcc8039ef665d5e15e4cdcb561015d79df618efc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a07feddf0d93c425dcd7dee264afff7d3eab6dbee8d7072439d3a48e6d122595e0c54200cb6442104232bdd9c1f5a93763b4474272b4ffba8c2bf9925967e937
|
7
|
+
data.tar.gz: 679e66d2d3b510e851b18f55d1a92fd1d6ef543ba7ec168fbee867e24e32ea27740dd6255b0a05e37ebddd605586a204f183a6d14b535b1501de4afec24a0e7f
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
- 1.9.2
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.1
|
7
|
+
- ruby-head
|
8
|
+
|
9
|
+
env:
|
10
|
+
- "RAILS_VERSION=3.1.0"
|
11
|
+
- "RAILS_VERSION=3.2.0"
|
12
|
+
- "RAILS_VERSION=4.0.0"
|
13
|
+
- "RAILS_VERSION=master"
|
14
|
+
|
15
|
+
matrix:
|
16
|
+
allow_failures:
|
17
|
+
- env: "RAILS_VERSION=master"
|
18
|
+
- rvm: ruby-head
|
data/README.md
CHANGED
@@ -4,19 +4,49 @@
|
|
4
4
|
|
5
5
|
Makes your life easier optimizing an application for retina displays.
|
6
6
|
|
7
|
+
### Still using version 1.0.x?
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
Check [upgrading](https://github.com/jhnvz/retina_rails/blob/master/UPGRADING.md) to upgrade or the version 1.0.x [readme](https://github.com/jhnvz/retina_rails/tree/1.0.4).
|
12
|
+
|
13
|
+
---
|
14
|
+
|
7
15
|
How it works
|
8
16
|
------------
|
9
17
|
|
10
|
-
Retina Rails
|
18
|
+
Retina Rails makes your application use high-resolution images by default. It automatically optimizes uploaded images (CarrierWave or Paperclip) for retina displays by making them twice the size and reducing the quality.
|
19
|
+
|
20
|
+
*Good source on setting up image quality: http://www.netvlies.nl/blog/design-interactie/retina-revolution*
|
21
|
+
|
22
|
+
Resources
|
23
|
+
------------
|
11
24
|
|
12
|
-
|
25
|
+
- [Installation](#installation)
|
26
|
+
- [Migrations](#migrations)
|
27
|
+
- [Carrierwave](#carrierwave)
|
28
|
+
- [Paperclip](#paperclip)
|
29
|
+
- [Displaying a retina image](#displaying-a-retina-image)
|
30
|
+
- [Upgrading](https://github.com/jhnvz/retina_rails/blob/master/UPGRADING.md)
|
13
31
|
|
14
32
|
Installation
|
15
33
|
------------
|
16
34
|
|
17
|
-
1. Add `gem 'retina_rails'` to your Gemfile.
|
35
|
+
1. Add `gem 'retina_rails', '~> 2.0.0'` to your Gemfile.
|
18
36
|
1. Run `bundle install`.
|
19
|
-
|
37
|
+
|
38
|
+
Migrations
|
39
|
+
------------
|
40
|
+
|
41
|
+
Add a text column named `retina_dimensions`. This column is used to store original dimensions of the images.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class AddRetinaDimensionsToUsers < ActiveRecord::Migration
|
45
|
+
def self.change
|
46
|
+
add_column :users, :retina_dimensions, :text
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
20
50
|
|
21
51
|
CarrierWave
|
22
52
|
------------
|
@@ -30,7 +60,7 @@ class ExampleUploader < CarrierWave::Uploader::Base
|
|
30
60
|
|
31
61
|
version :small do
|
32
62
|
process :resize_to_fill => [30, 30]
|
33
|
-
process :retina_quality =>
|
63
|
+
process :retina_quality => 80
|
34
64
|
end
|
35
65
|
|
36
66
|
version :large, :retina => false do
|
@@ -39,11 +69,12 @@ class ExampleUploader < CarrierWave::Uploader::Base
|
|
39
69
|
|
40
70
|
end
|
41
71
|
```
|
42
|
-
By default it sets the retina image quality to 40 which can be overriden with `process :retina_quality => 25`. To disable the creation of a retina version simply call `version :small, :retina => false`.
|
43
72
|
|
44
|
-
|
73
|
+
By default it sets the retina image quality to 60 which can be overriden with `process :retina_quality => 80`. To disable the creation of a retina version simply call `version :small, :retina => false`.
|
45
74
|
|
46
|
-
|
75
|
+
### Custom resize processors
|
76
|
+
|
77
|
+
You can also use your custom resize processors like so:
|
47
78
|
|
48
79
|
```ruby
|
49
80
|
class ExampleUploader < CarrierWave::Uploader::Base
|
@@ -51,23 +82,20 @@ class ExampleUploader < CarrierWave::Uploader::Base
|
|
51
82
|
retina!
|
52
83
|
|
53
84
|
version :small, :retina => false do
|
54
|
-
process :resize_to_fill_with_gravity => [
|
55
|
-
|
56
|
-
|
57
|
-
version :small_retina, :retina => false do
|
58
|
-
process :resize_to_fill_with_gravity => [200, 200, 'North', :jpg, 40]
|
85
|
+
process :resize_to_fill_with_gravity => [200, 200, 'North', :jpg, 75]
|
86
|
+
process :store_retina_dimensions
|
59
87
|
end
|
60
88
|
|
61
89
|
end
|
62
90
|
```
|
63
91
|
|
64
|
-
|
92
|
+
Make sure you double the image size yourself in your processor. In this example the image will be displayed with a size of 100x100.
|
65
93
|
|
66
94
|
|
67
95
|
Paperclip
|
68
96
|
------------
|
69
97
|
|
70
|
-
Simply add `retina!` to your model
|
98
|
+
Simply add `retina!` to your model.
|
71
99
|
|
72
100
|
```ruby
|
73
101
|
class ExampleUploader < ActiveRecord::Base
|
@@ -77,38 +105,38 @@ class ExampleUploader < ActiveRecord::Base
|
|
77
105
|
has_attached_file :image,
|
78
106
|
:styles => {
|
79
107
|
:original => ["800x800", :jpg],
|
80
|
-
:
|
108
|
+
:small => ["125x125#", :jpg]
|
81
109
|
},
|
82
|
-
:retina =>
|
83
|
-
:retina => { :quality => 25 } # Optional
|
110
|
+
:retina => { :quality => 80 }
|
84
111
|
|
85
112
|
end
|
86
113
|
```
|
87
|
-
By default it sets the retina image quality to 40 which can be overriden by adding a `quality` option.
|
88
114
|
|
89
|
-
|
115
|
+
By default it sets the retina image quality to 60 which can be overriden by adding a `quality` option. To disable the creation of a retina version set the `retina` option to false `:retina => false`.
|
116
|
+
|
117
|
+
Displaying a retina image
|
90
118
|
------------
|
91
119
|
|
92
120
|
```ruby
|
93
|
-
|
121
|
+
retina_image_tag(@user, :image, :small, :default => [50, 40)
|
122
|
+
# or
|
123
|
+
retina_image_tag(@user, :image, :small, :default => { :width => 50, :height => 40 })
|
94
124
|
```
|
95
125
|
|
96
|
-
|
126
|
+
If no image is uploaded (yet) it will display the default image defined with CarrierWave or Paperclip and set the width and height attributes specified in the default option.
|
127
|
+
|
128
|
+
**Voila! Now you're using Retina Rails.**
|
97
129
|
|
98
130
|
Supported Ruby Versions
|
99
131
|
------------
|
100
132
|
|
101
|
-
This library
|
133
|
+
This library is tested against Travis and aims to support the following Ruby
|
102
134
|
implementations:
|
103
135
|
|
104
136
|
* Ruby 1.9.2
|
105
137
|
* Ruby 1.9.3
|
106
138
|
* Ruby 2.0.0
|
107
|
-
|
108
|
-
Credits
|
109
|
-
------------
|
110
|
-
|
111
|
-
Retina Rails uses retinajs (https://github.com/imulus/retinajs)
|
139
|
+
* Ruby 2.1.1
|
112
140
|
|
113
141
|
Contributing
|
114
142
|
------------
|
@@ -122,4 +150,4 @@ Contributing
|
|
122
150
|
Copyright
|
123
151
|
------------
|
124
152
|
|
125
|
-
Copyright (c) 2012 Johan van Zonneveld. See LICENSE for details.
|
153
|
+
Copyright (c) 2012-2014 Johan van Zonneveld. See LICENSE for details.
|
data/UPGRADING
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
##################################################
|
2
|
+
# NOTE FOR UPGRADING FROM PRE-2.0 VERSION #
|
3
|
+
##################################################
|
4
|
+
|
5
|
+
Retina Rails 2.0 introduces non-backward compatible changes.
|
6
|
+
Check https://github.com/jhnvz/retina_rails/blob/master/UPGRADING.md
|
7
|
+
for more details.
|
data/UPGRADING.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Upgrading
|
2
|
+
|
3
|
+
In version 2.0.0 we got rid of double image uploads and and the javascript helper. Retina Rails now displays high-resolution images by default instead of swapping the low-res image with a high-res version. This will save up on requests and storage.
|
4
|
+
|
5
|
+
Since the whole strategy for displaying images has changed there are some things you need to do for version 2.0.0 to work.
|
6
|
+
|
7
|
+
## Javascript helper
|
8
|
+
|
9
|
+
Remove `//= require retina` from your Javascript manifest file (usually found at app/assets/javascripts/application.js) since we don't need it anymore.
|
10
|
+
|
11
|
+
## Migrations
|
12
|
+
|
13
|
+
You'll need to add a `retina_dimensions` column to the table of every model using retina optimised image uploads.
|
14
|
+
|
15
|
+
**For example:**
|
16
|
+
```ruby
|
17
|
+
class AddRetinaDimensionsToUsers < ActiveRecord::Migration
|
18
|
+
def self.change
|
19
|
+
add_column :users, :retina_dimensions, :text
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
## Displaying images
|
25
|
+
|
26
|
+
Instead of rendering images with the `image_tag` method we now render with `retina_image_tag`.
|
27
|
+
|
28
|
+
**Old way:**
|
29
|
+
```ruby
|
30
|
+
image_tag(@user.image.url(:small), :retina => true)
|
31
|
+
```
|
32
|
+
|
33
|
+
**New way:**
|
34
|
+
```ruby
|
35
|
+
retina_image_tag(@user, :image, :small, :default => [50, 40)
|
36
|
+
# or
|
37
|
+
retina_image_tag(@user, :image, :small, :default => { :width => 50, :height => 40 })
|
38
|
+
```
|
39
|
+
|
40
|
+
## Reprocessing uploads
|
41
|
+
|
42
|
+
Since we only store the retina optimised version we need to save the original dimensions of the uploaded image. Every uploaded image needs to be reprocessed.
|
43
|
+
|
44
|
+
### Carrierwave
|
45
|
+
|
46
|
+
Open up a console and run:
|
47
|
+
```ruby
|
48
|
+
Model.find_each do |model|
|
49
|
+
model.image.recreate_versions!
|
50
|
+
end
|
51
|
+
```
|
52
|
+
Or create a rake task that will do the trick for you.
|
53
|
+
|
54
|
+
### Paperclip
|
55
|
+
|
56
|
+
Run: `rake paperclip:refresh`
|
57
|
+
|
58
|
+
---
|
59
|
+
|
60
|
+
Make sure to run a test on your local machine or your staging environment before deploying to a production environment.
|
data/lib/retina_rails.rb
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
require 'retina_rails/version'
|
2
|
+
require 'retina_rails/extensions'
|
3
|
+
require 'retina_rails/processors'
|
2
4
|
require 'retina_rails/strategies'
|
3
5
|
require 'retina_rails/helpers'
|
4
|
-
require 'retina_rails/exception'
|
5
|
-
|
6
|
-
require 'retina_rails/deprecation/carrierwave'
|
7
|
-
require 'retina_rails/deprecation/paperclip'
|
8
6
|
|
9
7
|
module RetinaRails
|
10
8
|
|
11
9
|
if defined?(Rails)
|
12
|
-
class Engine < Rails::Engine; end
|
13
|
-
|
14
10
|
class Railtie < Rails::Railtie
|
11
|
+
initializer "retina_rails.include_extenions" do
|
12
|
+
RetinaRails::Extensions.include_extensions
|
13
|
+
end
|
14
|
+
|
15
|
+
initializer "retina_rails.include_processors" do
|
16
|
+
RetinaRails::Processors.include_processors
|
17
|
+
end
|
18
|
+
|
15
19
|
initializer "retina_rails.include_strategies" do
|
16
|
-
|
20
|
+
RetinaRails::Strategies.include_strategies
|
17
21
|
end
|
18
22
|
end
|
19
23
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RetinaRails
|
2
|
+
module Extensions
|
3
|
+
|
4
|
+
def self.include_extensions
|
5
|
+
if defined?(::CarrierWave)
|
6
|
+
require 'retina_rails/extensions/carrierwave'
|
7
|
+
::CarrierWave::Mount.send(:include, CarrierWave::Mount)
|
8
|
+
end
|
9
|
+
if defined?(::Paperclip)
|
10
|
+
require 'retina_rails/extensions/paperclip'
|
11
|
+
::Paperclip::Style.send(:include, Paperclip::Style)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end # Extensions
|
16
|
+
end # RetinaRails
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RetinaRails
|
2
|
+
module Extensions
|
3
|
+
module CarrierWave
|
4
|
+
module Mount
|
5
|
+
|
6
|
+
def self.included base
|
7
|
+
base.module_eval do
|
8
|
+
alias_method :original_mount_uploader, :mount_uploader
|
9
|
+
|
10
|
+
##
|
11
|
+
# Serialize retina_dimensions
|
12
|
+
# if mounted to class has a retina_dimensions column
|
13
|
+
#
|
14
|
+
def mount_uploader(*args)
|
15
|
+
original_mount_uploader(*args)
|
16
|
+
|
17
|
+
serialize :retina_dimensions if table_exists? && columns_hash.has_key?('retina_dimensions')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end # Mount
|
23
|
+
end # CarrierWave
|
24
|
+
end # Extensions
|
25
|
+
end # RetinaRails
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RetinaRails
|
2
|
+
module Extensions
|
3
|
+
module Paperclip
|
4
|
+
module Style
|
5
|
+
|
6
|
+
def self.included base
|
7
|
+
base.class_eval do
|
8
|
+
alias_method :original_processor_options, :processor_options
|
9
|
+
|
10
|
+
##
|
11
|
+
# Make sure to add the current style being processed to the args
|
12
|
+
# so we can identify which style is being processed
|
13
|
+
#
|
14
|
+
def processor_options
|
15
|
+
original_processor_options.merge!(:style => name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end # Style
|
21
|
+
end # Paperclip
|
22
|
+
end # Extensions
|
23
|
+
end # RetinaRails
|
data/lib/retina_rails/helpers.rb
CHANGED
@@ -2,23 +2,53 @@ module ActionView
|
|
2
2
|
module Helpers
|
3
3
|
module AssetTagHelper
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
##
|
6
|
+
# Displays a version of an upload and sets stored width and height attributes
|
7
|
+
#
|
8
|
+
# === Parameters
|
9
|
+
#
|
10
|
+
# [model (Model)] model instance
|
11
|
+
# [mounted_to (Sym)] attribute to which uploader is mounted
|
12
|
+
# [version (Sym)] version of the upload
|
13
|
+
# [options (Hash)] optional options hash
|
14
|
+
#
|
15
|
+
# === Examples
|
16
|
+
#
|
17
|
+
# retina_image_tag(@user, :avatar, :small, :default => { :width => 300, :height => 200 })
|
18
|
+
# retina_image_tag(@user, :avatar, :small, :default => [300, 200])
|
19
|
+
#
|
20
|
+
def retina_image_tag(model, mounted_to, version, options={})
|
21
|
+
default = options.delete(:default)
|
22
|
+
|
23
|
+
# Check if we can find the dimensions of the uploaded image.
|
24
|
+
# If no image or dimensions available use default.
|
25
|
+
if model.retina_dimensions.kind_of?(Hash) && model.retina_dimensions[mounted_to.to_sym]
|
26
|
+
dimensions = model.retina_dimensions[mounted_to.to_sym][version.to_sym]
|
27
|
+
else
|
28
|
+
if default.kind_of?(Array)
|
29
|
+
default = { :width => default[0], :height => default[1] }
|
30
|
+
end
|
31
|
+
dimensions = default || {}
|
32
|
+
end
|
7
33
|
|
8
|
-
|
9
|
-
retina_source = source.to_s
|
10
|
-
retina_source = retina_source.split('.')
|
11
|
-
filename = retina_source.slice!(-2)
|
12
|
-
retina_source = retina_source.insert(-2, "#{filename}@2x").join('.')
|
34
|
+
options = dimensions.merge(options)
|
13
35
|
|
14
|
-
|
15
|
-
|
36
|
+
image_tag(model.send(mounted_to).url(version), options)
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Show deprecation warning when old image_tag helper with retina option is used.
|
41
|
+
# TODO: Remove in new release
|
42
|
+
#
|
43
|
+
def image_tag_with_retina(source, options={})
|
44
|
+
if options.delete(:retina)
|
45
|
+
ActiveSupport::Deprecation.warn("`image_tag('image.png', :retina => true)` is deprecated use `retina_image_tag` instead")
|
16
46
|
end
|
17
47
|
|
18
48
|
image_tag_without_retina(source, options)
|
19
49
|
end
|
20
50
|
alias_method_chain :image_tag, :retina
|
21
51
|
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
52
|
+
end # AssetTagHelper
|
53
|
+
end # Helpers
|
54
|
+
end # ActionView
|