views:

343

answers:

3

Both my Rails model and controller code need to write files to the file system.

I would like to consolidate the logic into one method.

What's the best way to share this method across models and controllers?

Thanks!

+1  A: 

If you really need to do this, you could place a module in /lib and include it where needed.

However, if possible you should have your model take care of it. If you can supply some more details, it would be easier to steer you in the correct direction.

jdl
thanks, matthew.the problem is, the model needs to write a file that contains the contents of a render call. in other words, i'm writing a static version to disk of the page rendered by Email::Show.what do you recommend?
Crashalot
I would recommend doing it as Radar recommends, and passing the string representation of the render as the parameter to the model, which would then write it out to the file. That way the controller decides what to render, the model decides what to do with it.
Jeff Whitmire
thanks, guys.the problem has shifted: i'm handling an inbound message through ActionMailer::receive. this method gets invoked through a script run by qmail, when a certain user receives email. unfortunately, from within the ActionMailer model, i have no way of accessing render() to render_to_string() ... how can i write the static file to disk w/o duplicating code?
Crashalot
+2  A: 

I think the controller would defer the actual execution of writing a file to the file system to the model. While the controller is allowed to decide when to execute that code, it should not be responsible for it's implementation, So that code should really only be in the Model.

Matthew Vines
thanks, matthew.the problem is, the model needs to write a file that contains the contents of a render call. in other words, i'm writing a static version to disk of the page rendered by Email::Show.what do you recommend?
Crashalot
I'm not sure I completely understand what you are trying to accomplish, but it seems as though your controller knows what to write and that it should be written, which is fine. All you need to do is to use a File Writing method in your model that can take the data passed in from the controller and write it to the file system. If I am completely missing the point, let me know and I will do my best to correct my response.
Matthew Vines
OK, that could work. My question is: why should the email model know file paths and how to write out files, yet not know how to generate URLs (say, the email required a special token and therefore unique URL for the user to click in order to delete the email)?In the URL case, I'm supposed (I think) generate the URL from the controller or from the view, and pass in necessary data from the email model.Yet with the file system, I'm passing in data to the model and supposed to generate the file path (and the file) within the email model.Does this make sense?
Crashalot
First, I wouldn't let my model know where to save a file either, I would place that in a configuration file or something of that nature so that it can be changed without a recompile.
Matthew Vines
Second, I think the difference is in defined responsibilities. The role of the controller is to take the request from the user and issue the appropriate commands to the model and view to achieve the desired results. It can be argued that part of those responsibilities is putting together the URL. Otherwise the Controller is your commander on the field, it knows what to do for a given user action, and issues orders to the other two layers, but it should not actually be concerned with how that work is done.
Matthew Vines
thanks, matthew. helpful comments.
Crashalot
thanks, matthew.the problem has shifted: i'm handling an inbound message through ActionMailer::receive. this method gets invoked through a script run by qmail, when a certain user receives email. unfortunately, from within the ActionMailer model, i have no way of accessing render() to render_to_string() ... how can i write the static file to disk w/o duplicating code?
Crashalot
I'd like to see a code example of your problem so I can think of it in more concrete terms. Would you mind posting another question, or amending this one.
Matthew Vines