module-izing has_one :page association
git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@37 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7master
parent
abcc827626
commit
c713c6f321
|
@ -3,10 +3,7 @@
|
||||||
#
|
#
|
||||||
class Beer < ActiveRecord::Base
|
class Beer < ActiveRecord::Base
|
||||||
belongs_to :brewery
|
belongs_to :brewery
|
||||||
has_one :page, :foreign_key => 'owner_id', :dependent => :destroy,
|
has_one_tuxwiki_page :owner_class => 'Beer'
|
||||||
:conditions => "pages.owner_type = 'Beer'"
|
|
||||||
before_save :ensure_page_valid
|
|
||||||
after_save :save_page
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns a list of attributes for the Page partial.
|
# Returns a list of attributes for the Page partial.
|
||||||
|
@ -25,32 +22,4 @@ class Beer < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
pattr
|
pattr
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -3,10 +3,7 @@
|
||||||
#
|
#
|
||||||
class Brewery < ActiveRecord::Base
|
class Brewery < ActiveRecord::Base
|
||||||
has_many :beers
|
has_many :beers
|
||||||
has_one :page, :foreign_key => 'owner_id', :dependent => :destroy,
|
has_one_tuxwiki_page :owner_class => 'Brewery'
|
||||||
:conditions => "pages.owner_type = 'Brewery'"
|
|
||||||
before_save :ensure_page_valid
|
|
||||||
after_save :save_page
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns a list of attributes to add into the Page display.
|
# 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 << "Available Beers: #{beers.size}"
|
||||||
pattr
|
pattr
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -9,3 +9,4 @@ end
|
||||||
|
|
||||||
require 'redcloth'
|
require 'redcloth'
|
||||||
require 'actionview_text_helper'
|
require 'actionview_text_helper'
|
||||||
|
require 'has_one_page'
|
||||||
|
|
|
@ -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
|
Reference in New Issue