responsive_images 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in responsive_images.gemspec
4
+ gemspec
5
+
6
+ gem 'mobvious', "0.3.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 John Koht
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,119 @@
1
+ # Responsive Images
2
+
3
+ ResponsiveImages is a simple library that handles responsive images on both the server and client side for Rails projects. It provides a set of helper methods that automatically load the appropriate image for whatever device a visitor is using.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'responsive_images'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install responsive_images
18
+
19
+
20
+ ## Configuration
21
+
22
+ The Responsive Image gem is easy to setup and use. Please note that it is currently requires Rails and [Carrierwave](https://github.com/jnicklas/carrierwave). To get started, you should setup or identify your carrierwave versions, here are some examples:
23
+
24
+ class MyUploader < CarrierWave::Uploader::Base
25
+ ...
26
+
27
+ # Mobile version
28
+ version :mobile do
29
+ process :resize_to_fit => [300, 300]
30
+ end
31
+
32
+ # Tablet version
33
+ version :tablet do
34
+ process :resize_to_fit => [600, 600]
35
+ end
36
+
37
+ # Desktop version
38
+ version :desktop do
39
+ process :resize_to_fit => [900, 900]
40
+ end
41
+
42
+ ...
43
+ end
44
+
45
+ You can add as many as you'd like.
46
+
47
+ #### Initializer
48
+ In order to configure the gem, you'll need to add an initializer in app/initializers/responsive_images.rb
49
+
50
+ ResponsiveImages.configure do |config|
51
+ # Set the default version for image. If you leave it at :default then it will use
52
+ # the original size, i.e., image.url or you can use a specific version
53
+ config.default = :default
54
+ # Add some custom sizes that you'll generate with Carrierwave. You can make as many
55
+ # as you want. But you'll need to configure mobvious for any custom ones.
56
+ config.sizes = {
57
+ mobile: :mobile, # carrierwave version size
58
+ tablet: :tablet, # another carrierwave version size
59
+ desktop: :desktop # and one more version...
60
+ }
61
+ end
62
+
63
+ Note that :mobile, :tablet and :desktop should be replaced with whatever carrierwave version you created. You can add more version but you'll need to configure mobvious to identify these screen sizes. ResponsiveImages does not currently support this but it is a feature that will eventually be added.
64
+
65
+ ## Usage
66
+
67
+ ResponsiveImages is incredibly easy to use. There are currently two primary helpers
68
+
69
+
70
+ ### Responsive Image Tag
71
+
72
+ The `responsive_image_tag` is meant replace the `image_tag`. You can pass any arguments, just like you would with an `image_tag`. Here is a basic exmpale:
73
+
74
+ # basic usage
75
+ = responsive_image_tag @post.image
76
+ # => <img src="path/to/image.jpg", data-desktop-src="path/to/desktop_image.jpg" data-tablet-src="path/to/tablet_image.jpg" data-mobile-src="path/to/mobile_image.jpg" />
77
+
78
+ The helper method will determine what size is most appropriate for the `src` attribute. Rather than load a default size and then swap them out, the ResponsiveImage gem actually detects the user device and uses the appropriate image for the `src` attribute.
79
+
80
+ You can also pass custom sizes to the helper method. If you have a specific model or page that use's different image, then pass those Carrierwave versions through the :sizes parameter:
81
+
82
+ # custom versions
83
+ = responsive_image_tag @post.image, :sizes => { :tablet => :post_tablet, :mobile => :post_mobile }
84
+ # => <img src="path/to/image.jpg", data-desktop-src="path/to/desktop_image.jpg" data-tablet-src="path/to/post_tablet_image.jpg" data-mobile-src="path/to/post_mobile_image.jpg" />
85
+
86
+ These custom versions will overwrite the default versions from our configuration. This is incredibly useful for pages or models that require a different set of sizes. For example, a page hero/banner might be much larger than most content images and requires custom sizes.
87
+
88
+
89
+ ### Responsive Background Image
90
+
91
+ ResponsiveImages also includes a helper for background images. You can easily call this with:
92
+
93
+ # basic usage
94
+ = responsive_background_image @post.image
95
+ # => style="url(path/to/image.jpg)" data-desktop-src="path/to/image.jpg" data-tablet-src="path/to/tablet_image.jpg" data-mobile-src="path/to/mobile_image.jpg"
96
+
97
+ This will automatically create the attributes for your `<div/>` or whatever tag you add a background image too.
98
+
99
+ You can also pass custom version to the helper method with:
100
+
101
+ # custom versions
102
+ = responsive_background_image @post.image, :sizes => { :'mobile' => :post_small, :tablet => :post_tablet }
103
+ # => style="url(path/to/image.jpg)" data-desktop-src="path/to/image.jpg" data-tablet-src="path/to/post_tablet_image.jpg" data-mobile-src="path/to/post_mobile_image.jpg"
104
+
105
+ ### Mobvious
106
+
107
+ ResponsiveImages uses [Mobvious](https://github.com/jistr/mobvious) for device detection. Check out [the gem](https://github.com/jistr/mobvious) for more details. You can use Mobvious to detect the user device by simply calling:
108
+
109
+ return request.env['mobvious.device_type']
110
+ # => desktop (mobile or tablet)
111
+
112
+
113
+ ## Contributing
114
+
115
+ 1. Fork it
116
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
117
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
118
+ 4. Push to the branch (`git push origin my-new-feature`)
119
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ require "responsive_images/version"
2
+ require "responsive_images/config"
3
+ require 'responsive_images/railtie' if defined?(Rails)
4
+
5
+ module ResponsiveImages
6
+
7
+ extend Config
8
+
9
+ class << self
10
+ def initialize attrs={}
11
+ attrs = ResponsiveImages.options.merge(attrs)
12
+ Config::VALID_OPTION_KEYS.each do |key|
13
+ instance_variable_set("@#{key}".to_sym, attrs[key])
14
+ end
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,28 @@
1
+ module ResponsiveImages
2
+ module Config
3
+
4
+ VALID_OPTION_KEYS = [
5
+ :default,
6
+ :sizes
7
+ ]
8
+
9
+ attr_accessor *VALID_OPTION_KEYS
10
+
11
+ def configure
12
+ yield self
13
+ self
14
+ end
15
+
16
+ def options
17
+ options = {}
18
+ VALID_OPTION_KEYS.each{ |pname| options[pname] = send(pname) }
19
+ options
20
+ end
21
+
22
+
23
+ def responsive_sizes
24
+ sizes
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # lib/my_gem/railtie.rb
2
+ require 'responsive_images/view_helpers'
3
+ require "mobvious"
4
+
5
+ module ResponsiveImages
6
+ class Railtie < Rails::Railtie
7
+
8
+ # Add our view helpers
9
+ initializer "responsive_images.view_helpers" do
10
+ ActionView::Base.send :include, ViewHelpers
11
+ end
12
+
13
+ # Add the mobvious middleware
14
+ initializer "railtie.configure_rails_initialization" do |app|
15
+ app.middleware.use Mobvious::Manager
16
+ end
17
+
18
+ # Configure Mobvious and setup the mobile tablet and desktop groups
19
+ Mobvious.configure do |config|
20
+ config.strategies = [ Mobvious::Strategies::MobileESP.new(:mobile_tablet_desktop) ]
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module ResponsiveImages
2
+ module Activator
3
+
4
+ class << self
5
+ def initialize attrs={}
6
+ attrs = ResponsiveImages.options.merge(attrs)
7
+ Config::VALID_OPTION_KEYS.each do |key|
8
+ instance_variable_set("@#{key}".to_sym, attrs[key])
9
+ end
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ResponsiveImages
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ module ResponsiveImages
2
+ module ViewHelpers
3
+
4
+
5
+ # Use mobvious to get the user's device type, it will return mobile, tablet or desktop
6
+ def device_type
7
+ return request.env['mobvious.device_type']
8
+ end
9
+
10
+
11
+ # Create a image tag with our responsive image data attributes
12
+ def responsive_image_tag image, options={}
13
+ # Merge any options passed with the configured options
14
+ sizes = ResponsiveImages.options.deep_merge(options)
15
+ # Let's create a hash of the alternative options for our data attributes
16
+ data_sizes = alternative_sizes(image, sizes)
17
+ # Get the image source
18
+ image_src = src_path(image, sizes)
19
+ # Return the image tag with our responsive data attributes
20
+ return image_tag image_src, data_sizes
21
+ end
22
+
23
+
24
+ def responsive_background_image image, options={}
25
+ # Merge any options passed with the configured options
26
+ sizes = ResponsiveImages.options.deep_merge(options)
27
+ data_hash = { style: "background-image: #{src_path(image, sizes)}" }.merge(alternative_sizes(image, sizes))
28
+ end
29
+
30
+
31
+ # Let's identify the default image size for the image tag. If it's a desktop then our
32
+ # default src attribute should be desktop (or default) but if it's a mobile or table
33
+ # then we should set the src attribute to the mobile or tablet image
34
+ def src_path image, sizes
35
+ case device_type
36
+ when :desktop
37
+ image_src = sizes[:default] == :default ? image.url : image.url.send(sizes[:default])
38
+ when :tablet
39
+ image_src = sizes[:sizes][:tablet].present? ? image.url.send(sizes[:sizes][:tablet]) : image.url.send(sizes[:default])
40
+ when :mobile
41
+ image_src = sizes[:sizes][:mobile].present? ? image.url.send(sizes[:sizes][:mobile]) : image.url.send(sizes[:default])
42
+ end
43
+ end
44
+
45
+
46
+ # Loop over the images sizes and create our data attributes hash
47
+ def alternative_sizes image, sizes
48
+ data_sizes = {}
49
+ sizes[:sizes].each do |size, value|
50
+ if value.present?
51
+ data_sizes["data-#{size}-src"] = (value == :default ? image.url : image.url.send(value))
52
+ else
53
+ false
54
+ end
55
+ end
56
+ data_sizes
57
+ end
58
+
59
+
60
+ end
61
+ end
@@ -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 'responsive_images/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "responsive_images"
8
+ spec.version = ResponsiveImages::VERSION
9
+ spec.authors = ["John Koht"]
10
+ spec.email = ["john@kohactive.com"]
11
+ spec.description = "A responsive image gem for Rails and Carrierwave"
12
+ spec.summary = "A responsive image gem for Rails and Carrierwave"
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_dependency "mobvious"
24
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: responsive_images
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Koht
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mobvious
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A responsive image gem for Rails and Carrierwave
63
+ email:
64
+ - john@kohactive.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/responsive_images.rb
75
+ - lib/responsive_images/config.rb
76
+ - lib/responsive_images/railtie.rb
77
+ - lib/responsive_images/responsive_images.rb
78
+ - lib/responsive_images/version.rb
79
+ - lib/responsive_images/view_helpers.rb
80
+ - responsive_images.gemspec
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.25
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: A responsive image gem for Rails and Carrierwave
106
+ test_files: []