From c713c6f321cfa2bc71a40b79fdec242de9493407 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 17 Nov 2007 08:40:46 +0000 Subject: [PATCH] module-izing has_one :page association git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@37 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7 --- app/models/beer.rb | 33 +-------------------- app/models/brewery.rb | 33 +-------------------- config/environment.rb | 1 + lib/has_one_page.rb | 68 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 lib/has_one_page.rb diff --git a/app/models/beer.rb b/app/models/beer.rb index 5b5ad61..a0ac8c0 100644 --- a/app/models/beer.rb +++ b/app/models/beer.rb @@ -3,10 +3,7 @@ # 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 + has_one_tuxwiki_page :owner_class => 'Beer' ## # Returns a list of attributes for the Page partial. @@ -25,32 +22,4 @@ class Beer < ActiveRecord::Base end pattr end - - 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 diff --git a/app/models/brewery.rb b/app/models/brewery.rb index 8263782..481a204 100644 --- a/app/models/brewery.rb +++ b/app/models/brewery.rb @@ -3,10 +3,7 @@ # class Brewery < ActiveRecord::Base has_many :beers - has_one :page, :foreign_key => 'owner_id', :dependent => :destroy, - :conditions => "pages.owner_type = 'Brewery'" - before_save :ensure_page_valid - after_save :save_page + has_one_tuxwiki_page :owner_class => 'Brewery' ## # Returns a list of attributes to add into the Page display. @@ -16,32 +13,4 @@ class Brewery < ActiveRecord::Base pattr << "Available Beers: #{beers.size}" pattr end - - 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 diff --git a/config/environment.rb b/config/environment.rb index d827c92..b68d22f 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -9,3 +9,4 @@ end require 'redcloth' require 'actionview_text_helper' +require 'has_one_page' diff --git a/lib/has_one_page.rb b/lib/has_one_page.rb new file mode 100644 index 0000000..3d3b4e4 --- /dev/null +++ b/lib/has_one_page.rb @@ -0,0 +1,68 @@ +module ActiveRecord # :nodoc: + class Base # :nodoc: + class << self + ## + # This method will add a has_one :page association and a few useful + # callbacks to the requested model. It expects to have a + # :owner_class parameter given so that it knows what the owner class + # name should be. The associated model will automatically be deleted + # when this model is deleted. + # + # The Page will automatically have the title updated from the owner's + # title field and be saved after a successful save. When a Page errors + # on validation, the errors are automatically copied into the owner so + # that the user doesn't even have to know what is going on. + # + def has_one_tuxwiki_page(options = {}) + return if self.included_modules.include?(TuxWiki::HasOnePage) + send :include, TuxWiki::HasOnePage + + # we need a class name for right now. i don't know how to inflect it + # on my own, yet. + options[:owner_class] ||= 'Page' + + class_eval do + has_one :page, :foreign_key => 'owner_id', :dependent => :destroy, + :conditions => "pages.owner_type = '#{options[:owner_class]}'" + before_save :ensure_tuxwiki_page_valid + after_save :save_tuxwiki_page + end + end + end + end +end + +module TuxWiki # :nodoc: + module HasOnePage # :nodoc: + + 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_tuxwiki_page + self.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. Also + # copies the errors from the page model into the owner model. + # + def ensure_tuxwiki_page_valid + if self.page.nil? + self.errors.add(:page, "is missing") + else + self.page.owner = self + self.page.title = self.title + self.page.errors.each do |key, val| + self.errors.add(key, val) + end if !self.page.valid? + end + return false if self.errors.size > 0 + true + end + end +end