From c2c17af72651b2f9947c8c71f96199f6e7726664 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 16 Nov 2007 00:16:52 +0000 Subject: [PATCH] missing model git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@27 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7 --- app/models/beer.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/models/beer.rb diff --git a/app/models/beer.rb b/app/models/beer.rb new file mode 100644 index 0000000..c24cca8 --- /dev/null +++ b/app/models/beer.rb @@ -0,0 +1,38 @@ +## +# This model will represent a beverage produced by a Brewery. +# +class Beer < ActiveRecord::Base + belongs_to :brewery + has_one :page, :foreign_key => 'owner_id', :dependent => :destroy, + :conditions => "pages.owner_type = 'Beer'" + before_save :ensure_page_valid + after_save :save_page + + protected + + ## + # This model always has a Page associated. Save it before this model to make + # sure that everything is kosher with the name and whatnot. + # + def save_page + page.save + end + + ## + # This will let the Page model keep track of names being unique and all. + # Might be nil, so let the after_save hook create it. + # + def ensure_page_valid + if page.nil? + self.errors.add(:page, "is missing") + else + page.owner = self + page.title = self.title + page.errors.each do |key, val| + self.errors.add(key, val) + end if !page.valid? + end + return false if self.errors.size > 0 + true + end +end