How to convert a Behavior to a Page Type in a Radiant Extens


#1

ROUGH STEPS TO CONVERTING A BEHAVIOR TO A PAGE TYPE PACKAGED AS A
RADIANT EXTENSION

I’ve ported all my own behaviors to Page Types packaged in Extensions
within the last day so my mind is fresh on the topic, however this is
by no means a comprehensive or even wholly accurate guide.

Use at your own risk (SEE ALSO NOTE AT BOTTOM).:

  1. Rename the folder of your Behavior and DON’T name it end with
    Extension then move the folder from plugins to extensions so:

move

/vendor/plugins/comments_behavior

to

/vendor/extensions/comments

  1. Delete the init.rb file and create a new file called
    “your_extension_name_extension.rb” and fill it with this general:

class CommentableExtension < Radiant::Extension
version “0.1”
description “Blah blah blah which will appear as the description
of this Extension under the Extensions list in the Mental / new
Radiant.”
url “http://www.fn-group.com/

Happens when the enabled checkbox is check and saved in the

admin Extensions list
def activate
# These are Page type classes which were converted from
behaviors as detailed below
CommentBucket
Commentable
end

The opposite

def deactivate
end
end

  1. Classes already in the lib directory are auto-loaded and no longer
    need to be explicitly required. In fact the explicit requiring of the
    classes in the lib directory seems to break things, but no need to
    worry they’re already available.

  2. Rename your class appropriately, at least taking off “Behavior”
    from the end of name if it’s there, then inherit from Page not
    Behavior::Base, so:

class CommentableBehavior < Behavior::Base # becomes
class Commentable < Page # Isn’t that neat. We’re just adding right
onto the page.

  1. Comment out or delete the register line, this is no longer
    needed in a Page type and will in fact break things if it’s not removed:

register “Commentable” # becomes

register “Commentable”

  1. Remove the “define_tags do … end” enclosure. It’s no longer
    needed. Nifty Page types will know what to do with all those "tag “”
    do |tag|"s

  2. If you have disabled the page caching in your behavior change:

def cache_page?
false
end

#to

def cache?
false
end

  1. Search and replace “@page” with “self” …

  2. Encapsulate existing comments in “desc %{ what your tag does
    here … }” headers on the line above a tag definition to get a
    description to show-up in the new “Available Tags” link in the Admin
    interface.

  3. Restart the web server for your Radiant app, look for errors in
    log/mongrel.log (or later in log/production.log) – repeat.

NOTE
If you were creating an Extension from scratch you get this framework
by using “script/generate extension YourExtensionName” …)

This guide in no way demonstrates the power or architecture of the
new Radiant Extensions which go beyond the scope of behavior and can
include, among other things an admin interface, custom models, views
and routes

Loren J.
www.fn-group.com


#2

Loren,
Thanks for posting this.
BJ Clark