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



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
68
69
# File 'app/classes/compendium/presenters/option.rb', line 43

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

  case option.type.to_sym
    when :date
      out << date_field(form)

    when :dropdown
      raise ArgumentError, MISSING_CHOICES_ERROR unless option.choices?

      options = option.choices
      options = ctx.instance_exec(&options) if options.respond_to?(:call)
      out << dropdown(form, 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
# 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?
    note = t(option.note == true ? :"#{option.name}_note" : option.note)

    if defined?(AccessibleTooltip)
      return accessible_tooltip(:help, label: out, title: t("#{option.name}_note_title", default: '')) { 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(option.name)
end

#noteObject



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

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