From 3d3934ac43c90e158e134b32928b42e5a8c5b66b Mon Sep 17 00:00:00 2001 From: f Date: Wed, 31 Mar 2021 15:04:04 -0300 Subject: [PATCH] algunos campos solo se escriben una vez, como los precios en la tienda --- app/helpers/application_helper.rb | 1 + app/models/metadata_template.rb | 15 +++++++++++++++ app/models/post.rb | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f177211..9f7be21 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 c8ac0df..49e432c 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 cb637f9..d0253a7 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