I’m trying to wrap my head around this meta-programming deal and figured
I might be able to get some help from the community…
So I am using acts_as_versioned for an ActiveRecord model (Page) and I
want to be able treat the versioned Page (Page::Version) objects as I do
normal Page objects. To do this there are all the associated methods
(has_many, belongs_to etc) of the Page that need to be added to
Page::Version.
Below is an abbreviated code snippet:
class Page < ActiveRecord::Base
acts_as_versioned :if_changed => [:name, :description, :body,
:address]
belongs_to :author, :class_name => "User", :foreign_key => "author_id"
belongs_to :category
has_many :images, :order => "display_order"
has_many :attachments, :order => "display_order"
has_many :comments, :order => "created_at"
has_many :links, :order => "display_order"
has_many :references, :order => "display_order"
def to_param
address.blank? ? "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}".downcase :
address.downcase
rescue
id.to_s
end
end
And the Page::Version class
class Page::Version
def current_page
self.page
end
def to_param
current_page.to_param
end
def author
current_page.author
end
def category
current_page.category
end
def comments
current_page.comments
end
def comments_count
current_page.comments_count
end
def links
current_page.links
end
def references
current_page.references
end
def versions
current_page.versions
end
def images
current_page.images
end
def attachments
current_page.attachments
end
end
Which is really nasty!!
Any ideas on how I can DRY up the Page::Version class?
Many thanks!