lazyload-rails 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +25 -0
- data/lib/lazyload-rails.rb +30 -1
- data/lib/lazyload-rails/configuration.rb +29 -0
- data/lib/lazyload-rails/version.rb +1 -1
- metadata +11 -18
- data/.gitignore +0 -6
- data/.travis.yml +0 -4
- data/Gemfile +0 -7
- data/Rakefile +0 -8
- data/lazyload-rails.gemspec +0 -22
- data/test/asset_tag_helper_test.rb +0 -57
data/README.md
CHANGED
@@ -3,6 +3,16 @@
|
|
3
3
|
This project integrates the jQuery Lazy Load Plugin
|
4
4
|
for Rails `image_tag` helpers
|
5
5
|
|
6
|
+
### What's jQuery Lazy Load?
|
7
|
+
|
8
|
+
From the project page:
|
9
|
+
|
10
|
+
*Lazy Load is a jQuery plugin written in JavaScript. It delays loading of images in long web pages. Images outside of viewport (visible part of web page) won't be loaded before user scrolls to them. This is opposite of image preloading.*
|
11
|
+
|
12
|
+
*Using Lazy Load on long web pages containing many large images makes the page load faster. Browser will be in ready state after loading visible images. In some cases it can also help to reduce server load.*
|
13
|
+
|
14
|
+
See [example](http://backbonejs.org/#examples) (scroll down to see images load)
|
15
|
+
|
6
16
|
## Documentation
|
7
17
|
|
8
18
|
### Features
|
@@ -28,8 +38,23 @@ Add this line to your application's Gemfile:
|
|
28
38
|
Download the [jQuery Lazy Load Plugin](https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.js)
|
29
39
|
into your `vendor/assets/javascripts` directory and include it however you prefer.
|
30
40
|
|
41
|
+
And in your JavaScript code do:
|
42
|
+
|
43
|
+
$("img").lazyload();
|
44
|
+
|
45
|
+
Lazy Load can be customized, [see more options](http://www.appelsiini.net/projects/lazyload)
|
46
|
+
|
31
47
|
*Important: Remember that the Lazy Load Plugin depends on jQuery.*
|
32
48
|
|
49
|
+
## Configuration
|
50
|
+
|
51
|
+
By default, a 1x1 grey gif is used as placeholder (from [http://appelsiini.net/projects/lazyload/img/grey.gif](http://www.appelsiini.net/projects/lazyload/img/grey.gif)). This can be easily customized:
|
52
|
+
|
53
|
+
# config/initializers/lazyload.rb
|
54
|
+
Lazyload::Rails.configure do |config|
|
55
|
+
config.placeholder = "/public/img/grey.gif"
|
56
|
+
end
|
57
|
+
|
33
58
|
## FAQ
|
34
59
|
|
35
60
|
* *Q: How can I use the old `image_tag` method?*
|
data/lib/lazyload-rails.rb
CHANGED
@@ -1,12 +1,41 @@
|
|
1
1
|
require "nokogiri"
|
2
2
|
require "action_view"
|
3
|
+
|
3
4
|
require "lazyload-rails/version"
|
5
|
+
require "lazyload-rails/configuration"
|
6
|
+
|
7
|
+
module Lazyload
|
8
|
+
module Rails
|
9
|
+
|
10
|
+
def self.configuration
|
11
|
+
@configuration ||= Lazyload::Rails::Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configuration=(new_configuration)
|
15
|
+
@configuration = new_configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
# Yields the global configuration to a block.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# Lazyload::Rails.configure do |config|
|
22
|
+
# config.placeholder = '/public/images/foo.gif'
|
23
|
+
# end
|
24
|
+
def self.configure
|
25
|
+
yield configuration if block_given?
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reset
|
29
|
+
@configuration = nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
4
33
|
|
5
34
|
ActionView::Helpers::AssetTagHelper.module_eval do
|
6
35
|
alias :x_image_tag :image_tag
|
7
36
|
|
8
37
|
def image_tag(*attrs)
|
9
|
-
placeholder =
|
38
|
+
placeholder = Lazyload::Rails.configuration.placeholder
|
10
39
|
img = Nokogiri::HTML::DocumentFragment.parse(x_image_tag(*attrs)).at_css("img")
|
11
40
|
src = img["src"]
|
12
41
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Lazyload
|
3
|
+
module Rails
|
4
|
+
|
5
|
+
# Stores runtime configuration information.
|
6
|
+
#
|
7
|
+
# Example settings
|
8
|
+
# Lazyload::Rails.configure do |c|
|
9
|
+
# c.placeholder = "/public/img/grey.gif"
|
10
|
+
# end
|
11
|
+
class Configuration
|
12
|
+
|
13
|
+
# The placeholder image to put into the img src attribute
|
14
|
+
# (default: 1×1 pixel grey gif at
|
15
|
+
# "http://www.appelsiini.net/projects/lazyload/img/grey.gif").
|
16
|
+
def placeholder
|
17
|
+
@placeholder
|
18
|
+
end
|
19
|
+
def placeholder=(new_placeholder)
|
20
|
+
@placeholder = new_placeholder
|
21
|
+
end
|
22
|
+
|
23
|
+
# Set default settings
|
24
|
+
def initialize
|
25
|
+
@placeholder = "http://www.appelsiini.net/projects/lazyload/img/grey.gif"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazyload-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.5
|
21
|
+
version: '1.5'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 1.5
|
29
|
+
version: '1.5'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: actionpack
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -43,24 +43,18 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '3.1'
|
46
|
-
description:
|
47
|
-
|
48
|
-
email:
|
49
|
-
- javier@tractical.com
|
46
|
+
description: lazyload-rails project integrates jQuery Lazy Load Plugin for Rails image_tag
|
47
|
+
helpers
|
48
|
+
email: javier@tractical.com
|
50
49
|
executables: []
|
51
50
|
extensions: []
|
52
51
|
extra_rdoc_files: []
|
53
52
|
files:
|
54
|
-
- .gitignore
|
55
|
-
- .travis.yml
|
56
|
-
- Gemfile
|
57
|
-
- MIT-LICENSE
|
58
53
|
- README.md
|
59
|
-
-
|
60
|
-
- lazyload-rails.
|
61
|
-
- lib/lazyload-rails.rb
|
54
|
+
- MIT-LICENSE
|
55
|
+
- lib/lazyload-rails/configuration.rb
|
62
56
|
- lib/lazyload-rails/version.rb
|
63
|
-
-
|
57
|
+
- lib/lazyload-rails.rb
|
64
58
|
homepage: https://github.com/jassa/lazyload-rails
|
65
59
|
licenses:
|
66
60
|
- MIT
|
@@ -86,5 +80,4 @@ rubygems_version: 1.8.23
|
|
86
80
|
signing_key:
|
87
81
|
specification_version: 3
|
88
82
|
summary: jQuery Lazy Load for Rails image_tag helpers
|
89
|
-
test_files:
|
90
|
-
- test/asset_tag_helper_test.rb
|
83
|
+
test_files: []
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/lazyload-rails.gemspec
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
$:.push File.expand_path("../lib", __FILE__)
|
2
|
-
require "lazyload-rails/version"
|
3
|
-
|
4
|
-
Gem::Specification.new do |gem|
|
5
|
-
gem.author = ["Javier Saldana"]
|
6
|
-
gem.email = ["javier@tractical.com"]
|
7
|
-
gem.homepage = "https://github.com/jassa/lazyload-rails"
|
8
|
-
gem.name = "lazyload-rails"
|
9
|
-
gem.license = "MIT"
|
10
|
-
gem.version = Lazyload::Rails::VERSION
|
11
|
-
gem.summary = "jQuery Lazy Load for Rails image_tag helpers"
|
12
|
-
gem.description = <<-EOS
|
13
|
-
lazyload-rails project integrates jQuery Lazy Load
|
14
|
-
Plugin for Rails image_tag helpers
|
15
|
-
EOS
|
16
|
-
|
17
|
-
gem.files = `git ls-files`.split($/)
|
18
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
-
|
20
|
-
gem.add_dependency "nokogiri", "~> 1.5.9"
|
21
|
-
gem.add_development_dependency "actionpack", ">= 3.1"
|
22
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
-
|
3
|
-
require "test/unit"
|
4
|
-
require "action_view"
|
5
|
-
require "lazyload-rails"
|
6
|
-
|
7
|
-
class BasicController
|
8
|
-
attr_accessor :request
|
9
|
-
|
10
|
-
def config
|
11
|
-
@config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config).tap do |config|
|
12
|
-
public_dir = File.expand_path("../fixtures/public", __FILE__)
|
13
|
-
config.assets_dir = public_dir
|
14
|
-
config.assets = ActiveSupport::InheritableOptions.new({ :prefix => "assets" })
|
15
|
-
config
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class AssetTagHelperTest < ActionView::TestCase
|
21
|
-
tests ActionView::Helpers::AssetTagHelper
|
22
|
-
|
23
|
-
attr_reader :request
|
24
|
-
|
25
|
-
def setup
|
26
|
-
super
|
27
|
-
|
28
|
-
@controller = BasicController.new
|
29
|
-
|
30
|
-
@request = Class.new do
|
31
|
-
attr_accessor :script_name
|
32
|
-
def protocol() 'http://' end
|
33
|
-
def ssl?() false end
|
34
|
-
def host_with_port() 'localhost' end
|
35
|
-
def base_url() 'http://www.example.com' end
|
36
|
-
end.new
|
37
|
-
|
38
|
-
@controller.request = @request
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_lazy_attributes
|
42
|
-
expected = '<img alt="Foo" height="150"' +
|
43
|
-
' src="http://www.appelsiini.net/projects/lazyload/img/grey.gif"' +
|
44
|
-
' width="100" data-original="/images/foo.png">'
|
45
|
-
|
46
|
-
assert_equal expected, image_tag("foo.png", size: "100x150")
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_lazy_attributes_override
|
50
|
-
expected = '<img alt="Bar" data-original="/images/bar.png" height="150"' +
|
51
|
-
' src="http://www.appelsiini.net/projects/lazyload/img/grey.gif" width="200">'
|
52
|
-
|
53
|
-
actual = image_tag("bar.png", size: "200x150", data: { original: "foo" })
|
54
|
-
|
55
|
-
assert_equal expected, actual
|
56
|
-
end
|
57
|
-
end
|