Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save albertoperdomo/2292627 to your computer and use it in GitHub Desktop.
Save albertoperdomo/2292627 to your computer and use it in GitHub Desktop.
Duda de inherited_resources
...
def decorate_resource_or_collection(item_or_items)
klass = item_or_items.is_a?(Array) ? item_or_items.first.class.to_s : item_or_items.class.to_s
(klass + "Decorator").constantize.decorate(item_or_items)
rescue NameError
item_or_items
end
...
# coding: UTF-8
class Opinables::TraspasosController < InheritedResources::Base
actions :new, :create
before_filter :admin_required, :decorate_parent
belongs_to :hipoteca, :deposito, :polymorphic => true
def new
# Metemos un new en blanco para evitar que InheritedResources intente acceder a parent.traspasos
end
def create
producto_destino = parent_class.find(params[:id_destino])
ActiveRecord::Base.transaction do
parent.opiniones.update_all(opinable_id: producto_destino.id)
producto_destino.recalcula_datos_opiniones
parent.update_attribute("estado", Opinables::Estados::Constantes::ESTADO_NO_PUBLICADO)
parent.subtipo.update_attribute('productos_temas_count', parent.subtipo.productos_temas_count - 1)
end
redirect_to producto_destino.producto_link
end
protected
def decorate_parent
instance_variable_set("@#{parent_class.to_s.downcase}".to_sym, decorate_resource_or_collection(parent))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment