Skip to content

Commit 0031fb0

Browse files
committed
Merge branch 'Oldharlem-master'
2 parents 7e13bb4 + 49f0e39 commit 0031fb0

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,32 @@ See [example](http://backbonejs.org/#examples) (scroll down to see images load)
1818
### Features
1919

2020
* 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.
2122
* Simple (really). That's pretty much it.
2223

2324
### Example
2425

2526
```erb
26-
<%= image_tag "kittenz.png", alt: "OMG a cat!", lazy: true %>
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" %>
2740
```
2841

2942
Equals:
3043

3144
```html
32-
<img alt="OMG a cat!" data-original="/images/kittenz.png" src="http://www.appelsiini.net/projects/lazyload/img/grey.gif">
45+
<img src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs="
46+
data-original="/images/kittenz.png" />
3347
```
3448

3549
**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
5771

5872
## Configuration
5973

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-
6274
```ruby
6375
# config/initializers/lazyload.rb
6476
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:
6579
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
6684
end
6785
```
6886

lib/lazyload-rails.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ def image_tag(*attrs)
3737
options, args = extract_options_and_args(*attrs)
3838
image_html = rails_image_tag(*args)
3939

40-
if options[:lazy]
41-
to_lazy_image(image_html)
40+
is_lazy = options.fetch(:lazy) { Lazyload::Rails.configuration.lazy_by_default }
41+
42+
if is_lazy
43+
to_lazy(image_html)
4244
else
4345
image_html
4446
end
4547
end
4648

4749
private
4850

49-
def to_lazy_image(image_html)
51+
def to_lazy(image_html)
5052
img = Nokogiri::HTML::DocumentFragment.parse(image_html).at_css("img")
5153

5254
img["data-original"] = img["src"]

lib/lazyload-rails/config.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@ def placeholder=(new_placeholder)
2020
@placeholder = new_placeholder
2121
end
2222

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+
2331
# Set default settings
2432
def initialize
2533
@placeholder = "data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs="
34+
@lazy_by_default = false
2635
end
2736
end
2837
end

test/test_image_tag.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TestImageTag < ::ActionView::TestCase
55

66
def setup
77
backwards_compatibility_setup
8+
Lazyload::Rails.reset
89
end
910

1011
def test_lazy_option_sets_lazy_attributes
@@ -15,6 +16,29 @@ def test_lazy_option_sets_lazy_attributes
1516
assert_equal "101", img["width"]
1617
end
1718

19+
def test_lazy_by_default_config_sets_lazy_attributes
20+
Lazyload::Rails.configure do |config|
21+
config.lazy_by_default = true
22+
end
23+
24+
img = parse(image_tag("foo.png", size: "101x151"))
25+
26+
assert_match %r(^.*foo.png$), img["data-original"]
27+
assert_equal "151", img["height"]
28+
assert_equal "101", img["width"]
29+
end
30+
31+
def test_lazy_option_overrides_default_config
32+
Lazyload::Rails.configure do |config|
33+
config.lazy_by_default = true
34+
end
35+
36+
img = parse(image_tag("foo.png", size: "101x151", lazy: false))
37+
38+
assert_nil img["data-original"]
39+
assert_match %r(^.*foo.png$), img["src"]
40+
end
41+
1842
def test_lazy_attrs_override_conflicting_attributes
1943
conflicting_opts = { data: { original: "foo.png" } } # because the gem sets data-original
2044

@@ -32,8 +56,6 @@ def test_custom_placeholder_is_used_instead_of_normal_src
3256

3357
assert_equal "/public/img/grey.gif", img["src"]
3458
assert_match %r(^.*baz.png$), img["data-original"]
35-
36-
Lazyload::Rails.reset
3759
end
3860

3961
def test_nonlazy_attributes_can_be_set

0 commit comments

Comments
 (0)