Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
566 lines (431 sloc) 34.7 KB
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Mr. Casals]]></title>
<link href="http://mrcasals.github.com/atom.xml" rel="self"/>
<link href="http://mrcasals.github.com/"/>
<updated>2012-04-11T19:50:49+02:00</updated>
<id>http://mrcasals.github.com/</id>
<author>
<name><![CDATA[Marc Riera]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[My first Octopress plugin: Spotify Play button]]></title>
<link href="http://mrcasals.github.com/blog/2012/04/11/my-first-octopress-plugin-spotify-play-button/"/>
<updated>2012-04-11T18:02:00+02:00</updated>
<id>http://mrcasals.github.com/blog/2012/04/11/my-first-octopress-plugin-spotify-play-button</id>
<content type="html"><![CDATA[<p>I&#8217;m a big fan of Spotify. I use it everyday, I pay my monthly fee to get rid of
those annoying ads (I don&#8217;t know what ads do people get in other countries, but
Spanish ones are really annoying&#8230; <em>really</em>). I was really happy when they
launched their <a href="https://developer.spotify.com/technologies/web-api/">Metadata API</a>, but <a href="http://www.spotify.com/se/blog/archives/2012/04/11/introducing-the-spotify-play-button/">today&#8217;s
announcement</a> is even better.</p>
<h2>Embed Spotify everywhere&#8230; even Octopress!</h2>
<p>Spotify&#8217;s new feature <a href="https://developer.spotify.com/technologies/spotify-play-button/">Play Button</a> lets you embed a single track,
an album or a playlist to any web page, giving you the possibility to put a
soundtrack anywhere. So I decided to make an Octopress plugin for this. You can
find <a href="https://github.com/mrcasals/octopress_spotify_play_plugin">its source on GitHub</a>.</p>
<p>The plugin is pretty simple. You have to use the <code>spotify</code> liquid tag and, as
param, one of the following examples.</p>
<p>In the next release it will let you specify the height and the width of the
widget. I hope it won&#8217;t take very long :)</p>
<h3>A single track</h3>
<p>Example input:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>spotify spotify:track:4vVE8YDntxjT0gld4bdd4x</span></code></pre></td></tr></table></div></figure>
<p>Example output:</p>
<div class='spotify_play_button'>
<iframe src='https://embed.spotify.com/?uri=spotify:track:4vVE8YDntxjT0gld4bdd4x ' width='300' height='380' frameborder='0' allowtransparency='true'></iframe>
</div>
<h3>An album</h3>
<p>Example input:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>spotify spotify:album:2B9q4KPjOEYu885Keo9dfX</span></code></pre></td></tr></table></div></figure>
<p>Example output:</p>
<div class='spotify_play_button'>
<iframe src='https://embed.spotify.com/?uri=spotify:album:2B9q4KPjOEYu885Keo9dfX ' width='300' height='380' frameborder='0' allowtransparency='true'></iframe>
</div>
<h3>A playlist</h3>
<p>Example input:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>spotify spotify:user:mrc2407:playlist:1O9JUUKiMqA8LXahz61fCH</span></code></pre></td></tr></table></div></figure>
<p>Example output:</p>
<div class='spotify_play_button'>
<iframe src='https://embed.spotify.com/?uri=spotify:user:mrc2407:playlist:1O9JUUKiMqA8LXahz61fCH ' width='300' height='380' frameborder='0' allowtransparency='true'></iframe>
</div>
<h3>Some independent tracks</h3>
<p>Example input:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>spotify spotify:trackset:PREFEREDTITLE:5Z7ygHQo02SUrFmcgpwsKW,1x6ACsKV4UdWS2FMuPFUiT,4bi73jCM02fMpkI11Lqmfe</span></code></pre></td></tr></table></div></figure>
<p>Example output:</p>
<div class='spotify_play_button'>
<iframe src='https://embed.spotify.com/?uri=spotify:trackset:PREFEREDTITLE:2fdMAIsNH4wY7RRda8aCfL,4ik6sbfwyVd9QoU3nUHvKI ' width='300' height='380' frameborder='0' allowtransparency='true'></iframe>
</div>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[An approach to DataMapper, an alternative to ActiveRecord]]></title>
<link href="http://mrcasals.github.com/blog/2012/02/27/approach-to-datamapper/"/>
<updated>2012-02-27T17:47:00+01:00</updated>
<id>http://mrcasals.github.com/blog/2012/02/27/approach-to-datamapper</id>
<content type="html"><![CDATA[<p><strong>Note</strong>: <em>I published this post for the first time at the <a href="http://blog.codegram.com/2011/11/datamapper-an-alternative-to-activerecord">Codegram blog</a>, so this is just a repost :)</em></p>
<p>A few days ago we started a new project at Codegram. The project (which is still in development) is a good challenge for us and a real motivator: we all were tired of using always the same technologies, so we decided to use some tools we hadn&#8217;t had opportunity to use. One of them is <a href="http://datamapper.org"><strong>DataMapper</strong></a>.</p>
<h2>So what is DataMapper and why using it?</h2>
<blockquote><p>DataMapper is an Object Relational Mapper written in Ruby. The goal is to create an ORM which is fast, thread-safe and feature rich.</p><footer><strong>DataMapper.org</strong> <cite><a href='http://datamapper.org'>datamapper.org/&hellip;</a></cite></footer></blockquote>
<blockquote><p>Datamapper is an object-relational mapper library written in Ruby and commonly used with Merb. It was developed to address perceived shortcomings in Ruby on Rails&#8217; ActiveRecord library.</p><footer><strong>Wikipedia</strong> <cite><a href='http://en.wikipedia.org/wiki/Datamapper'>en.wikipedia.org/wiki/&hellip;</a></cite></footer></blockquote>
<p>So, as the Wikipedia article says, DataMapper is an alternative to ActiveRecord. So why using it instead of the default ActiveRecord?</p>
<ul>
<li>No need to write structural migrations</li>
<li>Scoped relations</li>
<li>Lazy loading on certain attribute types</li>
<li>Strategic eager loading</li>
<li>Default support for composite and natural keys</li>
</ul>
<p>As you can see, DataMapper has some really interesting features. This post will be a little introduction to this ORM, let&#8217;s see some of them.</p>
<!--more-->
<h2>Using DataMapper with Rails 3.1</h2>
<p>First of all, we have to add Datamapper to our Rails project. I haven&#8217;t found any metagem to include Rails and DataMapper basic gems, so we&#8217;ll have to use this in our Gemfile:</p>
<figure class='code'><figcaption><span>Gemfile </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">source</span> <span class="ss">:rubygems</span>
</span><span class='line'>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;actionmailer&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;actionpack&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;activemodel&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;activeresource&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;activesupport&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;tzinfo&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="sx">%w{core constraints migrations transactions timestamps do-adapter rails active_model sqlite-adapter}</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">dm</span><span class="o">-</span><span class="n">gem</span><span class="o">|</span>
</span><span class='line'> <span class="n">gem</span> <span class="s2">&quot;dm-</span><span class="si">#{</span><span class="n">dm</span><span class="o">-</span><span class="n">gem</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">git</span><span class="p">:</span> <span class="s2">&quot;git://github.com/datamapper/dm-</span><span class="si">#{</span><span class="n">dm</span><span class="o">-</span><span class="n">gem</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>You can also use the <code>data_mapper</code> gem, which installs the most commonly used DataMapper gems. Visit <a href="https://github.com/datamapper/data_mapper">its GitHub page</a> for more info.</p>
<h3>Our first DataMapper model</h3>
<p>No more config needed! <em>What now?</em> Just start coding! Don&#8217;t forget to <code>bundle install</code> first! Everything right? OK, then, here comes the important part: our first DataMapper model.</p>
<figure class='code'><figcaption><span>A typical DataMapper model (app/models/book.rb) </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Book</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">DataMapper</span><span class="o">::</span><span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Attributes</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:id</span><span class="p">,</span> <span class="no">Serial</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:title</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:synopsis</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:edition</span><span class="p">,</span> <span class="nb">Integer</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Wow! Amazing!</em> Yeah! Looks beautiful, right? If we check the model line by line, we find that:</p>
<ol>
<li>The model no longer inherits from ActiveModel. Oh, wait, we are not using ActiveModel anymore, so looks legit.</li>
<li>We include the <a href="https://github.com/datamapper/dm-core/blob/master/lib/dm-core/resource.rb">Resource DataMapper module</a>, which adds some basics methods and includes the Assertions module.</li>
<li>We define the <code>id</code> attribute, which will be a Serial. You can find more info about DataMapper data types <a href="http://datamapper.org/docs/properties.html">in the wiki</a>.</li>
<li>We define three more attributes: <code>title</code> and <code>synopsis</code> (which are <code>String</code>s) and <code>edition</code>, which is an <code>Integer</code>.</li>
</ol>
<p>Now that we have our model written, we have to create the database before creating our first book. Once we enter <code>rake -T</code> on our shell to see all our Rake tasks, we find two new tasks:</p>
<ul>
<li><code>rake db:automigrate</code> will <strong>destroy</strong> all our data and update de database to the last version</li>
<li><code>rake db:autoupdate</code> will update our database without destroying our data</li>
</ul>
<p><em>So what happens with <code>rake db:migrate</code>? I don&#8217;t want to lose everything every time I migrate!</em> Don&#8217;t worry, it&#8217;s aliased to <code>rake db:autoupgrade</code>, so it won&#8217;t hurt you if you&#8217;re too used to <code>rake db:migrate</code>.</p>
<p>We can now create our database with <code>rake db:create db:autoupgrade</code> and we can create our first book in the database! Let&#8217;s open our beautiful Rails console and create our first book. <code>&lt;spam&gt;</code><em>I&#8217;m using a real world example: a novel written by Patrick Rothfuss which all you fantasy-lovers should read. Oh, and people who just enjoy reading too :)</em><code>&lt;/spam&gt;</code></p>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='rbcon'><span class='line'><span class="go">:001 &gt; book = Book.create(title: &quot;The Name of the Wind&quot;, synopsis: &quot;A rare find these days, fit for lovers of fantasy and newcomers to the genre alike.&quot;, edition: 20)</span>
</span><span class='line'><span class="go"> =&gt; #&lt;Book @id=1 @title=&quot;The Name of the Wind&quot; @synopsis=&quot;A rare find these days, fit for lovers of fantasy and newcomers to the genre alike.&quot; @edition=20&gt;</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Wohoo! OMG it looks so beautiful!</em> As I know you liked this way of defining the model&#8217;s attributes, let&#8217;s take a look at migrations before we go to the controller.</p>
<h2>Structural migrations and lazy-loading</h2>
<p><em>Structural migrations?</em> I don&#8217;t know another way to call them, so I call them this way. Structural migrations are those that change a database table structure: the ones that add or remove a field, add or remove primary keys, change a field type or add or create a table. That is, all the migrations that define the current database structure. Those migrations don&#8217;t exist with DataMapper.</p>
<p>Let&#8217;s see the usual procedure with ActiveRecord (actually, the list order is not important):</p>
<ol>
<li>Think about the model&#8217;s attributes</li>
<li>Add validations to the model</li>
<li>Write a migration to create the model&#8217;s table and add attributes</li>
</ol>
<p>So we often create the migration at the last moment to ensure we don&#8217;t forget any field. And I repeat, you won&#8217;t need this last step with DataMapper. Let&#8217;s take the example from above and suppose we want to modify some model attributes, so our Book model will now look like this:</p>
<figure class='code'><figcaption><span>Our DataMapper model (app/models/book.rb) after some changes </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Book</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">DataMapper</span><span class="o">::</span><span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Attributes</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:id</span><span class="p">,</span> <span class="no">Serial</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:title</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:synopsis</span><span class="p">,</span> <span class="no">Text</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:author</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:publication_year</span><span class="p">,</span> <span class="nb">Integer</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Notice we&#8217;ve added two new attributes, <code>author</code> and <code>publication_year</code>, and we have changed <code>synopsis</code>&#8217;s type from <code>String</code> to <code>Text</code>. Let&#8217;s update our database with <code>rake db:migrate</code> and see what has changed:</p>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='rbcon'><span class='line'><span class="go"> :001 &gt; Book.first</span>
</span><span class='line'><span class="go"> =&gt; #&lt;Book @id=1 @title=&quot;The Name of the Wind&quot; @synopsis=&lt;not loaded&gt; @author=nil @publication_year=nil&gt;</span>
</span><span class='line'><span class="go"> :002 &gt; Book.first.synopsis</span>
</span><span class='line'><span class="go"> =&gt; &quot;A rare find these days, fit for lovers of fantasy and newcomers to the genre alike.&quot;</span>
</span></code></pre></td></tr></table></div></figure>
<p>So what has happened here? the database has been successfully updated to our last version: the book doesn&#8217;t have any edition number and now has an author and a publication year. <em>And what about the synopsis? What does <code>&lt;not loaded&gt;</code> mean?</em> DataMapper lazy-loads some data types to make database queries faster. This means that fields with a lot of data (like <code>Text</code> attributes) won&#8217;t be loaded until they are required, like in this example.</p>
<h3>Other migrations</h3>
<p><em>What if I really need a migration?</em> Well, that&#8217;s a good point against DataMapper: this way of migrating and updating the database is really nice, but fails when we really need to change the data. There&#8217;s a gem out there called <a href="https://github.com/datamapper/dm-migrations"><em>dm-migrations</em></a> which could help you on that, but I haven&#8217;t tried it, so I can&#8217;t tell you anything about it right now. Sorry about that :(</p>
<h2>Beautiful scoped associations</h2>
<p>Let&#8217;s see another database-related feature from DataMapper: <strong>scoped associations</strong>. To see the point, we have to add two new models and modify our Book model a little bit:</p>
<figure class='code'><figcaption><span>app/models/author.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Author</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">DataMapper</span><span class="o">::</span><span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Attributes</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:id</span><span class="p">,</span> <span class="no">Serial</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:name</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Relations</span>
</span><span class='line'> <span class="n">has</span> <span class="n">n</span><span class="p">,</span> <span class="ss">:books</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>app/models/book.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Book</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">DataMapper</span><span class="o">::</span><span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Attributes</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:id</span><span class="p">,</span> <span class="no">Serial</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:title</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:synopsis</span><span class="p">,</span> <span class="no">Text</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:publication_year</span><span class="p">,</span> <span class="nb">Integer</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Relations</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:author</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="kp">false</span>
</span><span class='line'> <span class="n">has</span> <span class="n">n</span><span class="p">,</span> <span class="ss">:topics</span><span class="p">,</span> <span class="n">through</span><span class="p">:</span> <span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>app/models/topic.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Topic</span>
</span><span class='line'> <span class="kp">include</span> <span class="no">DataMapper</span><span class="o">::</span><span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Attributes</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:id</span><span class="p">,</span> <span class="no">Serial</span>
</span><span class='line'> <span class="n">property</span> <span class="ss">:name</span><span class="p">,</span> <span class="nb">String</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># Relations</span>
</span><span class='line'> <span class="n">has</span> <span class="n">n</span><span class="p">,</span> <span class="ss">:books</span><span class="p">,</span> <span class="n">through</span><span class="p">:</span> <span class="no">Resource</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Woah, not so fast! What does <code>through: Resource</code> mean?</em> Another beautiful DataMapper feature! Stablishing relations through <code>Resource</code> makes DataMapper auto-create the table between <code>books</code> and <code>topics</code>, but that&#8217;s not all! You want beautiful things? You&#8217;ll have them. Open your <code>rails console</code> and enter the following after migrating the database:</p>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='rbcon'><span class='line'><span class="go"> :001 &gt; book = Book.first</span>
</span><span class='line'><span class="go"> =&gt; #&lt;Book @id=1 @title=&quot;The Name of the Wind&quot; @synopsis=&lt;not loaded&gt; @publication_year=nil @author_id=0&gt;</span>
</span><span class='line'><span class="go"> :002 &gt; book.author = Author.create(name: &quot;Patrick Rothfuss&quot;)</span>
</span><span class='line'><span class="go"> =&gt; #&lt;Author @id=2 @name=&quot;Patrick Rothfuss&quot;&gt;</span>
</span><span class='line'><span class="go"> :003 &gt; book.topics</span>
</span><span class='line'><span class="go"> =&gt; []</span>
</span><span class='line'><span class="go"> :004 &gt; book.topics &lt;&lt; Topic.create(name: &quot;fantasy&quot;)</span>
</span><span class='line'><span class="go"> =&gt; [#&lt;Topic @id=1 @name=&quot;fantasy&quot;&gt;]</span>
</span><span class='line'><span class="go"> :005 &gt; book.topics</span>
</span><span class='line'><span class="go"> =&gt; [#&lt;Topic @id=1 @name=&quot;fantasy&quot;&gt;]</span>
</span><span class='line'><span class="go"> :006 &gt; book.save</span>
</span><span class='line'><span class="go"> =&gt; true</span>
</span><span class='line'><span class="go"> :007 &gt; BookTopic.all</span>
</span><span class='line'><span class="go"> =&gt; [#&lt;BookTopic @book_id=1 @topic_id=1&gt;]</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>WAIT AGAIN! What&#8217;s that??</em> Amazing, right? When defining a relation through <code>Resource</code>, DataMapper creates a class so that relation becomes an object! Let&#8217;s try to destroy this one!</p>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='rbcon'><span class='line'><span class="go"> :008 &gt; BookTopic.first.destroy</span>
</span><span class='line'><span class="go"> =&gt; true</span>
</span><span class='line'><span class="go"> :009 &gt; Book.first.topics</span>
</span><span class='line'><span class="go"> =&gt; []</span>
</span><span class='line'><span class="go"> :010 &gt; BookTopic.create(book: Book.first, topic: Topic.first)</span>
</span><span class='line'><span class="go"> =&gt; #&lt;BookTopic @book_id=1 @topic_id=1&gt;</span>
</span><span class='line'><span class="go"> :011 &gt; Book.first.topics</span>
</span><span class='line'><span class="go"> =&gt; [#&lt;Topic @id=1 @name=&quot;fantasy&quot;&gt;]</span>
</span></code></pre></td></tr></table></div></figure>
<p>So you can play with relations modifying directly the relation object. Let&#8217;s keep with the scoped associations we were talking about before and try this on our <code>rails console</code> again:</p>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='rbcon'><span class='line'><span class="go"> :012 &gt; Author.first.books.topics</span>
</span><span class='line'><span class="go"> =&gt; [#&lt;Topic @id=1 @name=&quot;fantasy&quot;&gt;]</span>
</span></code></pre></td></tr></table></div></figure>
<p>That&#8217;s how we get all the topics an author has written about without using dirty Arel Table relations, try this with ActiveRecord ;) Actually, associations are a really powerful resource with DataMapper. We could keep talking about them forever, but you can also take a look at the <a href="http://datamapper.org/docs/associations.html">DataMapper associations docs page</a>, which is pretty neat and clear about that. You can also take a look at the <a href="http://datamapper.org/docs/find.html">Finding docs page</a>, where you&#8217;ll find another amazing feature with associations: <strong>queries combination</strong>.</p>
<h2>Rails controllers with DataMapper</h2>
<p>Just to end the post, I wanted to show you how a Rails controler looks with Datamapper. I&#8217;ve uploaded a gist with my BooksController so you can have a look at it:</p>
<div><script src='https://gist.github.com/1268166.js?file='></script>
<noscript><pre><code># This class is responsible for the Books REST interface.
#
class BooksController &lt; ApplicationController
# Get a book by the id
before_filter :get_book, only: [:edit, :update, :show, :destroy]
# Renders the form to create a new Book.
#
def new
@book = Book.new
end
# Creates a new Book from the params and redirects to edit view.
#
def create
@book = Book.create(params[:book])
redirect_to book_path(@book)
end
# Renders the form for a given book.
#
def edit
end
# Updates a Book from the params and redirects to edit view.
#
def update
@book.update(params[:book])
redirect_to edit_book_path(@book)
end
# Renders all books
#
def index
@books = Book.all
end
# Renders a Book
#
def show
end
# Destroys the Book object from database
#
def destroy
@book.destroy
redirect_to action: :index
end
private
def get_book
@book = Book.get(params[:id])
end
end
</code></pre></noscript></div>
<p>As you see, it&#8217;s very similar to an ActiveRecord one, except for one thing: <strong>DataMapper doesn&#8217;t have a <code>find</code> method</strong>. So use the <code>get</code> method, which works pretty similar to the first one :)</p>
<h2>Final thoughts</h2>
<p>I must say I love the DataMapper approach about the model, this way of clearly showing what attributes a model has and the clean way DataMapper migrates the database. I&#8217;ve been looking for a good way to have my models information properly sorted and easy to access, and I think I can achieve it with DataMapper and some design pattern like <a href="http://www.objectmentor.com/resources/articles/srp.pdf">Single Responsibility Principle</a>. I just wanted to introduce you to DataMapper, an ORM not as used as ActiveRecord in Rails world, but I really think you should give it a try.</p>
<p>As a final note, you can find a debate on using DataMapper or ActiveRecord <a href="http://wrangl.com/datamapper-activerecord">here</a>. You&#8217;ll find some of the points explained in this post and some more to help you decide :)</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Back online!]]></title>
<link href="http://mrcasals.github.com/blog/2012/02/26/back-online/"/>
<updated>2012-02-26T18:07:00+01:00</updated>
<id>http://mrcasals.github.com/blog/2012/02/26/back-online</id>
<content type="html"><![CDATA[<p>It&#8217;s been really long since the last time I wrote a post. A lot of things have
happened since: I started the second semester at university, I got angry with
the educational model they&#8217;re using there and I&#8217;m trying to change it. I started
organizing the <a href="http://baruco.org/">Barcelona Ruby Conference 2012 (BaRuCo)</a> (which looks
amazing, I must say) with the people at <a href="http://codegram.com/">Codegram</a>. I improved my
VIm knowledge. I learned about design patterns and became kind of obsessed with
them. I got an Android smartphone. And I found myself with the need of a place
where to talk about all those things.</p>
<p>But sometimes it&#8217;s better to get rid of everything to start again instead of
trying to keep something alive. That&#8217;s what I did with the blog: I had one, but
I didn&#8217;t use it. Now I find myself with the need of a place to save my thoughts
and things I do&#8230; But I couldn&#8217;t use the older blog. It was too unupdated and I
screwed the repo at some point, so I just deleted it and created a new one.</p>
<p>I don&#8217;t need a change, I just need to start again. That&#8217;s why I&#8217;m still using
<a href="http://octopress.org/">Octopress</a>. Besides, Octopress has implemented some nice features
since the last time I took a look at it, such as separating plugins into their
own system or linked posts, which are still in beta (but I&#8217;d love to see them
merged in master). Oh, and I just love writing posts with Markdown and VIm :)</p>
<p>The only drawback I find using Octopress is the lack of themes. I just can&#8217;t
stand another blog with this layout. Too mainstream, I&#8217;d say. So I&#8217;ll try to
create my own layout for this blog, which will be a funny challenge because I&#8217;ll
have to design something useful <em>from zero to hero</em> and take a look at Octopress
templating system (about which I could not find any docs&#8230; shame on you!).</p>
<p>I&#8217;m also thinking about taking a look at Octopress&#8217; plugins system, which looks
quite nice and easy to work with. I&#8217;ve already got a plugin in mind related to
Spotify and it will take me to create a gem to work with its API, so three
things in one :)</p>
<p>PS: this reminds me that I have to do a cleanup to me GitHub account&#8230; So much
stupid repos there.</p>
]]></content>
</entry>
</feed>