On Oct 4, 2011, at 6:54 AM, "Hadi S." <[email protected]> wrote:
> Hi,
>
> I need to create some pages in a rake task. For what attributes i have
> to watch out?
> I already implemented the rake task but can't save pages inside it,
> because the page is invalid. I think because the slug and breadcrumb
> attributes are not set. Where does the slug and breadcrumb default
> attributes get set in the radiant source code?
Start by writing a test for some object that will do this. Make a PageCreator
or something and write the methods in there. Then, in your rake task, call the
methods on that well tested object.
If you want to just handle it in your task (you shouldn't, because it's harder
to verify that your code is correct) then at least check to see if the pages
you create are "valid?" and then handle any errors.
But there are no default slug or breadcrumb settings. In the admin UI there is
JS that will watch what you type for the title and automatically set those
fields for you. Open the new page form in the web UI and click the more link to
see those fields. Then begin typing a title and you'll see slug and breadcrumb
automatically filled in. You don't have this JS in your rake task.
Another option is to just reopen the Page class in your code and add defaults
like this:
Page.class_eval do
before_save :set_my_defaults
def set_my_defaults
# your code
end
end
But if you don't write tests, you might make incorrect assumptions about your
program. For example, you might assume that you can save without setting a slug
or breadcrumb ;-)
Does this help?
-Jim