0

Scenario

I have an application in which I would like events logged to a third party (via a custom module leveraging Gabba mixed in to my models). The events are generated in the model, but the server side code to generate the events require access to current request's cookies.

MVC Problem

This is a specialization of the problem where the model needs data that's contained by the controller. Staying true to MVC, the model should not have access to the controller. The answer I've seen used is to pass the values needed as arguments to the model. While I agree with that approach in most cases, I don't think it's the best solution here because in general it's unknown and isn't the concern of the controller if the model is going to generate an event.

So for a concrete example:

Imagine a 'User' model has a static method called 'create'. The UsersController calls the create method with the user hash. The create method wants to publish a "user created" event to Gabba, but wants needs to resolve the google analytics cookies for the current request. It just doesn't seem right to send cookies to User#create.

What I've thought of is:

  1. Put the cookie values needed by the metrics module in some sort of global data store that is accessible by the models. (Where would be the best place?)
  2. Put the events in some sort of store, and have the controller run the event code at the end of processing the request.
Community
  • 1
  • 1
John Morales
  • 306
  • 4
  • 7
  • that's generally where service object or DCI enter the game – apneadiving Jan 04 '13 at 21:55
  • 1
    I would be leary of anything that makes your model aware of cookies. Theoretically, your model should be able to live and function outside of a web framework. That decoupling is the whole point of MVC, and is especially important if you need to do shell scripting or similar down the road. Do the cookies represent something more abstract that can be passed into the model? – keithjgrant Jan 04 '13 at 21:59
  • @keithjgrant: They mean nothing to the model, but to google analytics they correlate the server side events with the client side page events. – John Morales Jan 04 '13 at 22:03

0 Answers0