From 805225a0933602bdc864dafe721796fd4ab2f875 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 6 Oct 2023 10:44:21 -0300 Subject: [PATCH] feat: usar mixins para poder reciclar comportamiento #14433 --- .../metadata/always_public_concern.rb | 25 +++++++++++++++++++ .../concerns/metadata/content_concern.rb | 17 +++++++++++++ .../concerns/metadata/front_matter_concern.rb | 17 +++++++++++++ .../concerns/metadata/indexable_concern.rb | 20 +++++++++++++++ .../metadata/non_indexable_concern.rb | 20 +++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 app/models/concerns/metadata/always_public_concern.rb create mode 100644 app/models/concerns/metadata/content_concern.rb create mode 100644 app/models/concerns/metadata/front_matter_concern.rb create mode 100644 app/models/concerns/metadata/indexable_concern.rb create mode 100644 app/models/concerns/metadata/non_indexable_concern.rb diff --git a/app/models/concerns/metadata/always_public_concern.rb b/app/models/concerns/metadata/always_public_concern.rb new file mode 100644 index 00000000..40cc4b45 --- /dev/null +++ b/app/models/concerns/metadata/always_public_concern.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Metadata + # Mixin para campos que no se pueden cifrar + module AlwaysPublicConcern + extend ActiveSupport::Concern + + included do + # Siempre son públicos + # + # @return [Boolean] + def private? + false + end + + private + + def decrypt(value) + raise NotImplementedError, 'Este atributo no se cifra' + end + + alias_method :encrypt, :decrypt + end + end +end diff --git a/app/models/concerns/metadata/content_concern.rb b/app/models/concerns/metadata/content_concern.rb new file mode 100644 index 00000000..2efb6bf3 --- /dev/null +++ b/app/models/concerns/metadata/content_concern.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Metadata + # Mixin para campos que son el contenido de un documento + module ContentConcern + extend ActiveSupport::Concern + + included do + # Son parte del contenido + # + # @return [Boolean] + def front_matter? + false + end + end + end +end diff --git a/app/models/concerns/metadata/front_matter_concern.rb b/app/models/concerns/metadata/front_matter_concern.rb new file mode 100644 index 00000000..883211a7 --- /dev/null +++ b/app/models/concerns/metadata/front_matter_concern.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Metadata + # Mixin para campos que son el encabezado de un documento + module FrontMatterConcern + extend ActiveSupport::Concern + + included do + # Son parte del encabezado + # + # @return [Boolean] + def front_matter? + true + end + end + end +end diff --git a/app/models/concerns/metadata/indexable_concern.rb b/app/models/concerns/metadata/indexable_concern.rb new file mode 100644 index 00000000..d801642f --- /dev/null +++ b/app/models/concerns/metadata/indexable_concern.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Metadata + # Mixin para generar atributos indexables + module IndexableConcern + extend ActiveSupport::Concern + + included do + # Siempre indexar, a menos que sea un campo privado + # + # @return [Boolean] + def indexable? + true && !private? + end + + # Por defecto los valores indexables son el valor actual + alias_method :indexable_values, :value + end + end +end diff --git a/app/models/concerns/metadata/non_indexable_concern.rb b/app/models/concerns/metadata/non_indexable_concern.rb new file mode 100644 index 00000000..6cf807a7 --- /dev/null +++ b/app/models/concerns/metadata/non_indexable_concern.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Metadata + # Mixin para nunca generar atributos indexables + module NonIndexableConcern + extend ActiveSupport::Concern + + included do + # Nunca + def indexable? + false + end + + # Lanzar un error para poder enterarnos de la inconsistencia + def indexable_values + raise NotImplementedError, 'Este atributo no es indexable' + end + end + end +end