From a3effa6e2609db32ca9b345a123b4ed8fa0c7931 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 2 Jan 2020 20:29:53 -0300 Subject: [PATCH] optimizacion: usar menos Post#attribute_name --- app/models/post.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 25beb7f..b0c6ded 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -89,14 +89,18 @@ class Post < OpenStruct # XXX: rubocop dice que tenemos que usar super cuando ya lo estamos # usando... def method_missing(mid, *args) - unless attribute? mid + # Limpiar el nombre del atributo, para que todos los ayudantes + # reciban el método en limpio + name = attribute_name mid + + unless attribute? name raise NoMethodError, I18n.t('exceptions.post.no_method', method: mid) end # Definir los attribute_* - new_attribute_was(mid) - new_attribute_changed(mid) + new_attribute_was(name) + new_attribute_changed(name) # OpenStruct super(mid, *args) @@ -108,13 +112,12 @@ class Post < OpenStruct # Detecta si es un atributo válido o no, a partir de la tabla de la # plantilla def attribute?(mid) - mid = attribute_name mid included = DEFAULT_ATTRIBUTES.include?(mid) || PRIVATE_ATTRIBUTES.include?(mid) || PUBLIC_ATTRIBUTES.include?(mid) if !included && singleton_class.method_defined?(:attributes) - included = attributes.include? attribute_name(mid) + included = attributes.include? mid end included @@ -275,7 +278,7 @@ class Post < OpenStruct end def new_attribute_was(method) - attr_was = (attribute_name(method).to_s + '_was').to_sym + attr_was = "#{method}_was".to_sym return attr_was if singleton_class.method_defined? attr_was define_singleton_method(attr_was) do @@ -290,7 +293,7 @@ class Post < OpenStruct # Pregunta si el atributo cambió def new_attribute_changed(method) - attr_changed = (attribute_name(method).to_s + '_changed?').to_sym + attr_changed = "#{method}_changed?".to_sym return attr_changed if singleton_class.method_defined? attr_changed