class GatherContent::Config::Element::ChoiceRadio

Attributes

options[RW]
other_option[RW]

Public Class Methods

new(name = "", required = false, label = "", microcopy = "", other_option = false) click to toggle source
# File lib/gather_content/config/elements/choice_radio.rb, line 11
def initialize(name = "", required = false, label = "", microcopy = "", other_option = false)
  super(name, required, label, microcopy)
  @other_option = other_option
  @options = []
end

Public Instance Methods

serialize(_options = nil) click to toggle source
# File lib/gather_content/config/elements/choice_radio.rb, line 17
def serialize(_options = nil)
  raise ArgumentError, "You need to supply at least one option" if options.size == 0

  if other_option
    verify_other_option_types!(options)
  else
    verify_option_types!(options)
  end

  verify_options!(options)

  super.merge({
    type: 'choice_radio',
    other_option: !!other_option,
    options: options.map{ |el| el.serialize(_options) }
  })
end
to_json(options = nil) click to toggle source
# File lib/gather_content/config/elements/choice_radio.rb, line 35
def to_json(options = nil)
  serialize.to_json(options)
end

Private Instance Methods

verify_option_types!(options) click to toggle source
# File lib/gather_content/config/elements/choice_radio.rb, line 49
def verify_option_types!(options)
  cleaned = options.select{ |opt| opt.instance_of?(GatherContent::Config::Element::Option) }

  raise ArgumentError, "Options can only be GatherContent::Config::Element::Option" if cleaned.size == 0
end
verify_options!(options) click to toggle source
# File lib/gather_content/config/elements/choice_radio.rb, line 55
def verify_options!(options)
  selected = options.select{ |opt| opt.selected }
  raise ArgumentError, "You can't select more than one ChoiceRadio Option" if selected.size > 1
end
verify_other_option_types!(options) click to toggle source
# File lib/gather_content/config/elements/choice_radio.rb, line 41
def verify_other_option_types!(options)
  cleaned = options.select{ |opt| opt.is_a?(GatherContent::Config::Element::Option) }
  last = cleaned.pop

  raise ArgumentError, "Options can only be GatherContent::Config::Element::Option" if cleaned.size == 0
  raise ArgumentError, "The last option must be a GatherContent::Config::Element::OptionOther" unless last.instance_of?(GatherContent::Config::Element::OtherOption)
end