Skip to content

Instantly share code, notes, and snippets.

@benburkert
Created September 24, 2008 20:31
Show Gist options
  • Save benburkert/12661 to your computer and use it in GitHub Desktop.
Save benburkert/12661 to your computer and use it in GitHub Desktop.
class Pais
include DataMapper::Resource
# Configuración de las relaciones
has n, :bancos
# Propiedades principales
property :id, Serial
property :nombre, String, :length => (1..50), :nullable => false
property :nombre_corto, String, :length => (1..20), :nullable => false
# Propiedades especiales
# Propiedades comunes de marca de tiempo
# created_at, updated_at, deleted_at
tiene_marcas_de_tiempo
end
class Banco
include DataMapper::Resource
# Propiedades principales
property :id, Serial
property :nombre, String, :length => (1..100), :nullable => false
property :nombre_corto, String, :length => (1..30), :nullable => false
property :sucursal, String, :length => 50
property :telefono, String, :length => 30
property :direccion, String, :length => 150
property :contacto, String, :length => 100
property :aba, String, :length => 40
property :pais_id, Integer, :nullable => false
# Configuración de las asociaciones
has n, :cuenta_bancarias
belongs_to :pais, :child_key => [:pais_id]
# Propiedades para las asociaciones
# Propiedades comunes de marca de tiempo
# created_at, updated_at, deleted_at
tiene_marcas_de_tiempo
#TODO: Hacer un plugin con metaprogramming que haga esto donde sea incluido
before :save, :checa_pais
def checa_pais
# Eliminamos cualquier error previo (limpia los anteriores antes de darle una pasada)
a_checar = :pais
self.errors[a_checar] = []
if(pais_id.nil?)
self.errors.add(a_checar, "No se especificó un país.")
throw :halt
end
temporal = Pais.get(pais_id)
if(temporal.nil?)
self.errors.add(a_checar, "Ese país no existe.")
throw :halt
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment