unveil-rails 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11ac877c26c44d8716fcd650f8539357e06f94a3
4
+ data.tar.gz: 803b516ad64838c23968b555167e01d90830c81a
5
+ SHA512:
6
+ metadata.gz: 68f73d490a9ad2603714f43a39274d5a53f07e2cb6db0d16f337cc782bd6da229890fa6ecc6dc867aff0054364813ce36931ff0541c692224ce08653af99591b
7
+ data.tar.gz: 1ada1b0cdb1497d1c196723253f817c356c026b7bcde1a330bc209981b0e2c9d2394b3b2e2f6b4412b6758c02c90d80fc5483a27ce855db5f690320447ed38b2
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Made Tech Ltd
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.
@@ -0,0 +1,77 @@
1
+ # Unveil.js for Rails
2
+
3
+ This gem will help you get started with unveil.js rather quickly.
4
+
5
+ ## Installation
6
+
7
+ Add the gem:
8
+
9
+ ``` ruby
10
+ gem 'unveil-rails', '~> 0.1.0'
11
+ ```
12
+
13
+ Install the assets:
14
+
15
+ ```
16
+ bin/rails g unveil:rails:install
17
+ ```
18
+
19
+ Include the asset in your JS:
20
+
21
+ ``` js
22
+ //= require unveil_init
23
+ ```
24
+
25
+ Now use the helper method whereever you want lazy images:
26
+
27
+ ```erb
28
+ <%= lazy_image_tag('an-image.png') %>
29
+ <%= lazy_image_tag('an-image.png', retina: 'a-retina-image.png') %>
30
+ <%= lazy_image_tag('an-image.png', placeholder: 'placeholder.png') %>
31
+ ```
32
+
33
+ ## Configuration
34
+
35
+ The installation generator will have added an initializer to your application.
36
+ In this file there are a couple of settings you can change.
37
+
38
+ ### Threshold
39
+
40
+ ``` ruby
41
+ Unveil::Rails.config.threshold = 200
42
+ ```
43
+
44
+ Setting this variable to an integer will set the unveil threshold setting
45
+ as [documented here][unveil-docs].
46
+
47
+ ### Default placeholder
48
+
49
+ You may set an alternative default placeholder image:
50
+
51
+ ``` ruby
52
+ Unveil::Rails.config.default_placeholder = 'default.jpg'
53
+ ```
54
+
55
+ You can set the placeholder per lazy image by passing in the placeholder option:
56
+
57
+ ``` erb
58
+ <%= lazy_image_tag('an-image.png', placeholder: 'placeholder.png') %>
59
+ ```
60
+
61
+ ## Credits
62
+
63
+ [![made](https://s3-eu-west-1.amazonaws.com/made-assets/googleapps/google-apps.png)][made]
64
+
65
+ Developed and maintained by [Made Tech][made]. Key contributions:
66
+
67
+ * [Luke Morton](https://github.com/DrPheltRight)
68
+
69
+ ## License
70
+
71
+ Copyright © 2014 Made Tech Ltd. It is free software, and may be
72
+ redistributed under the terms specified in the [LICENSE][license] file.
73
+
74
+ [made]: http://www.madetech.co.uk?ref=github&repo=cf-deploy
75
+ [license]: https://github.com/madebymade/cf-deploy/blob/master/LICENSE
76
+ [unveil-github]: https://github.com/luis-almeida/unveil
77
+ [unveil-docs]: http://luis-almeida.github.io/unveil/
@@ -0,0 +1,3 @@
1
+ require 'unveil/rails'
2
+ require 'unveil/rails/config'
3
+ require 'unveil/rails/railtie' if defined?(Rails)
@@ -0,0 +1,7 @@
1
+ module Unveil
2
+ class Rails
3
+ def self.config
4
+ @@config ||= Config.new
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Unveil
2
+ class Rails
3
+ class Config < Struct.new(:threshold, :default_placeholder)
4
+ def default_placeholder
5
+ super || 'blank.gif'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ module Unveil
2
+ class Rails
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('../../assets', __FILE__)
5
+
6
+ def copy_initializer
7
+ initializer 'unveil.rb' do
8
+ <<-CONFIG
9
+ Unveil::Rails.config.threshold = nil
10
+ Unveil::Rails.config.default_placeholder = 'blank.gif'
11
+ CONFIG
12
+ end
13
+ end
14
+
15
+ def copy_blank_gif_into_app_images
16
+ copy_file 'blank.gif', 'app/assets/images/blank.gif'
17
+ end
18
+
19
+ def copy_unveil_into_vendor
20
+ copy_file 'jquery.unveil.js', 'vendor/assets/javascripts/jquery.unveil.js'
21
+ copy_file 'unveil_init.js.erb', 'vendor/assets/javascripts/unveil_init.js.erb'
22
+
23
+ puts ''
24
+ puts 'Please add the following line to your JS:'
25
+ puts ''
26
+ puts ' //= require unveil_init'
27
+ puts ''
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require 'unveil/rails/view_helper'
2
+
3
+ module Unveil
4
+ class Rails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'unveil-rails.view_helpers' do
7
+ ActionView::Base.send(:include, ViewHelper)
8
+ end
9
+
10
+ generators do
11
+ require 'unveil/rails/install_generator'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Unveil
2
+ class Rails
3
+ VERSION='0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ module Unveil
2
+ class Rails
3
+ module ViewHelper
4
+ def lazy_image_tag(source, options = {})
5
+ placeholder_source = options[:placeholder] || Unveil::Rails.config.default_placeholder
6
+
7
+ lazy_options = options.merge('data-src' => image_path(source),
8
+ alt: image_alt(source))
9
+
10
+ if options.has_key?(:retina)
11
+ lazy_options['data-src-retina'] = image_path(options[:retina])
12
+ end
13
+
14
+ html = [image_tag(placeholder_source, lazy_options)]
15
+
16
+ html << '<noscript>'
17
+ html << image_tag(source, options)
18
+ html << '</noscript>'
19
+
20
+ html.join("\n").html_safe
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,61 @@
1
+ require 'unveil/rails/view_helper'
2
+ require_relative '../mocks/context_mock'
3
+
4
+ describe Unveil::Rails::ViewHelper do
5
+ let(:helper) { ContextMock.new }
6
+
7
+ before do
8
+ String.instance_eval do
9
+ alias_method :html_safe, :to_s
10
+ end
11
+ end
12
+
13
+ context '#lazy_image_tag' do
14
+ subject { helper.lazy_image_tag('my-image.png', options) }
15
+
16
+ context 'when called without options' do
17
+ let(:options) { {} }
18
+
19
+ let(:expected_html) do
20
+ <<-HTML
21
+ <img src="/blank.gif" data-src="/my-image.png" />
22
+ <noscript>
23
+ <img src="/my-image.png" />
24
+ </noscript>
25
+ HTML
26
+ end
27
+
28
+ it { is_expected.to eq(expected_html.strip) }
29
+ end
30
+
31
+ context 'when alternate placeholder provided' do
32
+ let(:options) { { placeholder: 'place.gif' } }
33
+
34
+ let(:expected_html) do
35
+ <<-HTML
36
+ <img src="/place.gif" data-src="/my-image.png" />
37
+ <noscript>
38
+ <img src="/my-image.png" />
39
+ </noscript>
40
+ HTML
41
+ end
42
+
43
+ it { is_expected.to eq(expected_html.strip) }
44
+ end
45
+
46
+ context 'when retina image provided' do
47
+ let(:options) { { retina: 'my-retina-image.png' } }
48
+
49
+ let(:expected_html) do
50
+ <<-HTML
51
+ <img src="/blank.gif" data-src="/my-image.png" data-src-retina="/my-retina-image.png" />
52
+ <noscript>
53
+ <img src="/my-image.png" />
54
+ </noscript>
55
+ HTML
56
+ end
57
+
58
+ it { is_expected.to eq(expected_html.strip) }
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,18 @@
1
+ class ContextMock
2
+ include Unveil::Rails::ViewHelper
3
+
4
+ def image_path(path)
5
+ "/#{path}"
6
+ end
7
+
8
+ def image_alt(alt)
9
+ alt
10
+ end
11
+
12
+ def image_tag(source, options = {})
13
+ options_html = ''
14
+ options_html << " data-src=\"#{options['data-src']}\"" if options['data-src']
15
+ options_html << " data-src-retina=\"#{options['data-src-retina']}\"" if options['data-src-retina']
16
+ "<img src=\"#{image_path(source)}\"#{options_html} />"
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ require 'unveil/rails'
2
+ require 'unveil/rails/config'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unveil-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luke Morton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ description:
56
+ email:
57
+ - luke@madetech.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/unveil-rails.rb
65
+ - lib/unveil/rails.rb
66
+ - lib/unveil/rails/config.rb
67
+ - lib/unveil/rails/install_generator.rb
68
+ - lib/unveil/rails/railtie.rb
69
+ - lib/unveil/rails/version.rb
70
+ - lib/unveil/rails/view_helper.rb
71
+ - spec/helpers/view_helper_spec.rb
72
+ - spec/mocks/context_mock.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/madebymade/unveil-rails
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.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Lazy loaded images for rails
98
+ test_files: []
99
+ has_rdoc: