mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 20:36:21 +00:00
optimizacion: usar menos Post#attribute_name
This commit is contained in:
parent
5c8d8aafdf
commit
a3effa6e26
1 changed files with 10 additions and 7 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue