Class: Interview::OptionAttribute
- Defined in:
- lib/interview/option_attribute.rb
Instance Attribute Summary collapse
-
#html_class ⇒ Object
Returns the value of attribute html_class.
-
#use_radios ⇒ Object
Returns the value of attribute use_radios.
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
Instance Method Summary collapse
- #get_options(object) ⇒ Object
- #render_radios ⇒ Object
- #render_read ⇒ Object
- #render_select ⇒ Object
- #render_write ⇒ Object
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_class ⇒ Object
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_radios ⇒ Object
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 (object) # todo: nach ActiveModel Translation auslagern = [] 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 << [h.t(defaults.shift, default: defaults), option] end end return end |
#render_radios ⇒ Object
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 (object).each do |option| html.div class: 'radio' do html.label do html << form_builder.(@method, option[1]) html.text! ' ' html.text! option[0] end end end return html.target! end |
#render_read ⇒ Object
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_select ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/interview/option_attribute.rb', line 25 def render_select object = find_attribute!(:object) = [[h.t('helpers.select.prompt'), nil]] += (object) html_class = 'form-control' html_class += " #{@html_class}" if @html_class form_builder.select @method, , {}, {class: html_class} end |
#render_write ⇒ Object
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 |