lazyload-rails 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -4
- data/lib/lazyload-rails.rb +5 -3
- data/lib/lazyload-rails/config.rb +9 -0
- data/lib/lazyload-rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33d389cb78112e751d38adcc205bb19d234aa3b0c0c846c599f681473ca22659
|
4
|
+
data.tar.gz: f99fb92895fffc33bac83c651115e255c39cba145048fab053f9b94244fbab85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef51c77c1e513d95e72e1be6042b5dfde4e3992cc01363bb006cfbd27da1061fbc699d5f6dbc40e63f4e5146f26ab5772e44114fb977d5d8de033e9f44545bb2
|
7
|
+
data.tar.gz: 5e2d800ea43a8120dfe08e1d74bf3e02e1d4aa60b79c308e91965c979ae7451bcdab8c880cab903958c33a9a7cbd67b110938e3ac6cac493104afa0f56d1e019
|
data/README.md
CHANGED
@@ -18,18 +18,32 @@ See [example](http://backbonejs.org/#examples) (scroll down to see images load)
|
|
18
18
|
### Features
|
19
19
|
|
20
20
|
* Add `lazy: true` option to Rails `image_tag` helpers to render lazyload-friendly img tags.
|
21
|
+
* Global config available to make them lazy by default.
|
21
22
|
* Simple (really). That's pretty much it.
|
22
23
|
|
23
24
|
### Example
|
24
25
|
|
25
26
|
```erb
|
26
|
-
<%= image_tag "kittenz.png",
|
27
|
+
<%= image_tag "kittenz.png", lazy: true %>
|
28
|
+
```
|
29
|
+
|
30
|
+
or
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# config/initializers/lazyload.rb
|
34
|
+
Lazyload::Rails.configure do |config|
|
35
|
+
config.lazy_by_default = true
|
36
|
+
end
|
37
|
+
```
|
38
|
+
```erb
|
39
|
+
<%= image_tag "kittenz.png" %>
|
27
40
|
```
|
28
41
|
|
29
42
|
Equals:
|
30
43
|
|
31
44
|
```html
|
32
|
-
<img
|
45
|
+
<img src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs="
|
46
|
+
data-original="/images/kittenz.png" />
|
33
47
|
```
|
34
48
|
|
35
49
|
**PRO TIP!** You must set image dimensions either as width and height attributes or in CSS. Otherwise plugin might not work properly.
|
@@ -57,12 +71,16 @@ Lazy Load can be customized, [see more options](http://www.appelsiini.net/projec
|
|
57
71
|
|
58
72
|
## Configuration
|
59
73
|
|
60
|
-
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:
|
61
|
-
|
62
74
|
```ruby
|
63
75
|
# config/initializers/lazyload.rb
|
64
76
|
Lazyload::Rails.configure do |config|
|
77
|
+
# By default, a 1x1 grey gif is used as placeholder ("data:image/gif;base64,...").
|
78
|
+
# This can be easily customized:
|
65
79
|
config.placeholder = "/public/img/grey.gif"
|
80
|
+
|
81
|
+
# image_tag can return lazyload-friendly images by default,
|
82
|
+
# no need to pass the { lazy: true } option
|
83
|
+
config.lazy_by_default = true
|
66
84
|
end
|
67
85
|
```
|
68
86
|
|
data/lib/lazyload-rails.rb
CHANGED
@@ -37,8 +37,10 @@ ActionView::Helpers::AssetTagHelper.module_eval do
|
|
37
37
|
options, args = extract_options_and_args(*attrs)
|
38
38
|
image_html = rails_image_tag(*args)
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
is_lazy = options.fetch(:lazy) { Lazyload::Rails.configuration.lazy_by_default }
|
41
|
+
|
42
|
+
if is_lazy
|
43
|
+
to_lazy(image_html)
|
42
44
|
else
|
43
45
|
image_html
|
44
46
|
end
|
@@ -46,7 +48,7 @@ ActionView::Helpers::AssetTagHelper.module_eval do
|
|
46
48
|
|
47
49
|
private
|
48
50
|
|
49
|
-
def
|
51
|
+
def to_lazy(image_html)
|
50
52
|
img = Nokogiri::HTML::DocumentFragment.parse(image_html).at_css("img")
|
51
53
|
|
52
54
|
img["data-original"] = img["src"]
|
@@ -20,9 +20,18 @@ module Lazyload
|
|
20
20
|
@placeholder = new_placeholder
|
21
21
|
end
|
22
22
|
|
23
|
+
# When set to true every image_tag will include { lazy: true } by default
|
24
|
+
def lazy_by_default=(lazy_by_default)
|
25
|
+
@lazy_by_default = !!lazy_by_default
|
26
|
+
end
|
27
|
+
def lazy_by_default
|
28
|
+
@lazy_by_default
|
29
|
+
end
|
30
|
+
|
23
31
|
# Set default settings
|
24
32
|
def initialize
|
25
33
|
@placeholder = "data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs="
|
34
|
+
@lazy_by_default = false
|
26
35
|
end
|
27
36
|
end
|
28
37
|
end
|