[LRUG] Moving logic from view to controller/model

Anthony Green Anthony.Green at bbc.co.uk
Mon Jul 28 02:02:19 PDT 2008


One of the guiding principles of Rails is Skinny Controller Fat Model.
I always endeavour to locate which aspects can be considered business logic
and pushed into the model and which is just for the view and should be in a
helper.
Presenter Patterns might also have they¹re place:
http://blog.jayfields.com/2007/03/rails-presenter-pattern.html

I don¹t think you need
<%= hidden_field_tag "user[role_ids][]", "" %>

The logic around destruction of the admin & System Administrator
relationship should definitely be in the model.

The way I think about it is that you should be able to interact with the
models directly through the console and get the same business behaviour as
if you were interacting via a browser..
I fact I always build my apps console and model first,  then controller,
then view.



On 25/7/08 18:14, "Kevin Fullerton" <kevin.fullerton at shotgun-suicide.co.uk>
wrote:

> Hi,
>  
> I¹ve got a little pet project I¹m working on with RoR and I¹ve added some
> logic to the view that I know should be in either the controller or the model.
>  
> The offending code is
>  
> <p>Roles<br/>
> <%= hidden_field_tag "user[role_ids][]", "" %>
> <% for role in Role.find(:all) %>
> <% if @user.login == 'admin' && role.role_name == 'System Administrator'  %>
>   <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role),
> :disabled => true %>
>   <%= hidden_field_tag "user[role_ids][]", role.id %>
> <% else  %>
>   <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
> <% end  %>
> <%= role.role_name %>
> <% end %>
> </p>
>  
> Basically, Role is a habtm join to User ­ I want to show all available Roles,
> clearing the habtm association if none are selected, apart from where the user
> is admin and the Role is System Administrator ­ that must always be selected
> (to ensure that no db monkeying is required when the inevitable happens and
> admin is removed from the System Administrator role)
>  
> So ­ should this go in the model in a before_save callback or in the
> controller ­ my gut says model but wanted to call on the wealth of knowledge
> here J
>  
> Many thanks
>  
> Kevin Fullerton
> 
> 
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org


-- 
Anthony Green
Client Side Developer
Future Media & Technology for BBC Audio & Music Interactive

http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
					
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20080728/b6ca9719/attachment-0003.html>


More information about the Chat mailing list