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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bea9d89fb6ecb8a0218753ab51a6a6aa39e31a613b9c943c8562957c3fbb28a
4
- data.tar.gz: 7cda2a364ee6b450965eacba99085a670b4e0f0f09d51e6c7003657903c65e18
3
+ metadata.gz: 33d389cb78112e751d38adcc205bb19d234aa3b0c0c846c599f681473ca22659
4
+ data.tar.gz: f99fb92895fffc33bac83c651115e255c39cba145048fab053f9b94244fbab85
5
5
  SHA512:
6
- metadata.gz: da29205ffe57d1de0eabc3ebf82cadff46484306fea78046ee6089912bd13dc1b17de9c37399d44c54049476c7e51596652e9da037bec2e8a8271dedf10f1e13
7
- data.tar.gz: 75ad188919cb37d8297d0b898687f4332177a152600183839cd6d928dc41a85baaeee2bc58222f5d884fddcf8f5008c4618fd98f6a27087512f005f8bd493452
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", 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" %>
27
40
  ```
28
41
 
29
42
  Equals:
30
43
 
31
44
  ```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" />
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
 
@@ -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
- 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)
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 to_lazy_image(image_html)
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
@@ -1,5 +1,5 @@
1
1
  module Lazyload
2
2
  module Rails
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Trevino Saldana