class Interview::OptionAttribute

Attributes

html_class[RW]
use_radios[RW]

Protected Instance Methods

build_radios(b) click to toggle source
# File lib/interview/controls/option_attribute.rb, line 37
def build_radios(b)
  object = @object || find_attribute!(:object)
  get_options(object).each do |option|
    b.section html_class: 'radio' do  
      b.section style: 'label' do
        b << form_builder.radio_button(@method, option[1])
        b.space
        b.text text: option[0]
      end
    end
  end
end
build_read(b) click to toggle source
# File lib/interview/controls/option_attribute.rb, line 8
def build_read(b)
  return if value.nil? or value == ''
  object = @object || find_attribute!(:object)
  if object.class.superclass.name == 'ActiveRecord::Base'
    model = object.class.name.underscore
  else
    model = object.class.superclass.name.underscore
  end
  b << h.t("activerecord.options.#{model}.#{@method}.#{value}")
end
build_select(b) click to toggle source
# File lib/interview/controls/option_attribute.rb, line 27
def build_select(b)
  object = @object || find_attribute!(:object)
  options = [[h.t('helpers.select.prompt'), nil]]
  options += get_options(object)
  
  html_class = 'form-control'
  html_class += " #{@html_class}" if @html_class
  b << form_builder.select(@method, options, {}, {class: html_class})
end
build_write(b) click to toggle source
# File lib/interview/controls/option_attribute.rb, line 19
def build_write(b)
  if @use_radios
    build_radios(b)
  else
    build_select(b)
  end
end
get_options(object) click to toggle source
# File lib/interview/controls/option_attribute.rb, line 50
def get_options(object)
  # todo: nach ActiveModel Translation auslagern
  options = []
  general_defaults = object.class.lookup_ancestors.map do |klass|
    "#{object.class.i18n_scope}.options.#{klass.model_name.i18n_key}.#{method}"
  end
  if object.class.const_defined? "#{@method.upcase}_OPTIONS"
    opts = object.class.const_get("#{@method.upcase}_OPTIONS")
  elsif object.class.respond_to? :descendants and
        (klass = object.class.descendants.find { |c| c.const_defined? "#{@method.upcase}_OPTIONS" })
    opts = klass.const_get("#{@method.upcase}_OPTIONS")
  end
  if opts
    opts.map do |option|
      defaults = general_defaults.map do |default|
        :"#{default}.#{option}"
      end
      options << [h.t(defaults.shift, default: defaults), option]
    end
  end
  return options
end