izi_lightup 1.0.12 → 1.0.17

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21f41382e7ef7800607c021d33859c57b10a4408580acef3e068d92a2eaf6fdb
4
- data.tar.gz: 635d22e277085980967845763882aaddfb8c3f161e4b0fff3bdb09d782f2a875
3
+ metadata.gz: f1c5603ca13de2a4cdacbde2661a234b2bc2840658a261914f35ff47cb98d07e
4
+ data.tar.gz: be1513d0fa4e6cb010e80acb83babff7c4464e7d07f566aec0594d23bf37820d
5
5
  SHA512:
6
- metadata.gz: 3909f266b8eaf65aae1ee745bf6079508f31008181fe24083cdfeb914177886f0b03f3752457ee72c9871a40f1a6c41ed517f43a77f2e7e3d4d9e0fb6d3a54b8
7
- data.tar.gz: 99964f117e4f1725b99375185891857310be36ecdaa119858f9b683297b85fd688eab1d97f6b8bc7c95811ca1bbec1ca74f60bf4ba5ade878b9f3d8e981ea9fb
6
+ metadata.gz: f879475075c1d7e5915cdfd1b13b9583da64c80eeb88aacde80193c2d6e780b8ad4e1b5a7e1f7c078d415408b38023c9db6b48c8a4e63ac6a32ea5ca2d3058fe
7
+ data.tar.gz: b74a74d53569055e21493ca675c7bfd6a0634e8108201a891fbf0d06c427cc84e926882fa6011a346d9d211e7953968c681967450288ca6a29e57f1aab0edc7e
@@ -52,16 +52,38 @@ module CriticalHelper
52
52
  IziLightup::SmartPicture.render(object, *args, **xargs, &block)
53
53
  end
54
54
 
55
+ def debug_critical_css(scope_name = 'critical')
56
+ {
57
+ lookup: scoped_css_files(scope_name),
58
+ found: find_scoped_css(scope_name)
59
+ }.to_json.html_safe
60
+ end
61
+
55
62
  private
56
63
 
57
- def find_scoped_css(scope_name)
64
+ def scoped_css_files(scope_name)
58
65
  [
59
- File.join(scope_name, "#{controller_path}_#{action_name}.css"),
60
- File.join(scope_name, "#{controller_path}.css"),
61
- File.join(scope_name, "#{controller_name}_#{action_name}.css"),
62
- File.join(scope_name, "#{controller_name}.css"),
63
- "#{scope_name}.css"
64
- ].detect { |n| asset_exist?(n) }
66
+ "#{controller_path}_#{action_name}",
67
+ controller_path,
68
+ *scoped_namespace_file(scope_name),
69
+ "#{controller_name}_#{action_name}",
70
+ controller_name,
71
+ scope_name
72
+ ].compact.uniq.map { |l| File.join(scope_name, "#{l}.css") }
73
+ end
74
+
75
+ def scoped_namespace_file(scope_name)
76
+ scopes = []
77
+ return scopes if controller_path == controller_name
78
+
79
+ parts = controller_path.gsub(/\/#{controller_name}$/, '').split('/').reject(&:blank?)
80
+ parts.each { |p| scopes.unshift(File.join([scopes.first, p].compact)) }
81
+
82
+ scopes.uniq
83
+ end
84
+
85
+ def find_scoped_css(scope_name)
86
+ scoped_css_files(scope_name).detect { |n| asset_exist?(n) }
65
87
  end
66
88
 
67
89
  def fetch_items(object, fields)
@@ -71,11 +93,25 @@ module CriticalHelper
71
93
  end
72
94
 
73
95
  def asset_exist?(name)
74
- return manifest.find_sources(name)&.first&.present? if Rails.env.development?
96
+ return find_asset_source(name)&.present? if Rails.env.development?
75
97
 
76
98
  manifest.assets.key?(name)
77
99
  end
78
100
 
101
+ def find_asset_source(name)
102
+ return find_sources_fallback(name) if old_manifest?
103
+
104
+ manifest.find_sources(name)&.first
105
+ end
106
+
107
+ def find_sources_fallback(asset_path)
108
+ Rails.application.assets.find_asset(asset_path)
109
+ end
110
+
111
+ def old_manifest?
112
+ !manifest.respond_to?(:find_sources)
113
+ end
114
+
79
115
  def manifest
80
116
  @manifest ||= Rails.application.assets_manifest
81
117
  end
@@ -19,10 +19,12 @@ module IziLightup
19
19
  next unless version_name == :default || item.data.respond_to?(version_name)
20
20
 
21
21
  version = version_name == :default ? item.data : item.data.public_send(version_name)
22
- next unless version&.file&.exists? && version&.url&.present?
22
+ next unless version_exists?(version)
23
23
 
24
24
  url = version.url
25
- params.merge!(%i[width height].zip(version.dimensions).to_h)
25
+ dimensions = safe_dimentions(item, version)
26
+ params.merge!(dimensions) if dimensions.present?
27
+
26
28
  return image_tag(url, params) unless block_given?
27
29
 
28
30
  yield(url, params)
@@ -35,6 +37,33 @@ module IziLightup
35
37
 
36
38
  private
37
39
 
40
+ def version_exists?(version)
41
+ return true if version&.url&.present? && version.url =~ /https?:/
42
+
43
+ version&.file&.exists? && version&.url&.present?
44
+ end
45
+
46
+ def fallback_dimentions(item)
47
+ [item&.width, item&.height].compact
48
+ end
49
+
50
+ def safe_dimentions_extract(item, version)
51
+ begin
52
+ version.dimensions
53
+ rescue MiniMagick::Error => _e
54
+ fallback_dimentions(item)
55
+ end
56
+ end
57
+
58
+ def safe_dimentions(item, version)
59
+ dimensions = fallback_dimentions(item) if version.url =~ /https?:/
60
+ dimensions ||= safe_dimentions_extract(item, version)
61
+
62
+ return {} if dimensions.blank? || (dimensions.size != 2)
63
+
64
+ %i[width height].zip(dimensions).to_h
65
+ end
66
+
38
67
  def fetch_items(object, fields)
39
68
  Array.wrap(fields).map do |name|
40
69
  object.respond_to?(name) ? object.public_send(name) : nil
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IziLightup
4
- VERSION = '1.0.12'
4
+ VERSION = '1.0.17'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: izi_lightup
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.12
4
+ version: 1.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - IzikAJ
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-15 00:00:00.000000000 Z
11
+ date: 2021-02-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Utils to speed up page load by using critical css &