5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-03 23:55:46 +00:00

optimizacion: usar menos Post#attribute_name

This commit is contained in:
f 2020-01-02 20:29:53 -03:00
parent 5c8d8aafdf
commit a3effa6e26
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D

View file

@ -89,14 +89,18 @@ class Post < OpenStruct
# XXX: rubocop dice que tenemos que usar super cuando ya lo estamos # XXX: rubocop dice que tenemos que usar super cuando ya lo estamos
# usando... # usando...
def method_missing(mid, *args) 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', raise NoMethodError, I18n.t('exceptions.post.no_method',
method: mid) method: mid)
end end
# Definir los attribute_* # Definir los attribute_*
new_attribute_was(mid) new_attribute_was(name)
new_attribute_changed(mid) new_attribute_changed(name)
# OpenStruct # OpenStruct
super(mid, *args) 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 # Detecta si es un atributo válido o no, a partir de la tabla de la
# plantilla # plantilla
def attribute?(mid) def attribute?(mid)
mid = attribute_name mid
included = DEFAULT_ATTRIBUTES.include?(mid) || included = DEFAULT_ATTRIBUTES.include?(mid) ||
PRIVATE_ATTRIBUTES.include?(mid) || PRIVATE_ATTRIBUTES.include?(mid) ||
PUBLIC_ATTRIBUTES.include?(mid) PUBLIC_ATTRIBUTES.include?(mid)
if !included && singleton_class.method_defined?(:attributes) if !included && singleton_class.method_defined?(:attributes)
included = attributes.include? attribute_name(mid) included = attributes.include? mid
end end
included included
@ -275,7 +278,7 @@ class Post < OpenStruct
end end
def new_attribute_was(method) 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 return attr_was if singleton_class.method_defined? attr_was
define_singleton_method(attr_was) do define_singleton_method(attr_was) do
@ -290,7 +293,7 @@ class Post < OpenStruct
# Pregunta si el atributo cambió # Pregunta si el atributo cambió
def new_attribute_changed(method) 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 return attr_changed if singleton_class.method_defined? attr_changed