diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f1772115..9f7be213 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -78,6 +78,7 @@ module ApplicationHelper { class: "form-control #{invalid(metadata.post, attribute)} #{extra[:class]}", required: required, + disabled: metadata.disabled?, autofocus: (metadata.post.attributes.first == attribute), aria: { describedby: id_for_help(attribute), diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index c8ac0df7..49e432c6 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -148,6 +148,21 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, layout.metadata.dig(name, 'private').present? end + # Determina si el campo debería estar deshabilitado + def disabled? + layout.metadata.dig(name, 'disabled') || !writable? + end + + # Determina si el campo es de solo lectura + # + # once => el campo solo se puede modificar si estaba vacío + def writable? + case layout.metadata.dig(name, 'writable') + when 'once' then value.blank? + else true + end + end + private # Si es obligatorio no puede estar vacío diff --git a/app/models/post.rb b/app/models/post.rb index cb637f93..d0253a71 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -379,7 +379,7 @@ class Post # Ejecuta la acción de guardado en cada atributo. def save_attributes! attributes.map do |attr| - self[attr].save + self[attr].writable? && self[attr].save end.all? end end