Hey Leon! very fun!
Basically what you will be doing is 'extract method' and shifting the reponsibility away from the Controller. The post reponding to this method about creating module with methods is valuable - but to really get skinny controller/fat model one needs to put the calls onto the model level. The first step is to Think about you model having methods encapsulating your if logic authorized? and add_as_subtask? now one of these may be about a specific task - then it would be an instance method - one of these may be about a Tasks in general - then that is a good canidate for a class method. One thing i like to do is to make a 'build' (and build!) class method that encapsulates a bunch of creation logic in the model. ; ie class << self def build(params) end end On Mon, Jan 25, 2010 at 5:39 AM, LeonS <[email protected]> wrote: > Hi, > > I'm really new to rails, so i programmed some stuff and today i read > some things about skinny Controllers, fat models. > My Controllers are really fat now. So i'm asking myself how can i > shrink my controllers and move the code to the models, especially in > fact of REST e.g. in focus on error codes? > > code example: > # POST /tasks > # POST /tasks.xml > def create > @authorized = false > @addsubtask=false > @task = Task.new(params[:task]) > if(@task.parent_id != nil ) #wenn parent id nicht leer ist > ueberpruefe ob Rechte fuer Uebertask da sind > #if ( current_user.is_owner_of? Task.find(@task.parent_id) ) || > ( current_user.is_moderator_of? Task.find(@task.parent_id) ) > if ( current_user.has_rights_for_task? Task.find > (@task.parent_id) , ["Owner","Moderator"]) > @authorized = true #Rechte da > @addsubtask = true > else > @authorized = false #Rechte da > @addsubtask = true > end > end > if ( ( (@authorized == false ) && (@addsubtask == false) ) || > ( (@authorized == true ) && (@addsubtask == true) ) ) > respond_to do |format| > if(@task.priority == nil) > @task.priority = Priority.find(2) > end > @task.accepts_role 'Owner', current_user > if @task.save > #Task an > if @addsubtask > @task.move_to_child_of(@task.parent_id) > end > @task.recalculate_progress_recursive > flash[:notice] = 'Task was successfully created.' > format.html { redirect_to(@task) } > format.xml { render :xml => @task, :status > => :created, :location => @task } > else > format.html { render :action => "new" } > format.xml { render :xml => @task.errors, :status > => :unprocessable_entity } > end > end > else > respond_to do |format| #BENUTZER HAT KEINE RECHTE subtask > anzulegen > format.html { render :file => "#{Rails.public_path}/ > 401.html", :status => :unauthorized } #401 page laden > format.xml { render :xml => @task.errors, :status > => :unauthorized } #statuscode bearbeiten > end > end > end > > > > greetings > LeonS > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<rubyonrails-talk%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

