<% constraint = form.object if constraint.component selected_component = constraint.component.id selected_space = constraint.component.participatory_space.id else selected_space = constraint.subject.try(:id) end %>

<%== "#{t(".constraint")}" %>

<%= form.select :subject_manifest, subject_manifests.map { |m| [ I18n.t("activerecord.models.#{m.model_class_name.underscore}.other"), m.name ] }, { include_blank: true }, class: "constraint-subject-selector" %>
<% subject_manifests.each do |manifest| %>
<% models = manifest.model_class_name.constantize.where(organization: current_organization) %> <%= fields_for "constraints[#{index}][subject_model][#{manifest.name}]", constraint do |subject_fields| %>
<%= subject_fields.hidden_field :subject_manifest, value: manifest.name.to_s %> <%= subject_fields.select :subject_id, models.map { |m| [translated_attribute(m.title), m.id] }, { selected: selected_space, include_blank: true }, class: "constraint-subject-model-selector" %>
<% models.each do |model| %> <%= fields_for "constraints[#{index}][subject_model][#{manifest.name}][component_model][#{model.id}]", constraint do |component_fields| %>
<%= component_fields.hidden_field :subject_id, value: model.id %> <% if model.respond_to?(:components) %> <%= component_fields.select :component_id, model.components.map { |c| [translated_attribute(c.name), c.id] }, selected: selected_component, include_blank: true %> <% else %> <%= component_fields.hidden_field :component_id, value: nil %> <% end %>
<% end %> <% end %> <% end %>
<% end %> <%= form.hidden_field :id %> <%= form.hidden_field :deleted %>