Module: Picturefill::ViewHelper

Defined in:
lib/picturefill/view_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract(media) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/picturefill/view_helper.rb', line 86

def extract media
  return if media.blank?
  case media 
  when /^(\d+)$/
    "(min-width: #{media}px)"
  when /^(\d+)px$/
    "(min-width: #{media})"
  when /min-width: (\d+)$/
    "(#{media}px)"
  when /min-width: (\d+)px$/
    "(#{media})"
  else 
    raise ArgumentError, "Picturefill :media attribute could not be parsed, was: #{media}"
  end
end

.filename(src) ⇒ Object



74
75
76
77
78
# File 'lib/picturefill/view_helper.rb', line 74

def filename src
  src_parts = src.split('.')
  ext = src_parts[1..-1].join('.')
  [src_parts.first, ext]
end

.ratio_attrib(ratio) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/picturefill/view_helper.rb', line 102

def ratio_attrib ratio
  ratio = ratio.to_s.delete('x')
  minor = 0
  case ratio.to_s
  when /^\d/
    major = ratio
  when /^\d.\d/
    major, minor = ratio.split '.'        
  else
    raise ArgumentError, "Invalid ratio: #{ratio}, must be a number, fx '2.5' or '2' (even 'x2' or 'x2.5')"
  end
  ratio_attribute major, minor
end

.ratio_file_name(src, ratio_opt) ⇒ Object



80
81
82
83
84
# File 'lib/picturefill/view_helper.rb', line 80

def ratio_file_name src, ratio_opt
  fn_parts = filename(src)
  ratio_opt = ratio_opt.delete('x')
  "#{fn_parts.first}_x2.#{fn_parts.last}"
end

Instance Method Details

#imgset_tag(src, srcset = nil, options = {}) ⇒ Object Also known as: imageset_tag



3
4
5
6
7
# File 'lib/picturefill/view_helper.rb', line 3

def imgset_tag src, srcset = nil, options = {}
  options.merge!(:src => src)
  options.merge!(:srcset => srcset) if srcset
   :img, nil, options
end

#picture_fallback(src, options = {}) ⇒ Object



69
70
71
# File 'lib/picturefill/view_helper.rb', line 69

def picture_fallback src, options = {}
   :noscript, (:img, nil, options.merge(src: src))
end

#picture_src(src, *args) ⇒ Object

UGLY AS HELL!!! Needs refactor :P



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
63
64
65
66
67
# File 'lib/picturefill/view_helper.rb', line 35

def picture_src src, *args
  options = args.extract_options!
  media = args.first.to_s if args.first.kind_of?(String) || args.first.kind_of?(Fixnum)

  tag = options[:tag] || :div
  ratio_opt = options.delete(:ratio)
  media_opt = Picturefill::ViewHelper.extract media unless media.blank? 

  unless media_opt && media_opt =~ /min-device-pixel-ratio/
    # use filename to provide ratio_opt
    filename = Picturefill::ViewHelper.filename(src).first
    fn = filename =~ /_x\d(\d)?/
    if fn && !ratio_opt
      ratio_opt = filename.match(/x\d(\d)?$/).to_s
    else
      auto_ratio_tag = ratio_opt[0] == 'x' unless ratio_opt.blank?
    end
    ratio = Picturefill::ViewHelper.ratio_attrib(ratio_opt) unless ratio_opt.blank?
    media_opt = [media_opt, ratio].compact.join(' and ')
  end
  
  next_content = if auto_ratio_tag
    opts = options.dup
    filename = Picturefill::ViewHelper.ratio_file_name src, ratio_opt
    opts.merge!(:ratio => ratio_opt.delete('x'))
    picture_src filename, media, opts
  end

  options.merge! :"data-media" => media_opt unless auto_ratio_tag || media_opt.blank?
  options.merge! :"data-src" => src      

  (tag, nil, options) + next_content
end

#picture_tag(alt = nil) ⇒ Object



10
11
12
13
14
# File 'lib/picturefill/view_helper.rb', line 10

def picture_tag alt = nil
  options = {}
  options.merge alt: alt if alt
   :picture, nil, options
end

#picturefill(options = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/picturefill/view_helper.rb', line 22

def picturefill options = {}, &block
  opts = {}
  alt = options.delete :alt
  clazz = options.delete :class
  opts.merge! :"data-alt" => alt unless alt.blank?
  opts.merge! "class" => clazz unless clazz.blank?
  opts.merge! :"data-picture" => true

  content = block_given? ? capture(&block) : ''
   :div, content, opts
end

#source_tag(src, *args) ⇒ Object



16
17
18
19
20
# File 'lib/picturefill/view_helper.rb', line 16

def source_tag src, *args
  options = args.extract_options!
  media = args.first.to_s if args.first.kind_of?(String) || args.first.kind_of?(Fixnum)      
  picture_src src, media, options.merge(tag: :source)
end