Page_attachments with page type not set to normal

Hi,

I’m using the page_attachments extension and it is working fine, except
when I use it on a page that with a page type of anything other than
normal. I saw an old mailing list post about it, but there was no
resolution…anyone else having problems with this?

I’m running 0.6.4 Radiant with mongrel and I have some extensions
installed including default_page_parts.

Thanks!

I think I’ve got part of the solution figured out. After digging into
the code a little and poking around console, I think I understanding
what’s going on.

page_attachments is literally attached to a page with class Page. In
page_attachments_extension.rb, the PageAttachment stuff gets
class_eval’ed onto Page.


Page.class_eval {
include PageAttachmentAssociations
include PageAttachmentTags
}

Also, I see in the PageAttachment class definition:

class PageAttachment < ActiveRecord::Base

belongs_to :page
end

From looking at a page with page type “Archive” in console:
#<ArchivePage:0x25cdc48 @attributes={“slug”=>"/",
“class_name”=>“ArchivePage”, …

This is no longer a page with class Page, it is an ArchivePage and
ArchivePage hasn’t received the PageAttachment extensions.

As an experiment, I tried just duplicating the include code above and
eval’ing it on ArchivePage:


ArchivePage.class_eval {
include PageAttachmentAssociations
include PageAttachmentTags
}

I restarted my server and tried to edit the page, but got stopped with
an error:

SQLite3::SQLException: no such column: page_attachments.archive_page_id:
SELECT count(*) AS count_all FROM page_attachments WHERE
(page_attachments.archive_page_id = 1)

What I noticed after that was that PageAttachmentAssociations evals the
following when it gets included into a class:

has_many :attachments, :class_name => “PageAttachment”, :dependent =>
:destroy

Adding :foreign_key => “page_id” should clear up the error that just
stopped us:

has_many :attachments, :class_name => “PageAttachment”, :dependent =>
:destroy, :foreign_key => “page_id”

I restarted my server and tried to attach an image to the Archive page
and it worked. So now, the main issue is just figuring out a way to
include PageAttachments gracefully into all of the page types when the
extension gets activated. Anyone have any ideas?

Dave,

That sounds kind of hack-ish. The better solution is make sure
page_attachments loads before anything else. Uncomment/put this line in
config/environment.rb:

config.extensions = [:page_attachments, :all]

Although I haven’t heard of changes to a superclass not propagating to
subclasses like this, it may have to do with associations.

Sean

Sean C. wrote:

Dave,

That sounds kind of hack-ish. The better solution is make sure
page_attachments loads before anything else. Uncomment/put this line in
config/environment.rb:

config.extensions = [:page_attachments, :all]

Although I haven’t heard of changes to a superclass not propagating to
subclasses like this, it may have to do with associations.

Sean

Nice work, Sean. I undid all of my changes and this solution worked
perfectly!

Thanks!