require 'nitro/control/relation' module Nitro # HasMany, ManyToMany and JoinsMany class HasManyControl < RelationControl #pre :do_this, :on => :populate_object def render str = "#{emit_label}" str << emit_container_start str << emit_js if selected_items.empty? str << emit_selector(:removable => false) else removable = selected_items.size != 1 ? true : false selected_items.each do |item| str << emit_selector(:selected => item.pk) end end str << emit_container_end end private # these parts are seperated from render to make it easier # to extend and customise the HasManyControl def all_items return @all_items unless @all_items.nil? @all_items = rel.target_class.all end def selected_items if @object.saved? values else [] # gmosx, THINK: this is a hack fix! end end def emit_container_start %{
} end def emit_container_end '
' end # :removable controls wether the minus button is active # :selected denotes the oid to flag as selected in the list def emit_selector(options={}) removable = options.fetch(:removable, true) selected = options.fetch(:selected, nil) %{
} end # Inline script: override this to change behavior def emit_js %{ } end end end