%= f.inputs :class => "linked-fields" do %>
<%
linkables_collection = [[t('fullstack.cms.url', :default => "Url"), ""]] + Linkable.linkable_class_names.map do |klass_name|
[t("fullstack.admin.resources.#{klass_name.underscore}", :default => klass_name), klass_name]
end
%>
<%= f.input :linked_type, :as => :radio, :collection => linkables_collection, :input_html => {:class => "linked-type-field"},
:label => t('fullstack.cms.link_resource_or_url', :default => "Link resource or url")
%>
<%= f.input :url, :label => false, :input_html => {:style => ("display: none" if f.object.persisted?), :class => "linked-url-field"} %>
<% end %>
<% unless @link_associated_fields_js_included %>
<% @link_associated_fields_js_included = true %>
<% content_for :javascripts do -%>
<%= javascript_tag do %>
<%= coffee_script do %>
$(document).ready ->
update_linked_fields = (linked_fields) ->
linked_type_field = linked_fields.find(".linked-type-field")
checked_linked_type_field = linked_fields.find(".linked-type-field:checked")
linked_id_field_placeholder = linked_fields.find(".linked-id-field-placeholder")
linked_url_field = linked_fields.find(".linked-url-field")
linked_type = checked_linked_type_field.val()
target_id = linked_id_field_placeholder.attr("id")
linked_id_field_name = linked_type_field.attr("name").replace(/linked_type\]$/, "linked_id]")
if linked_type
$.get '<%= fields_admin_linkables_path %>',
type: linked_type
target: target_id
field_name: linked_id_field_name
current_type: linked_id_field_placeholder.data("current-type")
current_id: linked_id_field_placeholder.data("current-id")
linked_url_field.hide()
else
linked_id_field_placeholder.empty()
linked_url_field.show()
$(".linked-fields").each ->
linked_fields = $(@)
update_linked_fields(linked_fields)
$(".linked-type-field").live "change", ->
update_linked_fields($(@).closest(".linked-fields"))
<% end %>
<% end %>
<% end -%>
<% end %>