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

#hidden_field(form) ⇒ Object



76
77
78
# File 'app/classes/compendium/presenters/option.rb', line 76

def hidden_field(form)
  form.hidden_field option.name
end

#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



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 12

def label(form)
  if option.note?
    key = option.note == true ? :"#{option.name}_note" : option.note
    note = t("options.#{key}", cascade: { offset: 2 })
  end

  if option.note? && defined?(AccessibleTooltip)
    title = t("options.#{option.name}_note_title", default: '', cascade: { offset: 2 })
    tooltip = accessible_tooltip(:help, label: name, title: title) { note }
    return form.label option.name, tooltip
  else
    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')
    out << (:div, note, class: 'option-note') if option.note?
    out
  end
end

#nameObject



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

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