class Interview::PolymorphicAddLink

Attributes

nested_resource[RW]
object[RW]
style[RW]

Public Instance Methods

render() click to toggle source
# File lib/interview/controls/polymorphic_add_link.rb, line 6
def render
  if @style and respond_to?("render_#{@style}_style", true)
    return send("render_#{@style}_style")
  else
    return render_dropdown_style
  end
end

Protected Instance Methods

get_poly_classes() click to toggle source
# File lib/interview/controls/polymorphic_add_link.rb, line 49
def get_poly_classes
  object = @object || find_attribute!(:object)
  return object.class::TYPE_OPTIONS.map do |option|
    option.constantize
  end
end
render_dropdown_style() click to toggle source
# File lib/interview/controls/polymorphic_add_link.rb, line 16
def render_dropdown_style
  poly_classes = get_poly_classes
  
  dropdown = Interview::Dropdown.new parent: self, image: 'plus'
  poly_classes.each do |poly_class|
    dropdown.add_control Interview::Link.new caption: poly_class.human_name,
                                             object: poly_class.new,
                                             action: 'new',
                                             filter: { type: poly_class.name },
                                             nested_resource: @nested_resource
  end
  return dropdown.render
end
render_select_style() click to toggle source
# File lib/interview/controls/polymorphic_add_link.rb, line 30
def render_select_style
  poly_classes = get_poly_classes
  
  select_options = [[ h.t('helpers.select.prompt'), nil ]]
  select_options += poly_classes.map do |poly_class|
    [ poly_class.human_name, poly_class.name ]
  end
  
  html = ::Builder::XmlMarkup.new
  html.div do
    html << h.select_tag("add_link_class", h.options_for_select(select_options), class: 'form-control', style: 'display: inline; width: auto;')
    html.text! ' '
    link = Link.new parent: self, caption: h.t('views.add'), action: 'new', style: 'button'
    link.html_class << 'polymorphic_add_link'
    html << link.render
  end
  return html.target!
end