Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This post by Jamis Buck about keeping Rails controllers skinny has always stuck with me. I have an app in which a zip file consisting of an XML file and some images is uploaded and used to create the model (mostly from the xml file, which points to the images).

Should all the code dealing with extracting the files from the zip and moving them to their appropriate place go in the controller or the model?

thanks!

share|improve this question

2 Answers 2

up vote 4 down vote accepted

That article is pretty much classic. Yes, you should stick to the "fat models, skinny controllers, stupid views" architecture as long as the models make sense as objects (and they almost always do).

In your case, I'm not really sure what the output "model" should look like, but let's say it's a gallery. So you can create a model like this:

class Gallery # no "< ActiveRecord" if don't work with database

  attr_accessor :zip_file_path, :xml, :images

  def initialize(params ={})
    @zip_file_path = params[:zip_file_path]
    @xml = nil
    @images = []
    @output = nil # model: html code, path to file...
  end

  def generate_model
    unzip_file! # populates @xml and @images
    transform_xml! # creates model into @output
    @output
  end

  def unzip_file!
    ...
  end

  def transform_xml!
    ...
  end

end

It's just a rough design, you might want to make it more function-oriented (@xml, @images = unzip_file(@zip_file_path) ) etc.

Controller will then act just as a mediator between model and views/user:

class GalleriesController < ApplicationController
  def new()
    # display upload form for zip_file
  end

 def create()
   # check input here, or leave it to model validations

  @output = Gallery.new(params).generate_model
 end

end
share|improve this answer

Simple answer: Yes, as you already guess, this goes into the model.

Depending on how exactly the content of the zip look, you most likely hand it to a class method of the Image model, which then unpacks and evaluates the xml an creates the database records, downloads the images or whatever needs to be done.

In the controller you should have not much more than something like: (together with whatever authentication you need to handle of course)

Image.scan( params[:image_zip] )

An alternative way would be to write a class independently if the Image model to handle this task if you don't want to get too much code into this single model.

share|improve this answer
    
Thanks I like the idea of a util class working within the model to unpack things. –  kreek Aug 17 '11 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.