Class: Compendium::Presenters::Option

Inherits:
Base
  • Object
show all
Defined in:
app/classes/compendium/presenters/option.rb

Constant Summary collapse

MISSING_CHOICES_ERROR =
"choices must be specified"

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_s

Constructor Details

This class inherits a constructor from Compendium::Presenters::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Compendium::Presenters::Base

Instance Method Details

#input(ctx, form) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/classes/compendium/presenters/option.rb', line 45

def input(ctx, form)
  out = ActiveSupport::SafeBuffer.new

  case option.type.to_sym
    when :scalar
      out << scalar_field(form)

    when :date
      out << date_field(form)

    when :dropdown
      raise ArgumentError, MISSING_CHOICES_ERROR unless option.choices

      choices = option.choices
      choices = ctx.instance_exec(&choices) if choices.respond_to?(:call)
      out << dropdown(form, choices, option.options)

    when :boolean, :radio
      choices = if option.radio?
        raise ArgumentError, MISSING_CHOICES_ERROR unless option.choices
        option.choices
      else
        %w(true false)
      end

      choices.each.with_index { |choice, index| out << radio_button(form, choice, index) }
  end

  out
end

#label(form) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/classes/compendium/presenters/option.rb', line 11

def label(form)
  label = case option.type.to_sym
    when :boolean, :radio
      name

    else
      form.label option.name, name
  end

  out = ActiveSupport::SafeBuffer.new
  out << (:span, label, class: 'option-label')

  if option.note?
    key = option.note == true ? :"#{option.name}_note" : option.note
    note = t("options.#{key}", cascade: { offset: 2 })
    title = t("options.#{option.name}_note_title", default: '', cascade: { offset: 2 })

    if defined?(AccessibleTooltip)
      return accessible_tooltip(:help, label: out, title: title) { note }
    else
      out << (:div, note, class: 'option-note')
    end
  end

  out
end

#nameObject



7
8
9
# File 'app/classes/compendium/presenters/option.rb', line 7

def name
  t("options.#{option.name}", cascade: { offset: 2 })
end

#noteObject



38
39
40
41
42
43
# File 'app/classes/compendium/presenters/option.rb', line 38

def note
  if option.note?
    key = option.note === true ? :"#{option.name}_note" : option.note
    (:div, t(key), class: 'option-note')
  end
end