Received: by 10.231.169.10 with SMTP id w10mr881384iby.5.1276554673145; Mon, 14 Jun 2010 15:31:13 -0700 (PDT) X-BeenThere: israelrb@googlegroups.com Received: by 10.231.205.32 with SMTP id fo32ls187887ibb.2.p; Mon, 14 Jun 2010 15:31:12 -0700 (PDT) Received: by 10.231.79.17 with SMTP id n17mr879336ibk.19.1276554672571; Mon, 14 Jun 2010 15:31:12 -0700 (PDT) Received: by 10.231.79.17 with SMTP id n17mr879335ibk.19.1276554672383; Mon, 14 Jun 2010 15:31:12 -0700 (PDT) Return-Path: Received: from mail-iw0-f176.google.com (mail-iw0-f176.google.com [209.85.214.176]) by gmr-mx.google.com with ESMTP id fa4si3444257ibb.3.2010.06.14.15.31.11; Mon, 14 Jun 2010 15:31:11 -0700 (PDT) Received-SPF: pass (google.com: domain of vita...@gmail.com designates 209.85.214.176 as permitted sender) client-ip=209.85.214.176; Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of vita...@gmail.com designates 209.85.214.176 as permitted sender) smtp.mail=vita...@gmail.com; dkim=pass (test mode) header...@gmail.com Received: by iwn39 with SMTP id 39so900353iwn.7 for ; Mon, 14 Jun 2010 15:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:sender:received :in-reply-to:references:from:date:x-google-sender-auth:message-id :subject:to:content-type; bh=T13UxuSVVUXhEekOhQ1HvZcklKP7t2GxnTYcoM5On84=; b=vPJfRNjqWTs5lWFxePV8wSERNG9AgKjEVoDHpg7rxMAoXctYBFq/E27EQW9S+F+7OA u0+Got6WK8KBwxCMZRfp/TLCQBxhsCAjgtGSDGaAV6pKXF4XYfIaUlH6WAAaCNGGsdu1 gEH3VEGUdL9isHPSmgVf3PZgFXYbldUrQLpW4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:content-type; b=Bz2pSfwUC9G+5l3VFRwu6WOWy5E8EPs5FqQ6fxjvyKoQF64PCUi0jG//15feDeFJca Hi6OUGpwtEDjJ5T44GH5XeGfD9bCbn7ogVRO+BlwI5Ddcso8f60rn3k9Be5aIs6R28fT CunPcOC+I2dEnGU4O9JqCt/qX43RhzUwAH+cY= Received: by 10.231.126.79 with SMTP id b15mr7036506ibs.80.1276554671137; Mon, 14 Jun 2010 15:31:11 -0700 (PDT) MIME-Version: 1.0 Sender: vita...@gmail.com Received: by 10.231.70.211 with HTTP; Mon, 14 Jun 2010 15:30:51 -0700 (PDT) In-Reply-To: References: <2727d2a4-692d-4c4a-8f17-9abd5817c...@t10g2000yqg.googlegroups.com> <3125AF83-209A-4002-87C9-4E0732E78...@gmail.com> From: Vitaly Kushner Date: Tue, 15 Jun 2010 01:30:51 +0300 Message-ID: Subject: Re: session variable and model code To: israelrb@googlegroups.com Content-Type: text/plain; charset=UTF-8 On Tue, Jun 15, 2010 at 12:54 AM, Dor Kalev wrote: > MVC is a great framework! Elad, I think what you are really looking for is a > religion ;-) +1 > On Tue, Jun 15, 2010 at 12:02 AM, Elad Meidar wrote: >> >> I just vomited a little bit in my mouth ;) Indeed dogma is never right. You always have exceptions. An inexperienced craftsman is better be following the strictest rules for the purpose of not hurting himself. But with time you acquire enough knowledge to see beyond the rules and dogmas and to bend the rules when needed in a careful and elegant way. Several use-cases I had (all so far required url helpers in the model). ==== 1 * existing rails site. clean code. restful and a lot of inherited_resources use. * requirement: add an .xml api with minimal effort * some xml responses need to include urls of other xml resources (this is required by the *proper* rest api) i.e. resource /users.xml doesn't just list user ids, but as is required by the properly designed rest it includes direct urls to user resources. the most natural way to implement something like this in Rails is to just use model.to_xml or collection.to_xml. the only thing that needs to be done then is to override as_json funciton in the models where you don't want to expose the whole internal structure or need special handling (like the urls) problem: you can't construct urls in the model. simplest possible solution: give models access to controller's url helpers. And don't bring me this 'you break MVC' bullshit, because it is just that : bullshit ;) by your logic I18n breaks MVC too because it saves current locale in a global variable. the thing is, some stuff in the 'current request context' can be required all over the place. one example is current_locale, another is current_anything_that_knows_to_construct_urs_using_the_right_host_and_port_and_routes. in case of I18n they had a clear place to stick their global into as they had a separate module for it. but it still breaks the frigging MVC. in our case I couldn't care less for the purity of MVC and exposing the current_controller for the purpose of current_controller.user_path(user) was the right thing to do. ==== 2 There is another use case. I had it on one of our first projects and at the time it was solved by some other hack I don't even remember now, but if I were to implement it today I would definitely use current_controller. We needed to implement db backed html widgets. there were several kinds and they were implemented with single-table-inheritance over a single table. Again, from the usage pattern of it it made a lot of sense to just have widget.to_html. This use case is arguably less demanding to break the MVC since there *is* a fairly easy workaround (pass current view. i.e. you'd do <%= @widget.to_html(self) %> in your views, but it also looks and feels quite kludgy You can read my old blog post about it http://blog.astrails.com/2009/10/27/liberate-my-controller -- Vitaly Kushner http://twitter.com/vkushner Founder, Astrails Ltd. http://astrails.com/ Check out our blog: http://blog.astrails.com/