# Core Dev: Page Controller and Page Editor refactorings

**URL:** <https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920>\
**Category:** Radiant CMS\
**Created:** [June 4, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920 "2007-06-04T17:50:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [June 4, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920/1 "2007-06-04T17:50:00Z")

</div>

Hi,

I’ve started to do some refactoring/improvment of the strucutre of  
page, page\_controller, and page\_parts. And I plan on continuing in a  
similar vein with admin/page/edit.rhtml

The goal of this particular iteration, is to make it possible for Page  
subclasses or extensions of the Page class to add has\_many,  
belongs\_to, or has\_one etc… relations to Page and NOT have to modify  
page\_controller. Since PageParts is a has\_many of Page, I have moved  
all of the logic related to the saving of page parts into an  
association extension. Page is setup to discover all association  
extensions that respond to :update\_association and pass parameters to  
and save these association appropriately.

[http://dev.radiantcms.org/radiant/attachment/ticket/508/page\_controller\_refactor.diff](http://dev.radiantcms.org/radiant/attachment/ticket/508/page_controller_refactor.diff)  
Please give me feedback on the direction I’m going!

thanks,  
Jacob

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [June 4, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920/2 "2007-06-04T17:58:01Z")

</div>

Jacob,

Looks like a good step in the right direction. I like your use of  
association extensions. However, wouldn’t it just be easier to do:

Page.new(params[:page])

or

@page.update\_attributes(params[:page])

and not have to call update\_associations?

Generally the strategy if you take this route is to make an  
attr(\_writer/\_accessor) with the desired name, then an after\_save  
callback that saves the associated models.

Sean

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [June 4, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920/3 "2007-06-04T18:16:05Z")

</div>

The equivalent of what you’ve written is currently performed by  
abstract\_model\_controller in handle\_new\_or\_edit\_post

as:

model.attributes = params[model\_symbol]

which for the case of page is essentially:

@page.attributes = params[:page]

The problem with hooking in to this behavior is that in order to save  
the page parts we need to look at:

params[:parts]

Supporting your suggestion would require much more broad sweeping  
changes.

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [June 4, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920/4 "2007-06-04T18:43:08Z")

</div>

> Supporting your suggestion would require much more broad sweeping changes.

That is part of the point, though, isn’t it? We should refactor the  
views as well so they are more model-oriented. I’d love to be able to  
change the routes file to look something like this:

map.with\_options :path\_prefix =\> “admin/” do |admin|  
admin.resources :pages, :snippets, :layouts, :users, :extensions  
end

# u.s.w.

Maybe that’s a bit too far down the road, but it’s the direction we  
should be going.

Sean

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [June 4, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920/5 "2007-06-04T19:59:38Z")

</div>

Actually, when you use map.resources, the named route for the index  
action would be ‘extensions\_url’ (with no parameters).

Sean

---

<div class="post-metadata">

**Author:** ![igotimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@igotimac](https://www.ruby-forum.com/u/igotimac)\
**Post date:** [June 4, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/core-dev-page-controller-and-page-editor-refactorings/105920/6 "2007-06-04T19:16:13Z")

</div>

> change the routes file to look something like this:
> 
> map.with\_options :path\_prefix =\> “admin/” do |admin|  
> admin.resources :pages, :snippets, :layouts, :users, :extensions  
> end

I tried this for the extensions controller, seems it should be simple  
enough right?

commented out the extensions routes and added:

map.with\_options :path\_prefix =\> “admin/” do |admin|  
admin.resources :extensions  
end

and I get:

undefined local variable or method `extension\_index\_url’ for  
#\<#Class:0x222df3c:0x222df14\>

It seems in order to be able use named routes, we have to name each  
route explicitly:

It seems that the line:  
extension.extension\_index ‘admin/extensions’, :action =\> ‘index’

is what makes “extension\_index\_url” come to life

and

```
extension.extension_indexwithanothername 'admin/extensions',

```

:action =\> ‘index’

would make “extension\_indexwithanothername\_url” come to life.

Thoughts on how to get around this?
