Module: Quby::Compiler::DSL::Helpers

Includes:
ActionView::Helpers::OutputSafetyHelper, ActionView::Helpers::TagHelper
Included in:
Base, QuestionBuilder, QuestionnaireBuilder, TableBuilder
Defined in:
lib/quby/compiler/dsl/helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_question_keys_uniqueness(key, options, questionnaire) ⇒ Object



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

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



16
17
18
# File 'lib/quby/compiler/dsl/helpers.rb', line 16

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

#image_tag(source, **options) ⇒ Object



10
11
12
# File 'lib/quby/compiler/dsl/helpers.rb', line 10

def image_tag(source, **options)
  tag.img(**options.reverse_merge(src: source, alt: image_alt(source)))
end

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



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

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) }

  options = {
    width: '100%',
    preload: 'none',
    poster: poster,
    loop: loop,
    autoplay: autoplay,
    muted: autoplay,
    controls: ''
  }

  content = sources.map { |url, ext| tag.source src: url, type: "video/#{ext}" }
    .append(I18n.t('video_not_supported'))
    .yield_self(&tag.method(:safe_join))

  (:video, content, options)
end