retina_rails 1.0.4 → 2.0.0

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -3
  3. data/.travis.yml +16 -4
  4. data/README.md +57 -29
  5. data/UPGRADING +7 -0
  6. data/UPGRADING.md +60 -0
  7. data/lib/retina_rails.rb +11 -7
  8. data/lib/retina_rails/extensions.rb +16 -0
  9. data/lib/retina_rails/extensions/carrierwave.rb +25 -0
  10. data/lib/retina_rails/extensions/paperclip.rb +23 -0
  11. data/lib/retina_rails/helpers.rb +42 -12
  12. data/lib/retina_rails/processors.rb +14 -0
  13. data/lib/retina_rails/processors/carrierwave.rb +49 -0
  14. data/lib/retina_rails/processors/paperclip.rb +30 -0
  15. data/lib/retina_rails/strategies.rb +6 -7
  16. data/lib/retina_rails/strategies/carrierwave.rb +56 -50
  17. data/lib/retina_rails/strategies/paperclip.rb +53 -55
  18. data/lib/retina_rails/version.rb +1 -1
  19. data/retina_rails.gemspec +11 -4
  20. data/spec/fixtures/db/retina_rails.sqlite3 +0 -0
  21. data/spec/helpers_spec.rb +87 -13
  22. data/spec/spec_helper.rb +10 -4
  23. data/spec/strategies/carrierwave_spec.rb +117 -154
  24. data/spec/strategies/paperclip_spec.rb +77 -104
  25. data/spec/support/carrierwave.rb +15 -0
  26. data/spec/support/file_string_io.rb +2 -0
  27. data/spec/support/paperclip.rb +9 -0
  28. data/spec/support/rails.rb +0 -11
  29. data/spec/support/schema.rb +8 -2
  30. metadata +53 -52
  31. data/lib/retina_rails/deprecation/carrierwave.rb +0 -23
  32. data/lib/retina_rails/deprecation/paperclip.rb +0 -23
  33. data/lib/retina_rails/exception.rb +0 -15
  34. data/spec/deprecation/carrierwave_spec.rb +0 -13
  35. data/spec/deprecation/paperclip_spec.rb +0 -14
  36. data/spec/fixtures/images/avatar.with.dots.jpeg +0 -0
  37. data/spec/fixtures/manifest.yml +0 -10
  38. data/vendor/assets/javascripts/retina.js +0 -113
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31ec07293af143bd14885c2afe55b7e2f4835827
4
- data.tar.gz: a52f634b29d6f2779ad7efe0c8707c6123538a20
3
+ metadata.gz: ab138b302000f7fe80c74791698ab63eb3767f9f
4
+ data.tar.gz: fcc8039ef665d5e15e4cdcb561015d79df618efc
5
5
  SHA512:
6
- metadata.gz: 3de52f4874673f6c29f14290fb0b716e2a2ad605790119b437428c9beb6267fa1ea5ce7dc7d57dae9b3ede7ae550eace649f624904a6b20be3abb0013d761ecb
7
- data.tar.gz: a3774c7bb7158d80c1a3cefaa0c3ccf601c9932c618d98f6cb645c16a069d0d6e5cea48e088ce3d8567ce5b84573bafc7c55b60c43823742dff769b8453efee5
6
+ metadata.gz: a07feddf0d93c425dcd7dee264afff7d3eab6dbee8d7072439d3a48e6d122595e0c54200cb6442104232bdd9c1f5a93763b4474272b4ffba8c2bf9925967e937
7
+ data.tar.gz: 679e66d2d3b510e851b18f55d1a92fd1d6ef543ba7ec168fbee867e24e32ea27740dd6255b0a05e37ebddd605586a204f183a6d14b535b1501de4afec24a0e7f
data/.gitignore CHANGED
@@ -13,11 +13,9 @@ lib/bundler/man
13
13
  pkg
14
14
  rdoc
15
15
  spec/reports
16
- spec/paperclip_uploads
17
- spec/strategies/paperclip_uploads
18
- spec/retina_rails/strategies/paperclip_uploads
19
16
  test/tmp
20
17
  test/version_tmp
21
18
  tmp
22
19
  log
23
20
  uploads
21
+ public
@@ -1,6 +1,18 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
- - 1.9.3
5
- - 2.0.0
6
- - ruby-head
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 automatically generates retina versions of your uploaded images (CarrierWave or Paperclip). It detects if a visitor has a retina display and if so it displays the @2x version.
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
- Note: It also works for images that live in assets/images.
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
- 1. Add `//= require retina` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
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 => 25
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
- ### Custom processors
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
- You can also use your custom processors like so:
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 => [100, 100, 'North', :jpg, 75]
55
- end
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
- This will generate `small.jpg` and `small@2x.jpg`.
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 and set `:retina` to true.
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
- :big => ["125x125#", :jpg]
108
+ :small => ["125x125#", :jpg]
81
109
  },
82
- :retina => true # Or
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
- For retina images use
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
- image_tag('image.png', :retina => true)
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
- Voila! Now you're using Retina Rails.
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 aims to support and is tested against[travis] the following Ruby
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.
@@ -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.
@@ -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.
@@ -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
- RetinaRails::Strategies.include_strategies
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
@@ -2,23 +2,53 @@ module ActionView
2
2
  module Helpers
3
3
  module AssetTagHelper
4
4
 
5
- def image_tag_with_retina(source, options={})
6
- retina = options.delete(:retina)
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
- if retina
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
- options[:data] ||= {}
15
- options[:data].merge!(:at2x => path_to_image(retina_source))
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