Module: Quby::Questionnaires::DSL::Helpers

Included in:
Base, QuestionBuilder, QuestionnaireBuilder, TableBuilder
Defined in:
lib/quby/questionnaires/dsl/helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_question_keys_uniqueness(key, options, questionnaire) ⇒ Object



21
22
23
24
25
26
# File 'lib/quby/questionnaires/dsl/helpers.rb', line 21

def check_question_keys_uniqueness(key, options, questionnaire)
  keys = QuestionBuilder.build(key, options).claimed_keys
  if keys.any? { |k| questionnaire.key_in_use? k }
    fail "#{questionnaire.key}:#{key}: A question or option with input key #{key} is already defined."
  end
end

#image_alt(source) ⇒ Object

Copied from ActionController::Base.helpers.image_alt, because it will be removed from Rails 6.0, but we want to keep using this functionality



17
18
19
# File 'lib/quby/questionnaires/dsl/helpers.rb', line 17

def image_alt(source)
  File.basename(source, ".*").sub(/-[[:xdigit:]]{32,64}\z/, "").tr("-_", " ").capitalize
end

#image_tag(*args) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/quby/questionnaires/dsl/helpers.rb', line 7

def image_tag(*args)
  options = args.extract_options!

  ActionController::Base.helpers.image_tag(
    *args, options.reverse_merge(alt: image_alt(args.first))
  )
end

#video_tag(*urls, poster: nil, autoplay: false, loop: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quby/questionnaires/dsl/helpers.rb', line 28

def video_tag(*urls, poster: nil, autoplay: false, loop: false)
  # assume the file extension is the video format
  # otherwise, the host's declared mime type is used to figure out compatibility
  # which is usually wrong
  sources = urls.map { |url| [url, url.match(/[^\.]*\z/)&.[](0).downcase] }
  sources.each { |_url, ext| raise 'unknown video url file extension' unless %w[mp4 webm].include?(ext) }

  ActionController::Base.helpers.tag.video\
    width: '100%',
    preload: 'none',
    poster: poster,
    loop: loop,
    autoplay: autoplay,
    muted: autoplay,
    controls: '' do |tag|
      sources.map { |url, ext| tag.source src: url, type: "video/#{ext}" }
        .append(I18n.t('video_not_supported'))
        .yield_self(&tag.method(:safe_join))
  end
end