Module: Responsive
- Included in:
- CloudinaryHelper
- Defined in:
- lib/cloudinary/responsive.rb
Instance Method Summary collapse
-
#generate_breakpoints(srcset) ⇒ Object
Calculate breakpoints for the given configuration.
-
#generate_image_responsive_attributes(public_id, attributes = {}, srcset_data = {}, options = {}) ⇒ Object
Generate the srcset and sizes attributes.
-
#generate_media_attribute(options) ⇒ Object
Generate media attribute value of the HTML img tag.
-
#generate_scaled_url(public_id, width, transformation = {}, options = {}) ⇒ Object
Generate a resource URL scaled to the given width.
-
#generate_sizes_attribute(breakpoints) ⇒ Object
Generate the sizes attribute.
-
#generate_srcset_attribute(public_id, breakpoints, transformation = {}, options = {}) ⇒ Object
Generate srcset attribute value of the HTML img tag.
-
#get_or_generate_breakpoints(public_id, srcset, options) ⇒ Object
If cache is enabled, get the breakpoints from the cache.
Instance Method Details
#generate_breakpoints(srcset) ⇒ Object
Calculate breakpoints for the given configuration
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cloudinary/responsive.rb', line 4 def generate_breakpoints(srcset) return srcset[:breakpoints] if srcset[:breakpoints].is_a? Array min_width, max_width, max_images = [:min_width, :max_width, :max_images].map {|k| srcset[k]} unless [min_width, max_width, max_images].all? {|a| a.is_a? Numeric} throw 'Either (min_width, max_width, max_images) or breakpoints must be provided to the image srcset attribute' end if min_width > max_width throw 'min_width must be less than max_width' end if max_images <= 0 throw 'max_images must be a positive integer' elsif max_images === 1 min_width = max_width end step_size = ((max_width - min_width).to_f / [max_images - 1, 1].max).ceil current = min_width breakpoints = [] while current < max_width do breakpoints.push(current) current += step_size end breakpoints.push(max_width) end |
#generate_image_responsive_attributes(public_id, attributes = {}, srcset_data = {}, options = {}) ⇒ Object
Generate the srcset and sizes attributes
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cloudinary/responsive.rb', line 31 def generate_image_responsive_attributes(public_id, attributes = {}, srcset_data = {}, = {}) # Create both srcset and sizes here to avoid fetching breakpoints twice responsive_attributes = {} generate_srcset = !attributes[:srcset] if srcset_data.empty? return responsive_attributes elsif srcset_data.is_a? String responsive_attributes[:srcset] = srcset_data return responsive_attributes end generate_sizes = !attributes[:sizes] && srcset_data[:sizes] if generate_srcset || generate_sizes breakpoints = get_or_generate_breakpoints(public_id, srcset_data, ) if generate_srcset transformation = srcset_data[:transformation] srcset_attr = generate_srcset_attribute(public_id, breakpoints, transformation, ) responsive_attributes[:srcset] = srcset_attr unless srcset_attr.empty? end if generate_sizes sizes_attr = generate_sizes_attribute(breakpoints) responsive_attributes[:sizes] = sizes_attr unless sizes_attr.empty? end end responsive_attributes end |
#generate_media_attribute(options) ⇒ Object
Generate media attribute value of the HTML img tag
97 98 99 100 101 102 103 |
# File 'lib/cloudinary/responsive.rb', line 97 def generate_media_attribute() ||= {} [:min_width, :max_width] .select {|name| [name]} .map {|name| "(#{name.to_s.tr('_', '-')}: #{[name]}px)"} .join(' and ').html_safe end |
#generate_scaled_url(public_id, width, transformation = {}, options = {}) ⇒ Object
Generate a resource URL scaled to the given width
77 78 79 80 81 82 83 84 85 |
# File 'lib/cloudinary/responsive.rb', line 77 def generate_scaled_url(public_id, width, transformation={}, ={}) config_params = Cloudinary::Utils.extract_config_params() transformation ||= config_params[:raw_transformation] = Cloudinary::Utils.generate_transformation_string( [transformation.clone, {:crop => 'scale', :width => width}].reject(&:blank?)) config_params.delete :width config_params.delete :height Cloudinary::Utils.cloudinary_url public_id, config_params end |
#generate_sizes_attribute(breakpoints) ⇒ Object
Generate the sizes attribute
108 109 110 |
# File 'lib/cloudinary/responsive.rb', line 108 def generate_sizes_attribute(breakpoints) breakpoints.map{|width| "(max-width: #{width}px) #{width}px"}.join(', ') end |
#generate_srcset_attribute(public_id, breakpoints, transformation = {}, options = {}) ⇒ Object
Generate srcset attribute value of the HTML img tag
89 90 91 92 93 |
# File 'lib/cloudinary/responsive.rb', line 89 def generate_srcset_attribute(public_id, breakpoints, transformation={}, ={}) = .clone Cloudinary::Utils.patch_fetch_format() breakpoints.map{|width| "#{generate_scaled_url(public_id, width, transformation, )} #{width}w"}.join(', ').html_safe end |
#get_or_generate_breakpoints(public_id, srcset, options) ⇒ Object
If cache is enabled, get the breakpoints from the cache. If the values were not found in the cache, or cache is not enabled, generate the values.
67 68 69 70 71 72 73 |
# File 'lib/cloudinary/responsive.rb', line 67 def get_or_generate_breakpoints(public_id, srcset, ) if srcset[:use_cache] Cloudinary::Cache.get(public_id, ) || [] else generate_breakpoints(srcset) end end |