Class: Interview::OptionAttribute

Inherits:
Attribute show all
Defined in:
lib/interview/option_attribute.rb

Instance Attribute Summary collapse

Attributes inherited from Attribute

#align, #caption, #caption_as_placeholder, #hide_caption, #hide_tooltip, #link, #method, #nil_value, #on_changed, #only_for, #style, #surrounding_tag

Attributes inherited from Control

#parent

Instance Method Summary collapse

Methods inherited from Attribute

#initialize, #render, #tooltip, #value

Methods inherited from Control

#ancestors, build, definition, #find_attribute, #find_attribute!, inherited, #initialize, #render, #set_attributes, #set_defaults

Constructor Details

This class inherits a constructor from Interview::Attribute

Instance Attribute Details

#html_classObject

Returns the value of attribute html_class.



4
5
6
# File 'lib/interview/option_attribute.rb', line 4

def html_class
  @html_class
end

#use_radiosObject

Returns the value of attribute use_radios.



4
5
6
# File 'lib/interview/option_attribute.rb', line 4

def use_radios
  @use_radios
end

Instance Method Details

#get_options(object) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/interview/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

#render_radiosObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/interview/option_attribute.rb', line 35

def render_radios
  object = find_attribute!(:object)
  html = Builder::XmlMarkup.new
  get_options(object).each do |option|
    html.div class: 'radio' do  
      html.label do
        html << form_builder.radio_button(@method, option[1])
        html.text! ' '
        html.text! option[0]
      end
    end
  end
  return html.target!
end

#render_readObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/interview/option_attribute.rb', line 6

def render_read
  return '' if value.nil? or value == ''
  object = find_attribute! :object
  if object.class.superclass.name == 'ActiveRecord::Base'
    model = object.class.name.underscore
  else
    model = object.class.superclass.name.underscore
  end
  return h.t "activerecord.options.#{model}.#{@method}.#{value}"
end

#render_selectObject



25
26
27
28
29
30
31
32
33
# File 'lib/interview/option_attribute.rb', line 25

def render_select
  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
  form_builder.select @method, options, {}, {class: html_class}
end

#render_writeObject



17
18
19
20
21
22
23
# File 'lib/interview/option_attribute.rb', line 17

def render_write
  if @use_radios
    render_radios
  else
    render_select
  end
end