Extension advice

Hello again,

I’m attempting to write a couple of extensions, and have what is
hopefully a quick question.

I have an extension that creates a table for age and extends pages
with two columns age_min, and age_max. From the edit page I can post
the id’s of age into age_min and age_max, and from the actual page can
return the values of those id’s. However, I would like to return the
name value connected to those id’s in age and not the actual id itself.

What would be a good example to look at, in order to achieve the above?

Many thanks,

Dominic

I’m not quite sure what you’re trying to do. My recommendation is to
check http://wiki.radiantcms.org/Thirdparty_Extensions for existing
extensions. Even if they don’t have what you want, there’s usually
some good examples.

Incidentally, are you trying to do something like delayed publishing,
where a page has to be a certain age before it’s visible, and it
expires after a certain age? Or am I completely misunderstanding your
age_min and age_max examples… If that’s what you’re trying to do,
check out the Scheduler extension.

-Andrew

Hi Andrew,

Thank you for replying, I’ve been looking through the extensions as
you say, but can’t really see anything close enough to what I want to
do in this particular case (from my limited knowledge). I think one of
the closest is perhaps Author, where the ID of the author name gets
entered in to the page table under the created_by column and the
Author Name gets returned from the ID on the pages table by it tag.
Its the connection between the local id and the foreign name that I
think I need to look up.

I’m a bit code-blind after all the code I’ve been looking at and
learning over the past few day’s, however I now think I have to
investigate associations and belongs_to in order to do the above for
my extension. If I’m heading down the wrong path someone please tell
me! Hopefully I’ll have a fresh look at it tomorrow, I’m hoping its
just a simple concept I have yet to digest…

I’m voluntarily building a site for a local theatre company as a
project to learn ruby/rails/radiant, so I’m learning as I go (in at
the deep end), the min_age and max_age aren’t really connected to the
other date extension I am doing. This one is just going to be a used
as an aid to find pages for particular age groups (and to show those
values on the particular events). I don’t think having two values will
prove to be the final/best solution, as a single multiple select
option box may be a better method to achieve the same ends, but this
part is working passably for now.

The previous example which you so kindly helped with was based on
Sean’s great Schedular extension. It adds start and end dates for
event pages (which are slightly different from publish dates in my
case), it also enables those values to be displayed on the page and to
order by them. As the information is stored directly on the page table
it is relatively easy for me to access it directly, the trouble I am
currently facing is matching a ID of one thing to a value in a
different DB table.

If anyone can point a good direction to look at for research that
would be great.

Thanks once again

Thank you Andrew for all this,

After hours of struggling to grasp this core concept I think finally
have it and your post arrived at just the time to reassure me I
finally got it right - the joys of learning! - it’s all looking a lot
simpler now! I think I got hung up on belongs_to being and has_many
being the wrong way around in my head and went in every wrong
direction from there.

I think my next goal is to use a single multiple select box in the
admin area to try and get the values returned as the selected options,
which I think might need a different type of association.

But, yes, once the bug’s has really bitten, I should imagine
developing it in a way very similar to what you describe - perhaps
even widening it to a more general and very simple page tagging
engine. (maybe)

Many many thanks for your kind help,

Dominic

Hi Dominic,

Ah – everything is much more clear now. What you’re describing is a
fairly core behavior of Rails. Associations are indeed the right way
to go here.

For example, say you have an extension called Ages, which you would
create with “script/generate extension Ages”. In the extension, you
have a model, AgeGroup (created with “script/generate extension_model
Ages AgeGroup”).

The create_table part of the migration for AgeGroup might look
something like (note that none of these examples have been tried yet,
so I may have minor syntax errors):

create_table do |t|
t.column :min, :integer
t.column :max, :integer
end

and the class might look like:

class AgeGroup < ActiveRecord::Base
has_many :pages
end

and somewhere else (either /app/models or /lib) have something like:

module PageExtension
belongs_to :age_group
end
Page.send(:include, AgePageExtension)

You should explicitly require this file in your age_extension.rb.

Of course, you’ll need a migration to alter pages to have an
:age_group_id column (and you’ll probably want an index on that too).

You’ll also need a controller for Admin-ing the age_groups table. It
should be simple CRUD operations, which other extensions cover pretty
well.

After you understand that, it’s time to move on to something a little
more complicated: To actually display the information, my
recommendation is to do something similar to the ArchivePages. Have
one page that lists all of the age groups with links to a child page
for each age group. The child pages would actually be a single
virtual page that gets the age-group specified in the URL. Although
code-blindness will hurt here, I recommend you look at
radiant/app/models/archive_page.rb,
radiant/app/models/archive_*_index_page.rb and
radiant/lib/archive_index_tags_and_methods.rb for an idea of what I’m
talking about.

The most important things are that ArchivePage overrides child_url
(the method responsible for making a url for a page’s child) and
find_by_url (the method responsible for returning a Page to render).
Also, tags are a good thing for dynamic content (such as a list of
pages for a certain age group).

Hope this helps,
Andrew