publish some old recovered posts from 2006

This commit is contained in:
Sami Samhuri 2011-12-03 15:01:11 -08:00
parent 4e2cd84f84
commit 484d3ff54b
17 changed files with 407 additions and 345 deletions

77
css/typocode.css Normal file
View file

@ -0,0 +1,77 @@
/* Syntax highlighting */
.typocode_ruby .normal {}
.typocode_ruby .comment { color: #005; font-style: italic; }
.typocode_ruby .keyword { color: #A00; font-weight: bold; }
.typocode_ruby .method { color: #077; }
.typocode_ruby .class { color: #074; }
.typocode_ruby .module { color: #050; }
.typocode_ruby .punct { color: #447; font-weight: bold; }
.typocode_ruby .symbol { color: #099; }
.typocode_ruby .string { color: #944; background: #FFE; }
.typocode_ruby .char { color: #F07; }
.typocode_ruby .ident { color: #004; }
.typocode_ruby .constant { color: #07F; }
.typocode_ruby .regex { color: #B66; background: #FEF; }
.typocode_ruby .number { color: #F99; }
.typocode_ruby .attribute { color: #7BB; }
.typocode_ruby .global { color: #7FB; }
.typocode_ruby .expr { color: #227; }
.typocode_ruby .escape { color: #277; }
.typocode_xml .normal {}
.typocode_xml .namespace { color: #B66; font-weight: bold; }
.typocode_xml .tag { color: #F88; }
.typocode_xml .comment { color: #005; font-style: italic; }
.typocode_xml .punct { color: #447; font-weight: bold; }
.typocode_xml .string { color: #944; }
.typocode_xml .number { color: #F99; }
.typocode_xml .attribute { color: #BB7; }
.typocode_yaml .normal {}
.typocode_yaml .document { font-weight: bold; color: #07F; }
.typocode_yaml .type { font-weight: bold; color: #05C; }
.typocode_yaml .key { color: #F88; }
.typocode_yaml .comment { color: #005; font-style: italic; }
.typocode_yaml .punct { color: #447; font-weight: bold; }
.typocode_yaml .string { color: #944; }
.typocode_yaml .number { color: #F99; }
.typocode_yaml .time { color: #F99; }
.typocode_yaml .date { color: #F99; }
.typocode_yaml .ref { color: #944; }
.typocode_yaml .anchor { color: #944; }
.typocode {
background-color:#eee;
padding:2px;
margin:5px;
margin-left:1em;
margin-bottom:1em;
}
.typocode pre {
padding: 0px;
margin: 0px;
border: none;
background: transparent;
font-family: monospace;
overflow:auto;
}
.typocode .lineno {
text-align: right;
/* color: #B00;*/
font-family: monospace;
padding-right: 1em;
}
code {
font-family: "Andale Mono", "Lucida Typewriter", "Bitstream Vera Sans Mono", fixed-width, monospace;
/* font-size: 120%;*/
}
pre {
background: #555;
border: 1px dashed #5189ea;
margin-bottom: 30px;
margin-top: -10px;
padding: 5px;
}

View file

@ -0,0 +1,38 @@
<p><i>
This post is the last of a triplet on generating basic x86 Mach-O files
with Ruby. The
<a href="working-with-c-style-structs-in-ruby.html">
first post</a> introduced CStruct, a Ruby class used to serialize
simple struct-like objects, while the
second describes
<a href="basics-of-the-mach-o-file-format.html">the
structure of a simple Mach-O file</a>.
</i></p>
<h2>Symbol Tables</h2>
<p></p>
<h3>N-List structures</h3>
<p></p>
<h3>Load Command</h3>
<p></p>
<h2>Relocation Tables</h2>
<p></p>
<h2>Putting it all together</h2>
As promised I'll show you how to create a very basic Mach-O binary
that you can execute on a machine running OS X (well, any x86 machine
running Darwin but at least 99% of the time that is OS X).

View file

@ -1,16 +1,3 @@
/*
FILE ARCHIVED ON 4:12:09 Mar 21, 2006 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 2:38:27 Aug 21, 2011.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
/* Island screen styles */
body {

View file

@ -0,0 +1,8 @@
Title: Jump to view/controller in TextMate
Date: February 18, 2006
Timestamp: 1140303060
Author: sjs
Tags: hacking, rails, textmate, rails, textmate
----
<a href="http://blog.inquirylabs.com/2006/02/17/controller-to-view-and-back-again-in-textmate/trackback/">Duane</a> came up with a way to jump to the controller method for the view youre editing, or vice versa in TextMate while coding using Rails. This is a huge time-saver, thanks!

View file

@ -5,12 +5,7 @@ Author: sjs
Tags: textmate, rails, hacking, rails, snippets, textmate
----
<a href="http://web.archive.org/web/20060615034816/http://sami.samhuri.net/admin/content/edit/10" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>Some TextMate snippets for Rails Migrations</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Sun, 19 Feb 2006 06:48:00 GMT">on Saturday, February 18, 2006</span></p>
<p>My arsenal of snippets and macros in TextMate is building as I read through the rails canon, <a href="http://web.archive.org/web/20060615034816/http://www.pragmaticprogrammer.com/titles/rails/" title="Agile Web Development With Rails">Agile Web Development…</a> Im only 150 pages in so I havent had to add much so far because I started with the bundle found on the <a href="http://web.archive.org/web/20060615034816/http://wiki.rubyonrails.org/rails/pages/TextMate">rails wiki</a>. The main ones so far are for migrations.</p><p>Initially I wrote a snippet for adding a table and one for dropping a table, but I dont want to write it twice every time! If Im adding a table in <strong>up</strong> then I probably want to drop it in <strong>down</strong>.</p>
<p>My arsenal of snippets and macros in TextMate is building as I read through the rails canon, <a href="http://web.archive.org/web/20060615034816/http://www.pragmaticprogrammer.com/titles/rails/" title="Agile Web Development With Rails">Agile Web Development…</a> Im only 150 pages in so I havent had to add much so far because I started with the bundle found on the <a href="http://web.archive.org/web/20060615034816/http://wiki.rubyonrails.org/rails/pages/TextMate">rails wiki</a>. The main ones so far are for migrations.</p><p>Initially I wrote a snippet for adding a table and one for dropping a table, but I dont want to write it twice every time! If Im adding a table in <strong>up</strong> then I probably want to drop it in <strong>down</strong>.</p>
<p>What I did was create one snippet that writes both lines, then its just a matter of cut &amp; paste to get it in <strong>down</strong>. The drop_table line should be inserted in the correct method, but that doesnt seem possible. I hope Im wrong!</p>

View file

@ -0,0 +1,215 @@
Title: Obligatory Post about Ruby on Rails
Date: February 20, 2006
Timestamp: 1140424260
Author: sjs
Tags: rails, coding, hacking, migration, rails, testing
Styles: typocode
----
<p><em>Im a Rails newbie and eager to learn. I welcome any suggestions or criticism you have. You can direct them to <a href="mailto:sjs@uvic.ca">my inbox</a> or leave me a comment below.</em></p>
<p>I finally set myself up with a blog. I mailed my dad the address and mentioned that it was running <a href="http://www.typosphere.org/">Typo</a>, which is written in <a href="http://www.rubyonrails.com/">Ruby on Rails</a>. The fact that it is written in Rails was a big factor in my decision. I am currently reading <a href="http://www.pragmaticprogrammer.com/titles/rails/">Agile Web Development With Rails</a> and it will be great to use Typo as a learning tool, since I will be modifying my blog anyways regardless of what language its written in.</p>
<p>Clearly Rails made an impression on me somehow or I wouldnt be investing this time on it. But my dad asked me a very good question:</p>
> Rails? What is so special about it? I looked at your page and it looks pretty normal to me. I miss the point of this new Rails technique for web development.
<p>Its unlikely that he was surprised at my lengthy response, but I was. I have been known to write him long messages on topics that interest me. However, Ive only been learning Rails for two weeks or so. Could I possibly have so much to say about it already? Apparently I do.</p><h2>Ruby on Rails background</h2>
<p>I assume a pretty basic knowledge of what Rails is, so if youre not familiar with it nows a good time to read something on the official <a href="http://www.rubyonrails.com/">Rails website</a> and watch the infamous <a href="http://www.rubyonrails.com/screencasts">15-minute screencast</a>, where Rails creator, <a href="http://www.loudthinking.com/">David Heinemeier Hansson</a>, creates a simple blog application.</p>
<p>The screencasts are what sparked my curiosity, but they hardly scratch the surface of Rails. After that I spent hours reading whatever I could find about Rails before deciding to take the time to learn it well. As a result, a lot of what you read here will sound familiar if youve read other blogs and articles about Rails. This post wasnt planned so theres no list of references yet. I hope to add some links though so please contact me if any ideas or paraphrasing here is from your site, or if you know who I should give credit to.</p>
<h2>Rails through my eyes</h2>
<p>Rails is like my Black &amp; Decker toolkit. I have a hammer, power screwdriver, tape measure, needle-nose pliers, wire cutters, a level, etc. This is exactly what I need—no more, no less. It helps me get things done quickly and easily that would otherwise be painful and somewhat difficult. I can pick up the tools and use them without much training. Therefore I am instantly productive with them.</p>
<p>The kit is suitable for many people who need these things at home, such as myself. Companies build skyscrapers and huge malls and apartments, and they clearly need more powerful tools than I. There are others that just need to drive in a nail to hang a picture, in which case the kit I have is overkill. Theyre better off just buying and using a single hammer. I happen to fall in the big grey middle <a href="http://web.archive.org/web/20070316171839/http://poignantguide.net/ruby/chapter-3.html#section2">chunk</a>, not the other two.</p>
<p>Im a university student. I code because its satisfying and fun to create software. I do plan on coding for a living when I graduate. I dont work with ancient databases, or create monster sites like Amazon, Google, or Ebay. The last time I started coding a website from scratch I was using <a href="http://www.php.net/"><span class="caps">PHP</span></a>, that was around the turn of the millennium. [It was a fan site for a <a href="http://www.nofx.org/">favourite band</a> of mine.]</p>
<p>After a year or so I realized I didnt have the time to do it properly (ie. securely and cleanly) if I wanted it to be done relatively soon. A slightly customized <a href="http://www.mediawiki.org/wiki/MediaWiki">MediaWiki</a> promptly took its place. It did all that I needed quite well, just in a less specific way.</p>
<p>The wiki is serving my site extremely well, but theres still that itch to create my <strong>own</strong> site. I feel if Rails was around back then I may have been able to complete the project in a timely manner. I was also frustrated with <span class="caps">PHP</span>. Part of that is likely due to a lack of experience and of formal programming education at that time, but it was still not fun for me. It wasnt until I started learning Rails that I thought “<em>hey, I could create that site pretty quickly using this!</em></p>
<p>Rails fits my needs like a glove, and this is where it shines. Many professionals are making money creating sites in Rails, so Im not trying to say its for amateurs only or something equally silly.</p>
<h2>Web Frameworks and iPods?</h2>
<p>Some might say I have merely been swept up in hype and am following the herd. You may be right, and thats okay. Im going to tell you a story. There was a guy who didnt get one of the oh-so-shiny iPods for a long time, though they looked neat. His discman plays mp3 CDs, and that was good enough for him. The latest iPod, which plays video, was sufficiently cool enough for him to forget that <strong>everyone</strong> at his school has an iPod and he would be trendy just like them now.</p>
<p>Shocker ending: he is I, and I am him. Now I know why everyone has one of those shiny devices. iPods and web frameworks have little in common except that many believe both the iPod and Rails are all hype and flash. Ive realized that something creating this kind of buzz may actually just be a good product. I feel that this is the only other thing the iPod and Rails have in common: they are both <strong>damn good</strong>. Enough about the iPod, everyone hates hearing about it. My goal is to write about the other thing everyone is tired of hearing about.</p>
<h2>Why is Rails special?</h2>
<p><strong>Rails is not magic.</strong> There are no exclusive JavaScript libraries or <span class="caps">HTML</span> tags. We all have to produce pages that render in the same web browsers. My dad was correct, there <em>is</em> nothing special about my website either. Its more or less a stock Typo website.</p>
<p>So what makes developing with Rails different? For me there are four big things that set Rails apart from the alternatives:</p>
<ol>
<li>Separating data, function, and design</li>
<li>Readability (which is underrated) </li>
<li>Database migrations</li>
<li>Testing is so easy it hurts</li>
</ol>
<h3><span class="caps">MVC 101</span> <em>(or, Separating data, function, and design)</em></h3>
<p>Now Im sure youve heard about separating content from design. Rails takes that one step further from just using <span class="caps">CSS</span> to style your website. It uses whats known as the <span class="caps">MVC</span> paradigm: <strong>Model-View-Controller</strong>. This is a tried and tested development method. Id used <span class="caps">MVC</span> before in Cocoa programming on Mac <span class="caps">OS X</span>, so I was already sold on this point.</p>
<ul>
<li>The model deals with your data. If youre creating an online store you have a product model, a shopping cart model, a customer model, etc. The model takes care of storing this data in the database (persistence), and presenting it to you as an object you can manipulate at runtime.</li>
</ul>
<ul>
<li>The view deals <em>only</em> with presentation. Thats it, honestly. An interface to your app.</li>
</ul>
<ul>
<li>The controller binds the model to the view, so that when the user clicks on the <strong>Add to cart</strong> link the controller is wired to call the <code>add_product</code> method of the cart model and tell it which product to add. Then the controller takes the appropriate action such as redirecting the user to the shopping cart view.</li>
</ul>
<p>Of course this is not exclusive to Rails, but its an integral part of its design.</p>
<h3>Readability</h3>
<p>Rails, and <a href="http://www.ruby-lang.org/">Ruby</a>, both read amazingly like spoken English. This code is more or less straight out of Typo. You define relationships between objects like this:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">Article</span> <span class="punct">&lt;</span> <span class="constant">Content</span>
<span class="ident">has_many</span> <span class="symbol">:comments</span><span class="punct">,</span> <span class="symbol">:dependent</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span> <span class="symbol">:order</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">created_at ASC</span><span class="punct">"</span>
<span class="ident">has_many</span> <span class="symbol">:trackbacks</span><span class="punct">,</span> <span class="symbol">:dependent</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span> <span class="symbol">:order</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">created_at ASC</span><span class="punct">"</span>
<span class="ident">has_and_belongs_to_many</span> <span class="symbol">:categories</span><span class="punct">,</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">article_id</span><span class="punct">'</span>
<span class="ident">has_and_belongs_to_many</span> <span class="symbol">:tags</span><span class="punct">,</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">article_id</span><span class="punct">'</span>
<span class="ident">belongs_to</span> <span class="symbol">:user</span>
<span class="punct">...</span></code></pre></div>
<p><code>dependent =&gt; true</code> means <em>if an article is deleted, its comments go with it</em>. Dont worry if you dont understand it all, this is just for you to see some actual Rails code.</p>
<p>In the Comment model you have:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">Comment</span> <span class="punct">&lt;</span> <span class="constant">Content</span>
<span class="ident">belongs_to</span> <span class="symbol">:article</span>
<span class="ident">belongs_to</span> <span class="symbol">:user</span>
<span class="ident">validates_presence_of</span> <span class="symbol">:author</span><span class="punct">,</span> <span class="symbol">:body</span>
<span class="ident">validates_against_spamdb</span> <span class="symbol">:body</span><span class="punct">,</span> <span class="symbol">:url</span><span class="punct">,</span> <span class="symbol">:ip</span>
<span class="ident">validates_age_of</span> <span class="symbol">:article_id</span>
<span class="punct">...</span></code></pre></div>
<p>(I snuck in some validations as well)</p>
<p>But look how it reads! Read it out loud. Id bet that my mom would more or less follow this, and shes anything but a programmer. Thats not to say programming should be easy for grandma, <strong>but code should be easily understood by humans</strong>. Let the computer understand things that are natural for me to type, since were making it understand a common language anyways.</p>
<p>Ruby and Ruby on Rails allow and encourage you to write beautiful code. That is so much more important than you may realize, because it leads to many other virtues. Readability is obvious, and hence maintainability. You must read code to understand and modify it. Oh, and happy programmers will be more productive than frustrated programmers.</p>
<h3 id="migrations">Database Migrations</h3>
<p>Heres one more life-saver: migrations. Migrations are a way to version your database schema from within Rails. So you have a table, call it <code>albums</code>, and you want to add the date the album was released. You could modify the database directly, but thats not fun. Even if you only have one server, all your configuration will be in one central place, the app. And Rails doesnt care if you have PostgreSQL, MySQL, or SQLite behind it. You can develop and test on SQLite and deploy on MySQL and the migrations will just work in both environments.</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">AddDateReleased</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Migration</span>
<span class="keyword">def </span><span class="method">self.up</span>
<span class="ident">add_column</span> <span class="punct">"</span><span class="string">albums</span><span class="punct">",</span> <span class="punct">"</span><span class="string">date_released</span><span class="punct">",</span> <span class="symbol">:datetime</span>
<span class="constant">Albums</span><span class="punct">.</span><span class="ident">update_all</span> <span class="punct">"</span><span class="string">date_released = now()</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">def </span><span class="method">self.down</span>
<span class="ident">remove_column</span> <span class="punct">"</span><span class="string">albums</span><span class="punct">",</span> <span class="punct">"</span><span class="string">date_released</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span></code></pre></div>
<p>Then you run the migration (<code>rake migrate</code> does that) and boom, your up to date. If youre wondering, the <code>self.down</code> method indeed implies that you can take this the other direction as well. Think <code>rake migrate VERSION=X</code>.</p>
<p><em>Along with the other screencasts is one on <a href="http://www.rubyonrails.org/screencasts">migrations</a> featuring none other than David Hansson. You should take a look, its the third video.</em></p>
<h3>Testing so easy it hurts</h3>
<p>To start a rails project you type <code>rails project_name</code> and it creates a directory structure with a fresh project in it. This includes a directory appropriately called <em>test</em> which houses unit tests for the project. When you generate models and controllers it creates test stubs for you in that directory. Basically, it makes it so easy to test that youre a fool not to do it. As someone wrote on their site: <em>It means never having to say “<strong>I introduced a new bug while fixing another.</strong></em></p>
<p>Rails builds on the unit testing that comes with Ruby. On a larger scale, that means that Rails is unlikely to flop on you because it is regularly tested using the same method. Ruby is unlikely to flop for the same reason. That makes me look good as a programmer. If you code for a living then its of even more value to you.</p>
<p><em>I dont know why it hurts. Maybe it hurts developers working with other frameworks or languages to see us have it so nice and easy.</em></p>
<h2>Wrapping up</h2>
<p>Rails means I have fun doing web development instead of being frustrated (CSS hacks aside). David Hansson may be right when he said you have to have been soured by Java or <span class="caps">PHP</span> to fully appreciate Rails, but that doesnt mean you wont enjoy it if you <em>do</em> like Java or <span class="caps">PHP</span>.</p>
<p><a href="http://www.relevancellc.com/blogs/wp-trackback.php?p=31">Justin Gehtland</a> rewrote a Java app using Rails and the number of lines of code of the Rails version was very close to that of the <span class="caps">XML</span> configuration for the Java version. Java has strengths, libraries available <strong>now</strong> seems to be a big one, but its too big for my needs. If youre like me then maybe youll enjoy Rails as much as I do.</p>
<h2>Youre not done, you lied to me!</h2>
<p>Sort of… there are a few things that it seems standard to include when someone writes about how Rails saved their life and gave them hope again. For completeness sake, I feel compelled to mention some principles common amongst those who develop Rails, and those who develop on Rails. Its entirely likely that theres nothing new for you here unless youre new to Rails or to programming, in which case I encourage you to read on.</p>
<h3><span class="caps">DRY</span></h3>
<p>Rails follows the <span class="caps">DRY</span> principle religiously. That is, <strong>Dont Repeat Yourself</strong>. Like <span class="caps">MVC</span>, I was already sold on this. I had previously encountered it in <a href="http://www.pragmaticprogrammer.com/ppbook/index.shtml">The Pragmatic Programmer</a>. Apart from telling <em>some_model</em> it <code>belongs_to :other_model</code> and <em>other_model</em> that it <code>has_many :some_models</code> nothing has jumped out at me which violates this principle. However, I feel that reading a models code and seeing its relationships to other models right there is a Good Thing™.</p>
<h3>Convention over configuration <em>(or, Perceived intelligence)</em></h3>
<p>Rails developers also have the mantra “<em>convention over configuration</em>”, which you can see from the video there. (you did watch it, didnt you? ;) Basically that just means Rails has sane defaults, but is still flexible if you dont like the defaults. You dont have to write even one line of <span class="caps">SQL</span> with Rails, but if you need greater control then you <em>can</em> write your own <span class="caps">SQL</span>. A standard cliché: <em>it makes the simple things easy and the hard possible</em>.</p>
<p>Rails seems to have a level of intelligence which contributes to the wow-factor. After <a href="#migrations">these relationships</a> are defined I can now filter certain negative comments like so:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="ident">article</span> <span class="punct">=</span> <span class="constant">Article</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:first</span>
<span class="keyword">for</span> <span class="ident">comment</span> <span class="keyword">in</span> <span class="ident">article</span><span class="punct">.</span><span class="ident">comments</span> <span class="keyword">do</span>
<span class="ident">print</span> <span class="ident">comment</span> <span class="keyword">unless</span> <span class="ident">comment</span><span class="punct">.</span><span class="ident">downcase</span> <span class="punct">==</span> <span class="punct">'</span><span class="string">you suck!</span><span class="punct">'</span>
<span class="keyword">end</span></code></pre></div>
<p>Rails knows to look for the field <strong>article_id</strong> in the <strong>comments</strong> table of the database. This is just a convention. You can call it something else but then you have to tell Rails what you like to call it.</p>
<p>Rails understands pluralization, which is a detail but it makes everything feel more natural. If you have a <strong>Person</strong> model then it will know to look for the table named <strong>people</strong>.</p>
<h3>Code as you learn</h3>
<p>I love how Ive only been coding in Rails for a week or two and I can do so much already. Its natural, concise and takes care of the inane details. I love how I <em>know</em> that I dont even have to explain that migration example. Its plainly clear what it does to the database. It doesnt take long to get the basics down and once you do it goes <strong>fast</strong>.</p>

View file

@ -0,0 +1,16 @@
Title: TextMate Snippets for Rails Assertions
Date: February 20, 2006
Timestamp: 1140508320
Author: sjs
Tags: textmate, rails, coding, rails, snippets, testing, textmate
----
<p>This time Ive got a few snippets for assertions. Using these to type up your tests quickly, and then hitting <strong>⌘R</strong> to run the tests without leaving TextMate, makes testing your Rails app that much more convenient. Just when you thought it was already too easy! (Dont forget that you can use <strong>⌥⌘↓</strong> to move between your code and the corresponding test case.)</p>
<p>This time Im posting the .plist files to make it easier for you to add them to TextMate. All you need to do is copy these to <strong>~/Library/Application Support/TextMate/Bundles/Rails.tmbundle/Snippets</strong>.</p>
<p style="text-align: center;"><a href="http://sami.samhuri.net/files/assert_snippets.zip">Assertion Snippets for Rails</a></p>
<p>If anyone would rather I list them all here I can do that as well. Just leave a comment.</p>
<p><em>(I wanted to include a droplet in the zip file that will copy the snippets to the right place, but my 3-hour attempt at writing the AppleScript to do so left me feeling quite bitter. Maybe I was just mistaken in thinking it would be easy to pick up AppleScript.)</em></p>

View file

@ -5,18 +5,12 @@ Author: sjs
Tags: textmate, rails, hacking, commands, macro, rails, snippets, textmate
----
<p><em><strong><span class="caps">UPDATE</span>:</strong> I got everything working and its all packaged up <a href="2006.02.22-intelligent-migration-snippets-01-for-textmate.html">here</a>. Theres an installation script this time as well.</em></p>
<a href="http://web.archive.org/web/20060303052933/http://sami.samhuri.net/admin/content/edit/20" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>TextMate: Insert text into self.down</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Tue, 21 Feb 2006 22:55:00 GMT">on Tuesday, February 21, 2006</span></p>
<p><em><strong><span class="caps">UPDATE</span>:</strong> I got everything working and its all packaged up <a href="http://web.archive.org/web/20060303052933/http://sami.samhuri.net/articles/2006/02/22/intelligent-migration-snippets-0_1">here</a>. Theres an installation script this time as well.</em></p>
<p>Thanks to <a href="http://thread.gmane.org/gmane.editors.textmate.general/8520">a helpful thread</a> on the TextMate mailing list I have the beginning of a solution to insert text at 2 (or more) locations in a file.</p>
<p>Thanks to <a href="http://web.archive.org/web/20060303052933/http://thread.gmane.org/gmane.editors.textmate.general/8520">a helpful thread</a> on the TextMate mailing list I have the beginning of a solution to insert text at 2 (or more) locations in a file.</p>
<p>I implemented this for a new snippet I was working on for migrations, <code>rename_column</code>. Since the command is the same in self.up and self.down simply doing a reverse search for <code>rename_column</code> in my <a href="http://web.archive.org/web/20060303052933/http://sami.samhuri.net/articles/2006/02/21/textmate-move-selection-to-self-down">hackish macro</a> didnt return the cursor the desired location.</p><p>Thats enough introduction, heres the program to do the insertion:</p>
<p>I implemented this for a new snippet I was working on for migrations, <code>rename_column</code>. Since the command is the same in self.up and self.down simply doing a reverse search for <code>rename_column</code> in my <a href="2006.02.21-textmate-move-selection-to-self-down.html">hackish macro</a> didnt return the cursor the desired location.</p><p>Thats enough introduction, heres the program to do the insertion:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="comment">#!/usr/bin/env ruby</span>
@ -42,26 +36,26 @@ Tags: textmate, rails, hacking, commands, macro, rails, snippets, textmate
<span class="comment"># return the new text, escaping special chars</span>
<span class="ident">print</span> <span class="ident">up_line</span> <span class="punct">+</span> <span class="ident">lines</span><span class="punct">.</span><span class="ident">reverse</span><span class="punct">.</span><span class="ident">to_s</span><span class="punct">.</span><span class="ident">gsub</span><span class="punct">('</span><span class="string">[$`<span class="escape">\\</span>]</span><span class="punct">',</span> <span class="punct">'</span><span class="string"><span class="escape">\\\\</span>\1</span><span class="punct">').</span><span class="ident">gsub</span><span class="punct">('</span><span class="string"><span class="escape">\\</span>$<span class="escape">\\</span>$</span><span class="punct">',</span> <span class="punct">'</span><span class="string">$</span><span class="punct">')</span></code></pre></div>
<p>Save this as a command in your Rails, or <a href="http://web.archive.org/web/20060303052933/http://blog.inquirylabs.com/">syncPeople on Rails</a>, bundle. The command options should be as follows:</p>
<p>Save this as a command in your Rails, or <a href="http://blog.inquirylabs.com/">syncPeople on Rails</a>, bundle. The command options should be as follows:</p>
<ul>
<li><strong>Save:</strong> Nothing</li>
<li><strong>Input:</strong> Selected Text or Nothing</li>
<li><strong>Output:</strong> Insert as Snippet</li>
<li><strong>Activation:</strong> Whatever you want, Im going to use a macro described below and leave this empty</li>
<li><strong>Scope Selector:</strong> source.ruby.rails</li>
</ul>
<ul>
<li><strong>Save:</strong> Nothing</li>
<li><strong>Input:</strong> Selected Text or Nothing</li>
<li><strong>Output:</strong> Insert as Snippet</li>
<li><strong>Activation:</strong> Whatever you want, Im going to use a macro described below and leave this empty</li>
<li><strong>Scope Selector:</strong> source.ruby.rails</li>
</ul>
<p>The first modification it needs is to get the lines to insert as command line arguments so we can use it for other snippets. Secondly, regardless of the <strong>Re-indent pasted text</strong> setting the text returned is indented incorrectly.</p>
<p>The first modification it needs is to get the lines to insert as command line arguments so we can use it for other snippets. Secondly, regardless of the <strong>Re-indent pasted text</strong> setting the text returned is indented incorrectly.</p>
The macro Im thinking of to invoke this is tab-triggered and will simply:
<ul>
<li>Select word (<tt><strong>⌃W</strong></tt>)</li>
<li>Delete (<tt><strong></strong></tt>)</li>
<li>Select to end of file (<tt><strong>⇧⌘↓</strong></tt>)</li>
<li>Run command “Put in self.down”</li>
</ul>
<ul>
<li>Select word (<tt><strong>⌃W</strong></tt>)</li>
<li>Delete (<tt><strong></strong></tt>)</li>
<li>Select to end of file (<tt><strong>⇧⌘↓</strong></tt>)</li>
<li>Run command “Put in self.down”</li>
</ul>

View file

@ -1,14 +0,0 @@
Title: Jump to view/controller in TextMate
Date: February 18, 2006
Timestamp: 1140303060
Author: sjs
Tags: hacking, rails, textmate, rails, textmate
----
<a href="http://web.archive.org/web/20060324095447/http://sami.samhuri.net/admin/content/edit/8" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>Jump to view/controller in TextMate</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Sat, 18 Feb 2006 22:51:00 GMT">on Saturday, February 18, 2006</span></p>
<p><a href="http://web.archive.org/web/20060324095447/http://blog.inquirylabs.com/2006/02/17/controller-to-view-and-back-again-in-textmate/trackback/">Duane</a> came up with a way to jump to the controller method for the view youre editing, or vice versa in TextMate while coding using Rails. This is a huge time-saver, thanks!</p>

View file

@ -1,225 +0,0 @@
Title: Obligatory Post about Ruby on Rails
Date: February 20, 2006
Timestamp: 1140424260
Author: sjs
Tags: rails, coding, hacking, migration, rails, testing
----
<a href="http://web.archive.org/web/20070316171839/http://sami.samhuri.net/admin/content/edit/15" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>Obligatory Post about Ruby on Rails</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Mon, 20 Feb 2006 08:31:00 GMT">on Monday, February 20, 2006</span></p>
<p><em>Im a Rails newbie and eager to learn. I welcome any suggestions or criticism you have. You can direct them to <a href="mailto:sjs@uvic.ca">my inbox</a> or leave me a comment below.</em></p>
<p>I finally set myself up with a blog. I mailed my dad the address and mentioned that it was running <a href="http://web.archive.org/web/20070316171839/http://www.typosphere.org/">Typo</a>, which is written in <a href="http://web.archive.org/web/20070316171839/http://www.rubyonrails.com/">Ruby on Rails</a>. The fact that it is written in Rails was a big factor in my decision. I am currently reading <a href="http://web.archive.org/web/20070316171839/http://www.pragmaticprogrammer.com/titles/rails/">Agile Web Development With Rails</a> and it will be great to use Typo as a learning tool, since I will be modifying my blog anyways regardless of what language its written in.</p>
<p>Clearly Rails made an impression on me somehow or I wouldnt be investing this time on it. But my dad asked me a very good question:</p>
<blockquote>
<p style="border-left: 3px solid #fff; padding-left: 5px;">Rails? What is so special about it? I looked at your page and it looks pretty normal to me. I miss the point of this new Rails technique for web development.</p>
</blockquote>
<p>Its unlikely that he was surprised at my lengthy response, but I was. I have been known to write him long messages on topics that interest me. However, Ive only been learning Rails for two weeks or so. Could I possibly have so much to say about it already? Apparently I do.</p><h2>Ruby on Rails background</h2>
<p>I assume a pretty basic knowledge of what Rails is, so if youre not familiar with it nows a good time to read something on the official <a href="http://web.archive.org/web/20070316171839/http://www.rubyonrails.com/">Rails website</a> and watch the infamous <a href="http://web.archive.org/web/20070316171839/http://www.rubyonrails.com/screencasts">15-minute screencast</a>, where Rails creator, <a href="http://web.archive.org/web/20070316171839/http://www.loudthinking.com/">David Heinemeier Hansson</a>, creates a simple blog application.</p>
<p>The screencasts are what sparked my curiosity, but they hardly scratch the surface of Rails. After that I spent hours reading whatever I could find about Rails before deciding to take the time to learn it well. As a result, a lot of what you read here will sound familiar if youve read other blogs and articles about Rails. This post wasnt planned so theres no list of references yet. I hope to add some links though so please contact me if any ideas or paraphrasing here is from your site, or if you know who I should give credit to.</p>
<h2>Rails through my eyes</h2>
<p><img src="./Obligatory Post about Ruby on Rails - samhuri.net_files/tools.jpg" align="right" alt="Black &amp; Decker tools" style="margin: 5px;"> Rails is like my Black &amp; Decker toolkit. I have a hammer, power screwdriver, tape measure, needle-nose pliers, wire cutters, a level, etc. This is exactly what I need—no more, no less. It helps me get things done quickly and easily that would otherwise be painful and somewhat difficult. I can pick up the tools and use them without much training. Therefore I am instantly productive with them.</p>
<p>The kit is suitable for many people who need these things at home, such as myself. Companies build skyscrapers and huge malls and apartments, and they clearly need more powerful tools than I. There are others that just need to drive in a nail to hang a picture, in which case the kit I have is overkill. Theyre better off just buying and using a single hammer. I happen to fall in the big grey middle <a href="http://web.archive.org/web/20070316171839/http://poignantguide.net/ruby/chapter-3.html#section2">chunk</a>, not the other two.</p>
<p>Im a university student. I code because its satisfying and fun to create software. I do plan on coding for a living when I graduate. I dont work with ancient databases, or create monster sites like Amazon, Google, or Ebay. The last time I started coding a website from scratch I was using <a href="http://web.archive.org/web/20070316171839/http://www.php.net/"><span class="caps">PHP</span></a>, that was around the turn of the millennium. [It was a fan site for a <a href="http://web.archive.org/web/20070316171839/http://www.nofx.org/">favourite band</a> of mine.]</p>
<p>After a year or so I realized I didnt have the time to do it properly (ie. securely and cleanly) if I wanted it to be done relatively soon. A slightly customized <a href="http://web.archive.org/web/20070316171839/http://www.mediawiki.org/wiki/MediaWiki">MediaWiki</a> promptly took its place. It did all that I needed quite well, just in a less specific way.</p>
<p>The wiki is serving my site extremely well, but theres still that itch to create my <strong>own</strong> site. I feel if Rails was around back then I may have been able to complete the project in a timely manner. I was also frustrated with <span class="caps">PHP</span>. Part of that is likely due to a lack of experience and of formal programming education at that time, but it was still not fun for me. It wasnt until I started learning Rails that I thought “<em>hey, I could create that site pretty quickly using this!</em></p>
<p>Rails fits my needs like a glove, and this is where it shines. Many professionals are making money creating sites in Rails, so Im not trying to say its for amateurs only or something equally silly.</p>
<h2>Web Frameworks and iPods?</h2>
<p>Some might say I have merely been swept up in hype and am following the herd. You may be right, and thats okay. Im going to tell you a story. There was a guy who didnt get one of the oh-so-shiny iPods for a long time, though they looked neat. His discman plays mp3 CDs, and that was good enough for him. The latest iPod, which plays video, was sufficiently cool enough for him to forget that <strong>everyone</strong> at his school has an iPod and he would be trendy just like them now.</p>
<p>Shocker ending: he is I, and I am him. Now I know why everyone has one of those shiny devices. iPods and web frameworks have little in common except that many believe both the iPod and Rails are all hype and flash. Ive realized that something creating this kind of buzz may actually just be a good product. I feel that this is the only other thing the iPod and Rails have in common: they are both <strong>damn good</strong>. Enough about the iPod, everyone hates hearing about it. My goal is to write about the other thing everyone is tired of hearing about.</p>
<h2>Why is Rails special?</h2>
<p><img src="./Obligatory Post about Ruby on Rails - samhuri.net_files/rails.png" align="right" alt="Ruby on Rails" style="margin: 5px;"> <strong>Rails is not magic.</strong> There are no exclusive JavaScript libraries or <span class="caps">HTML</span> tags. We all have to produce pages that render in the same web browsers. My dad was correct, there <em>is</em> nothing special about my website either. Its more or less a stock Typo website.</p>
<p>So what makes developing with Rails different? For me there are four big things that set Rails apart from the alternatives:</p>
<ol>
<li>Separating data, function, and design</li>
<li>Readability (which is underrated) </li>
<li>Database migrations</li>
<li>Testing is so easy it hurts</li>
</ol>
<h3><span class="caps">MVC 101</span> <em>(or, Separating data, function, and design)</em></h3>
<p>Now Im sure youve heard about separating content from design. Rails takes that one step further from just using <span class="caps">CSS</span> to style your website. It uses whats known as the <span class="caps">MVC</span> paradigm: <strong>Model-View-Controller</strong>. This is a tried and tested development method. Id used <span class="caps">MVC</span> before in Cocoa programming on Mac <span class="caps">OS X</span>, so I was already sold on this point.</p>
<ul>
<li>The model deals with your data. If youre creating an online store you have a product model, a shopping cart model, a customer model, etc. The model takes care of storing this data in the database (persistence), and presenting it to you as an object you can manipulate at runtime.</li>
</ul>
<ul>
<li>The view deals <em>only</em> with presentation. Thats it, honestly. An interface to your app.</li>
</ul>
<ul>
<li>The controller binds the model to the view, so that when the user clicks on the <strong>Add to cart</strong> link the controller is wired to call the <code>add_product</code> method of the cart model and tell it which product to add. Then the controller takes the appropriate action such as redirecting the user to the shopping cart view.</li>
</ul>
<p>Of course this is not exclusive to Rails, but its an integral part of its design.</p>
<h3>Readability</h3>
<p>Rails, and <a href="http://web.archive.org/web/20070316171839/http://www.ruby-lang.org/">Ruby</a>, both read amazingly like spoken English. This code is more or less straight out of Typo. You define relationships between objects like this:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">Article</span> <span class="punct">&lt;</span> <span class="constant">Content</span>
<span class="ident">has_many</span> <span class="symbol">:comments</span><span class="punct">,</span> <span class="symbol">:dependent</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span> <span class="symbol">:order</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">created_at ASC</span><span class="punct">"</span>
<span class="ident">has_many</span> <span class="symbol">:trackbacks</span><span class="punct">,</span> <span class="symbol">:dependent</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span> <span class="symbol">:order</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">created_at ASC</span><span class="punct">"</span>
<span class="ident">has_and_belongs_to_many</span> <span class="symbol">:categories</span><span class="punct">,</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">article_id</span><span class="punct">'</span>
<span class="ident">has_and_belongs_to_many</span> <span class="symbol">:tags</span><span class="punct">,</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">article_id</span><span class="punct">'</span>
<span class="ident">belongs_to</span> <span class="symbol">:user</span>
<span class="punct">...</span></code></pre></div>
<p><code>dependent =&gt; true</code> means <em>if an article is deleted, its comments go with it</em>. Dont worry if you dont understand it all, this is just for you to see some actual Rails code.</p>
<p>In the Comment model you have:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">Comment</span> <span class="punct">&lt;</span> <span class="constant">Content</span>
<span class="ident">belongs_to</span> <span class="symbol">:article</span>
<span class="ident">belongs_to</span> <span class="symbol">:user</span>
<span class="ident">validates_presence_of</span> <span class="symbol">:author</span><span class="punct">,</span> <span class="symbol">:body</span>
<span class="ident">validates_against_spamdb</span> <span class="symbol">:body</span><span class="punct">,</span> <span class="symbol">:url</span><span class="punct">,</span> <span class="symbol">:ip</span>
<span class="ident">validates_age_of</span> <span class="symbol">:article_id</span>
<span class="punct">...</span></code></pre></div>
<p>(I snuck in some validations as well)</p>
<p>But look how it reads! Read it out loud. Id bet that my mom would more or less follow this, and shes anything but a programmer. Thats not to say programming should be easy for grandma, <strong>but code should be easily understood by humans</strong>. Let the computer understand things that are natural for me to type, since were making it understand a common language anyways.</p>
<p>Ruby and Ruby on Rails allow and encourage you to write beautiful code. That is so much more important than you may realize, because it leads to many other virtues. Readability is obvious, and hence maintainability. You must read code to understand and modify it. Oh, and happy programmers will be more productive than frustrated programmers.</p>
<h3 id="migrations">Database Migrations</h3>
<p>Heres one more life-saver: migrations. Migrations are a way to version your database schema from within Rails. So you have a table, call it <code>albums</code>, and you want to add the date the album was released. You could modify the database directly, but thats not fun. Even if you only have one server, all your configuration will be in one central place, the app. And Rails doesnt care if you have PostgreSQL, MySQL, or SQLite behind it. You can develop and test on SQLite and deploy on MySQL and the migrations will just work in both environments.</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">AddDateReleased</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Migration</span>
<span class="keyword">def </span><span class="method">self.up</span>
<span class="ident">add_column</span> <span class="punct">"</span><span class="string">albums</span><span class="punct">",</span> <span class="punct">"</span><span class="string">date_released</span><span class="punct">",</span> <span class="symbol">:datetime</span>
<span class="constant">Albums</span><span class="punct">.</span><span class="ident">update_all</span> <span class="punct">"</span><span class="string">date_released = now()</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">def </span><span class="method">self.down</span>
<span class="ident">remove_column</span> <span class="punct">"</span><span class="string">albums</span><span class="punct">",</span> <span class="punct">"</span><span class="string">date_released</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span></code></pre></div>
<p>Then you run the migration (<code>rake migrate</code> does that) and boom, your up to date. If youre wondering, the <code>self.down</code> method indeed implies that you can take this the other direction as well. Think <code>rake migrate VERSION=X</code>.</p>
<p><em>Along with the other screencasts is one on <a href="http://web.archive.org/web/20070316171839/http://www.rubyonrails.org/screencasts">migrations</a> featuring none other than David Hansson. You should take a look, its the third video.</em></p>
<h3>Testing so easy it hurts</h3>
<p>To start a rails project you type <code>rails project_name</code> and it creates a directory structure with a fresh project in it. This includes a directory appropriately called <em>test</em> which houses unit tests for the project. When you generate models and controllers it creates test stubs for you in that directory. Basically, it makes it so easy to test that youre a fool not to do it. As someone wrote on their site: <em>It means never having to say “<strong>I introduced a new bug while fixing another.</strong></em></p>
<p>Rails builds on the unit testing that comes with Ruby. On a larger scale, that means that Rails is unlikely to flop on you because it is regularly tested using the same method. Ruby is unlikely to flop for the same reason. That makes me look good as a programmer. If you code for a living then its of even more value to you.</p>
<p><em>I dont know why it hurts. Maybe it hurts developers working with other frameworks or languages to see us have it so nice and easy.</em></p>
<h2>Wrapping up</h2>
<p>Rails means I have fun doing web development instead of being frustrated (CSS hacks aside). David Hansson may be right when he said you have to have been soured by Java or <span class="caps">PHP</span> to fully appreciate Rails, but that doesnt mean you wont enjoy it if you <em>do</em> like Java or <span class="caps">PHP</span>.</p>
<p><a href="http://web.archive.org/web/20070316171839/http://www.relevancellc.com/blogs/wp-trackback.php?p=31">Justin Gehtland</a> rewrote a Java app using Rails and the number of lines of code of the Rails version was very close to that of the <span class="caps">XML</span> configuration for the Java version. Java has strengths, libraries available <strong>now</strong> seems to be a big one, but its too big for my needs. If youre like me then maybe youll enjoy Rails as much as I do.</p>
<h2>Youre not done, you lied to me!</h2>
<p>Sort of… there are a few things that it seems standard to include when someone writes about how Rails saved their life and gave them hope again. For completeness sake, I feel compelled to mention some principles common amongst those who develop Rails, and those who develop on Rails. Its entirely likely that theres nothing new for you here unless youre new to Rails or to programming, in which case I encourage you to read on.</p>
<h3><span class="caps">DRY</span></h3>
<p>Rails follows the <span class="caps">DRY</span> principle religiously. That is, <strong>Dont Repeat Yourself</strong>. Like <span class="caps">MVC</span>, I was already sold on this. I had previously encountered it in <a href="http://web.archive.org/web/20070316171839/http://www.pragmaticprogrammer.com/ppbook/index.shtml">The Pragmatic Programmer</a>. Apart from telling <em>some_model</em> it <code>belongs_to :other_model</code> and <em>other_model</em> that it <code>has_many :some_models</code> nothing has jumped out at me which violates this principle. However, I feel that reading a models code and seeing its relationships to other models right there is a Good Thing™.</p>
<h3>Convention over configuration <em>(or, Perceived intelligence)</em></h3>
<p>Rails developers also have the mantra “<em>convention over configuration</em>”, which you can see from the video there. (you did watch it, didnt you? ;) Basically that just means Rails has sane defaults, but is still flexible if you dont like the defaults. You dont have to write even one line of <span class="caps">SQL</span> with Rails, but if you need greater control then you <em>can</em> write your own <span class="caps">SQL</span>. A standard cliché: <em>it makes the simple things easy and the hard possible</em>.</p>
<p>Rails seems to have a level of intelligence which contributes to the wow-factor. After <a href="http://web.archive.org/web/20070316171839/http://sami.samhuri.net/articles/2006/02/20/obligatory-post-about-ruby-on-rails#migrations">these relationships</a> are defined I can now filter certain negative comments like so:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="ident">article</span> <span class="punct">=</span> <span class="constant">Article</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:first</span>
<span class="keyword">for</span> <span class="ident">comment</span> <span class="keyword">in</span> <span class="ident">article</span><span class="punct">.</span><span class="ident">comments</span> <span class="keyword">do</span>
<span class="ident">print</span> <span class="ident">comment</span> <span class="keyword">unless</span> <span class="ident">comment</span><span class="punct">.</span><span class="ident">downcase</span> <span class="punct">==</span> <span class="punct">'</span><span class="string">you suck!</span><span class="punct">'</span>
<span class="keyword">end</span></code></pre></div>
<p>Rails knows to look for the field <strong>article_id</strong> in the <strong>comments</strong> table of the database. This is just a convention. You can call it something else but then you have to tell Rails what you like to call it.</p>
<p>Rails understands pluralization, which is a detail but it makes everything feel more natural. If you have a <strong>Person</strong> model then it will know to look for the table named <strong>people</strong>.</p>
<h3>Code as you learn</h3>
<p>I love how Ive only been coding in Rails for a week or two and I can do so much already. Its natural, concise and takes care of the inane details. I love how I <em>know</em> that I dont even have to explain that migration example. Its plainly clear what it does to the database. It doesnt take long to get the basics down and once you do it goes <strong>fast</strong>.</p>

View file

@ -1,26 +0,0 @@
Title: TextMate Snippets for Rails Assertions
Date: February 20, 2006
Timestamp: 1140508320
Author: sjs
Tags: textmate, rails, coding, rails, snippets, testing, textmate
----
<a href="http://web.archive.org/web/20070316131730/http://sami.samhuri.net/admin/content/edit/18" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>TextMate Snippets for Rails Assertions</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Tue, 21 Feb 2006 07:52:00 GMT">on Monday, February 20, 2006</span></p>
<p>This time Ive got a few snippets for assertions. Using these to type up your tests quickly, and then hitting <strong>⌘R</strong> to run the tests without leaving TextMate, makes testing your Rails app that much more convenient. Just when you thought it was already too easy! (Dont forget that you can use <strong>⌥⌘↓</strong> to move between your code and the corresponding test case.)</p>
<p>This time Im posting the .plist files to make it easier for you to add them to TextMate. All you need to do is copy these to <strong>~/Library/Application Support/TextMate/Bundles/Rails.tmbundle/Snippets</strong>.</p>
<p style="text-align: center;"><a href="http://web.archive.org/web/20070316131730/http://sami.samhuri.net/files/assert_snippets.zip">Assertion Snippets for Rails</a></p>
<p>If anyone would rather I list them all here I can do that as well. Just leave a comment.</p>
<p><em>(I wanted to include a droplet in the zip file that will copy the snippets to the right place, but my 3-hour attempt at writing the AppleScript to do so left me feeling quite bitter. Maybe I was just mistaken in thinking it would be easy to pick up AppleScript.)</em></p>

View file

@ -0,0 +1,34 @@
Title: TextMate: Move selection to self.down
Date: February 21, 2006
Timestamp: 1140510360
Author: sjs
Tags: textmate, rails, hacking, hack, macro, rails, textmate
----
<a href="http://web.archive.org/web/20060321041233/http://sami.samhuri.net/admin/content/edit/19" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>TextMate: Move selection to self.down</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Tue, 21 Feb 2006 08:26:00 GMT">on Tuesday, February 21, 2006</span></p>
<p><strong><span class="caps">UPDATE</span>:</strong> <em>This is obsolete, see <a href="2006.02.21-textmate-insert-text-into-self-down.html">this post</a> for a better solution.</em></p>
<p><a href="2006.02.18-some-textmate-snippets-for-rails-migrations#comment-17">Duanes comment</a> prompted me to think about how to get the <code>drop_table</code> and <code>remove_column</code> lines inserted in the right place. I dont think TextMates snippets are built to do this sort of text manipulation. It would be nicer, but a quick hack will suffice for now.</p><p>Use <acronym title="Migration Create and Drop Table">MCDT</acronym> to insert:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="ident">create_table</span> <span class="punct">"</span><span class="string">table</span><span class="punct">"</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">t</span><span class="punct">|</span>
<span class="keyword">end</span>
<span class="ident">drop_table</span> <span class="punct">"</span><span class="string">table</span><span class="punct">"</span></code></pre></div>
<p>Then press tab once more after typing the table name to select the code <code>drop_table "table"</code>. I created a macro that cuts the selected text, finds <code>def self.down</code> and pastes the line there. Then it searches for the previous occurence of <code>create_table</code> and moves the cursor to the next line, ready for you to add some columns.</p>
<p>I have this bound to <strong>⌃⌥⌘M</strong> because it wasnt in use. If your Control key is to the left the A key its quite comfortable to hit this combo. Copy the following file into <strong>~/Library/Application Support/TextMate/Bundles/Rails.tmbundle/Macros</strong>.</p>
<p style="text-align: center;"><a href="http://sami.samhuri.net/files/move-to-self.down.plist">Move selection to self.down</a></p>
<p>This works for the <acronym title="Migration Add and Remove Column">MARC</acronym> snippet as well. I didnt tell you the whole truth, the macro actually finds the previous occurence of <code>(create_table|add_column)</code>.</p>
<p>The caveat here is that if there is a <code>create_table</code> or <code>add_column</code> between <code>self.down</code> and the table you just added, it will jump back to the wrong spot. Its still faster than doing it all manually, but should be improved. If you use these exclusively, the order they occur in <code>self.down</code> will be opposite of that in <code>self.up</code>. That means either leaving things backwards or doing the re-ordering manually. =/</p>

View file

@ -1,37 +0,0 @@
Title: TextMate: Move selection to self.down
Date: February 21, 2006
Timestamp: 1140510360
Author: sjs
Tags: textmate, rails, hacking, hack, macro, rails, textmate
----
<a href="http://web.archive.org/web/20060321041233/http://sami.samhuri.net/admin/content/edit/19" class="admintools" id="admin_article" style="display: none;">edit</a>
<h2>TextMate: Move selection to self.down</h2>
<p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
<span class="typo_date" title="Tue, 21 Feb 2006 08:26:00 GMT">on Tuesday, February 21, 2006</span></p>
<p><strong><span class="caps">UPDATE</span>:</strong> <em>This is obsolete, see <a href="http://web.archive.org/web/20060321041233/http://sami.samhuri.net/articles/2006/02/21/textmate-insert-text-into-self-down">this post</a> for a better solution.</em></p>
<p><a href="http://web.archive.org/web/20060321041233/http://sami.samhuri.net/articles/2006/02/18/textmate-snippets-for-rails-migrations#comment-17">Duanes comment</a> prompted me to think about how to get the <code>drop_table</code> and <code>remove_column</code> lines inserted in the right place. I dont think TextMates snippets are built to do this sort of text manipulation. It would be nicer, but a quick hack will suffice for now.</p><p>Use <acronym title="Migration Create and Drop Table">MCDT</acronym> to insert:</p>
<div class="typocode"><pre><code class="typocode_ruby "><span class="ident">create_table</span> <span class="punct">"</span><span class="string">table</span><span class="punct">"</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">t</span><span class="punct">|</span>
<span class="keyword">end</span>
<span class="ident">drop_table</span> <span class="punct">"</span><span class="string">table</span><span class="punct">"</span></code></pre></div>
<p>Then press tab once more after typing the table name to select the code <code>drop_table "table"</code>. I created a macro that cuts the selected text, finds <code>def self.down</code> and pastes the line there. Then it searches for the previous occurence of <code>create_table</code> and moves the cursor to the next line, ready for you to add some columns.</p>
<p>I have this bound to <strong>⌃⌥⌘M</strong> because it wasnt in use. If your Control key is to the left the A key its quite comfortable to hit this combo. Copy the following file into <strong>~/Library/Application Support/TextMate/Bundles/Rails.tmbundle/Macros</strong>.</p>
<p style="text-align: center;"><a href="http://web.archive.org/web/20060321041233/http://sami.samhuri.net/files/move-to-self.down.plist">Move selection to self.down</a></p>
<p>This works for the <acronym title="Migration Add and Remove Column">MARC</acronym> snippet as well. I didnt tell you the whole truth, the macro actually finds the previous occurence of <code>(create_table|add_column)</code>.</p>
<p>The caveat here is that if there is a <code>create_table</code> or <code>add_column</code> between <code>self.down</code> and the table you just added, it will jump back to the wrong spot. Its still faster than doing it all manually, but should be improved. If you use these exclusively, the order they occur in <code>self.down</code> will be opposite of that in <code>self.up</code>. That means either leaving things backwards or doing the re-ordering manually. =/</p>