Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 566 lines (431 sloc) 35.499 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
<?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>
Something went wrong with that request. Please try again.