lib/goaloc/model.rb in mattknox-goaloc-0.2.10 vs lib/goaloc/model.rb in mattknox-goaloc-0.2.11

- old
+ new

@@ -30,11 +30,11 @@ self.associations = { } self.fields = { } self.options = { } self.routes = [] # of the form [:classname, [:otherclass, :classname], ...] self.foreign_keys = [] - self.validations = [] + self.validations = {} class << self # TODO: can some or all of this be moved out of here and into the model class? # thanks to Josh Ladieu for this: it's the array of things needed to get to an instance of this class def resource_tuple # this returns the minimal route to this model, or nothing, if there is no unambiguous minimal route @@ -85,19 +85,22 @@ def validates(validation_type, field, opts = { }) self.validations << opts.merge({ :val_type => validation_type, :field => field}) end - def belongs_to(m, o = { }) associate(:belongs_to, m, o) end + def belongs_to(m, o = { }) + self.validations["validates_presence_of:#{m.s}_id"] = { :type => "validates_presence_of", :target => m.s} + associate(:belongs_to, m, o) + end def has_many(m, o = { }) associate(:has_many, m, o) - m.associate(:belongs_to, self, o) unless o[:skip_belongs_to] + m.belongs_to(self, o) unless o[:skip_belongs_to] end def has_one(m, o = { }) associate(:has_one, m, o) - m.associate(:belongs_to, self, o) unless o[:skip_belongs_to] + m.belongs_to(self, o) unless o[:skip_belongs_to] end def hmt(o = { }) # this should generate the has_many :through and the requisite has_many m = o[:class] # this is the thing which this model has many of