Just A Summary : Tag acts_as_resource, everything about acts_as_resource http://www.bofh.org.uk/articles/tag/acts_as_resource.rss en-us 40 Piers Cawley Practices Punditry Initial release of acts_as_resource <p>Right. I&#8217;ve bundled <code>acts_as_resource</code> up and stuck it on the typosphere <span class="caps">SVN</span> server. You can grab it from http://svn.typosphere.org/typo/plugins/acts_as_resource if you&#8217;re interested.</p> <p>It&#8217;s currently in what I&#8217;d call an all convention, no configuration state &#8211; if your resources don&#8217;t look pretty similar to the kind of things you get from the resource scaffolding, you&#8217;ll probably have some pain, but I expect to rectify that with coming releases. One thing I want/need to do for instance is to allow for &#8216;relative&#8217; ids in your resource url. For instance, if you&#8217;re looking at <code>/albums/10/tracks/982</code>, it&#8217;s not the most readable of permalinks&#8230; next trick is to allow you to have urls like <code>/albums/because-its-there/tracks/1</code>, ie: the first track on the album &#8216;Because it&#8217;s There&#8217;. I&#8217;m sort of expecting that you&#8217;d do that by doing:</p> <pre><code>class Album has_many :tracks acts_as_resource :uri_field =&gt; :name_dasherized end</code></pre> <pre><code>class Track belongs_to :album acts_as_list acts_as_resource :uri_field =&gt; :position, :parent =&gt; :album end</code></pre> <p>However, my first priority is to add some tests (or, more likely,Rspec specifications) so I&#8217;ve got some confidence that I&#8217;m not breaking things as I go.</p> <p>Anyhow, go grab the plugin, have a play, let me know what you think.</p> Thu, 25 Jan 2007 12:56:00 -0600 urn:uuid:4f48a68b-136b-40c4-bc5b-f885580ba284 pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2007/01/25/initial-release-of-acts_as_resource#comments Ruby rails railsguts acts_as_resource http://www.bofh.org.uk/articles/2007/01/25/initial-release-of-acts_as_resource 'acts_as_resource' progress <p>I&#8217;m very nearly ready to release <code>acts_as_resource</code>, I just have to pull up and tidy code that&#8217;s currently in my working directory&#8217;s ApplicationController and we&#8217;re laughing. However, I thought you&#8217;d like to see what my nested controller looks like. </p> <pre><code>class ChildrenController &lt; ApplicationController before_filter :fetch_resources end</code></pre> <pre><code>def index end</code></pre> <pre><code>def show end</code></pre> <pre><code>def edit end</code></pre> <pre><code>def create @child = @children.build(params[:child]) if @child.save flash[:notice] = 'Child was successfully created.' redirect_to child_path(@child) end end</code></pre> <pre><code>def update if @child.update_attributes(params[:child]) redirect_to child_path(@child) else render :action =&gt; 'edit' end end</code></pre> <pre><code>def destroy @child.destroy redirect_to children_path end</code></pre> <p>I&#8217;ve removed the <span class="caps">XML</span> responses to save vertical space, but they work pretty straightforwardly.</p> <p>Note that <code>fetch_resources</code> is absolutely generic. It is unscaffolded, uses the resourceful conventions and extra information provided by each model&#8217;s <code>acts_as_resource</code> declations to work out what instance variables should be called and sets them appropriately.</p> <p>With a little work to set up a generically named variable as well as the conventionally named ones (say, <code>resource_chain</code>) and a wee bit of simply_helpful pixie dust (polymorphic paths), it should be possible to write a ResourceController class that does <em>all</em> the work for you but allows you to alter behaviour without having to rewrite an entire action method. But that&#8217;s definitely for the <em>next</em> release of the plugin.</p> Thu, 25 Jan 2007 09:30:00 -0600 urn:uuid:3e7b86e0-e64a-45b5-be6e-b982a156e597 pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2007/01/25/acts_as_resource-progress#comments Teaching rails railsguts acts_as_resource http://www.bofh.org.uk/articles/2007/01/25/acts_as_resource-progress My first 'acts_as' plugin <p>So, you&#8217;ve upgraded to Rails 1.2.1 and you&#8217;re working on a tool to maintain a database of all the tunes you have in your various songbooks and (eventually) your record collection. You start with:</p> <pre><code>$ ./script/generate rspec_resource MusicBook title:string author_id:integer \ abstract:text $ ./script/generate rspec_resource Tune title:string composer_id:integer \ abc:text book_id:integer</code></pre> <p>You decide to come back to composers and authors later, so you set up your models<sup><a href="#fn1">1</a></sup>:</p> <pre><code>MusicBook.has_many :tunes Tune.belongs_to :music_book</code></pre> <p>And your routes:</p> <pre><code>map.resource :music_books do |book| book.resource :tunes end</code></pre> <h3>Problems start here</h3> <p>Being a cautious sort, before you start adding behaviour, you fire up a development server and go and check things with the browser. The <code>/music_books/</code> stuff works fine, but once you start looking at <code>/music_books/1/tunes</code> things start to get weird; all of a sudden your links aren&#8217;t making sense.</p> <p>The problem is, that your scaffolding is calling named routes with something like: <code>edit_tune_url @tune</code>, when, because of the nesting of your resources, they should <em>really</em> be calling <code>edit_tune_url @tune.book, @tune</code>. But how to fix things?</p> <p>Well, you <em>could</em> go through all your controllers and views, replacing all the calls to named_routes with the right version. But, if you&#8217;re anything like me, you&#8217;ll get bored stiff of the repetition after you&#8217;ve fixed up the first file. And that&#8217;s before you start fixing up your controllers to do </p> <pre><code>@tune = Book.find(params[:book_id]).tunes.find(params[:id])</code></pre> <p>So, I&#8217;m going to suggest that a better bet is to install the <code>acts_as_resource</code> plugin I&#8217;m in the process of writing.</p> <h3>Making the model pull its weight</h3> <p>Once you&#8217;ve got <code>acts_as_resource</code> installed, you can just do:</p> <pre><code>Book.acts_as_resource Tune.acts_as_resource :parent =&gt; :book</code></pre> <p>And your models will magically acquire a <code>resource_chain</code> which returns exactly the list of objects that your named routes need. I&#8217;m currently investigating the innards of named routes, but the plan is that you&#8217;ll never have to call <code>resource_chain</code> yourself, the named_route helper will do it for you and use the resulting list to build the url.</p> <p>Your model class, meanwhile, gets a handy </p> <pre><code>Tune.find_resource(params)</code></pre> <p>which will find your resource, verifying that it can be reached through the chain of resources specified in your params hash, which means we can fix up the scaffolding generator to use <code>find_resource</code> (or, more likely, a helper method that will set appropriately named instance variables in the controller).</p> <p>If I understand what simply_helpful is up to, the named_routes hack should play well with that too, which means that <code>form_for @tune</code> will get its url right without you having to remember to call <code>form_for @tune.book, @tune</code>.</p> <h3>Release date?</h3> <p>I&#8217;m not quite ready to release yet, I&#8217;m busy wrapping my head around how named routes work so I can fix &#8216;em up to use resource_chain. <code>find_resource</code> is written though.</p> <p>It&#8217;s amazing how much leverage you can get, simply by adding one class attribute and a support method to your model&#8230;</p> <p>Expect a release some time next week. However, if you&#8217;re desperate for a look, drop me a line and I&#8217;ll send you my local snapshot.</p> <p id="fn1"><sup>1</sup> Look, vertical space is precious. Pretend this is in the usual blocky type stuff you usually find in the files you&#8217;ll find in app/models/whatever.rb</p> Tue, 23 Jan 2007 17:39:00 -0600 urn:uuid:9f885978-688f-4c7e-b52a-8b6a3b122836 pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2007/01/23/my-first-acts_as-plugin#comments Ruby ruby rubyonrails plugins acts_as_resource http://www.bofh.org.uk/articles/2007/01/23/my-first-acts_as-plugin