Martinicity: Tag rubyMike Blaketag:www.martinicity.net,2005:TypoTypo2007-05-22T21:45:02+00:00Mike Blakeurn:uuid:5cbbdb9a-35d6-4c26-aed0-fdf039d899422007-05-22T21:16:00+00:002007-05-22T21:45:02+00:00The Philanthropic Programmers<p>Something fantastic is happening in the <a href="http://www.rubyonrails.org/">Ruby and Rails</a> World.</p>
<p></p>
<ol>
<li>Do unto others</li>
</ol>
<p>Open source developers know from experience that what goes around comes around. Contributions to projects like Rails invariably come back to benefit everyone, including the contributor. The community is now thinking on a larger scale. Frameworks come and go, but <a href="http://pragdave.pragprog.com/pragdave/2007/05/rails_is_love.html">love</a> is timeless .</p>
<ol>
<li>The Summer of Love
<p>is focusing his power on <a href="http://hacketyhack.net/">Hackety Hack</a> an amazing application that teaches children programming. And it’s likely more zeitgeist than coincidence that a professor from <span class="caps">MIT</span>, where the generous open source license originated, has started the noble project called <a href="http://laptop.org">One Laptop per Child</a> . Developers everywhere like <a href="http://slash7.com/articles/2006/4/17/teaching-rails-for-charity-in-d-c">Amy Hoy</a> are giving back in order to share the passion.</p></li>
</ol>
<p></p>
<p>Chad Fowler has challenged the entire community to harness our newfound passion to do <a href="http://www.chadfowler.com/2007/5/19/changing-the-world">great things</a>. And the community has only just begun to respond. During RailsConf over <a href="http://www.clarkware.com/cgi/blosxom/2007/05/22#RailsConf07">$33,000</a> was raised for <a href="http://pragmaticstudio.com/donate/">charity</a>.</p>
<h3><a href="http://www.lse.co.uk/ShowbizNews.asp?Code=WV31893G&headline=paris_hilton_shortlisted_for_nun_role">Witness the Transformation</a></h3>
<p>Software evolves rapidly. Humanity seems to evolve at a slower pace, but if you watch carefully, with ruby colored glasses , you can see it happening.</p>Mike Blakeurn:uuid:4211efe2-2393-422e-af13-478696806a852007-04-26T11:30:00+00:002009-01-10T05:27:53+00:00Hibernate 3.x Migration Guide<strong>Update 4/30</strong>
<blockquote>
<em>I finally did find the real <a href="http://www.hibernate.org/250.html">Hibernate 3 Migration Guide</a>. My fun with Hibernate is over though, I’ve found an easier persistence solution with the clients homegrown method. I believe every single java project I’ve worked on in the past 10 years has used a different and unique Persistence mechanism. My favorite was <a href="http://www.eclipsecon.org/2007/index.php?page=presenters/#Francois_Granade">Francois’</a> PersistentEntity at <a href="http://finance.groups.yahoo.com/group/viagone/">ViaFone</a>.</em>
</blockquote>
<hr />
I’ve been strugling for the last day trying to get a Java application working, mainly, trying to get Hibernate to see a DataSource. Now I’m at a point where I get this error message:
<blockquote>
<p>[junit] (cfg.Configuration 1312) configuring from resource: /hibernate.cfg.xml</p>
</blockquote>
<blockquote>
<p>[junit] (cfg.Configuration 1289) Configuration resource: /hibernate.cfg.xml</p>
</blockquote>
<blockquote>
<p>[junit] (util.DTDEntityResolver 30 ) Don’t use old DTDs, read the <b>Hibernate 3.x Migration Guide!</b></p>
</blockquote>
<p>A quick search does not turn up a Hibernate Migration Guide, so I decided to create this one:</p>
<h3>The Hibernate 3.x Migration Guide</h3>
<p><b>1. Get <a href="http://www.ruby-lang.org/en/">Ruby</a></b></p>
<p><b>2. Install <a href="http://www.rubyonrails.org/down">Rails</a></b></p>
<p><b>3. Rebuild your application.</b></p>
<p><b>4. Have fun again!</b></p>Mike Blakeurn:uuid:ae1917e2-4a47-4fa2-be71-d57109445b462007-03-31T13:11:00+00:002007-05-04T08:12:41+00:00Testing a Non-Rails Application Using Rails<p>Rails developers working on enterprise software projects are suprised to discover the lack of automated tests in many mature web applications. As frustrating as this can be, A lack of automated tests is also a tremendous opportunity to </p>
<pre><code>1. <b>Learn your non-rails applications underlying database structure.</b>
2. <b>Demonstrate to a devlopment team the power of The Rails Framework.</b>
2. <b>Encourage automated testing.</b></code></pre>
<p>Rails may be the quickest path to automate some basic test of your non-rails applications data model. These steps will get you all set up to write your automated tests in Ruby.</p>
<p>1. <a href="#connect">Connect to Your Enterprise Database</a>
2. <a href="#conventions">Set DB Conventions</a>
3. <a href="#safety">Safety Net</a>
4. <a href="#schema">Duplicate the Schema</a>
4. <a href="#extract">Extract Development Data</a></p>
<h4 id='connect'>I. Connect to Your Enterprise Database from Rails.</h4>
<p>
<p>Download and install any os driver, ruby gems, and rails adapters needed to connect to your database:</p>
<blockquote>
<p><a href="http://wiki.rubyonrails.com/rails/pages/MySQL">MySQL</a>
<br><a href="http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0606dumbill/?ca=dgr-lnxw01DB24RubyonRails"><span class="caps">DB2</span></a>
<br><a href="http://www.oracle.com/technology/pub/articles/saternos-ror-faq.html">Oracle</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/HowToSetupSybaseAdapterOnRails">Sybase</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/HowtoConnectToMicrosoftSQLServer">SQLServer</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/PostgreSQL">PostgreSQL</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/Firebird">Firebird</a></p>
</blockquote>
<h4 id='conventions'>II. Set Your Existing Database Conventions </h4>
<p>
<p>, Recipie #16 can walk you through this very quickly.</p>
Identify any conventions used by your legacy database. Application wide conventions can be set in the <code>config/environment.rb</code> file.
They are specified by calling the appropriate class methods available on <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">ActiveRecord::Base</a>. The methods you need to call depend on how your database is configured:
<pre>
ActiveRecord::Base.table_name_prefix 'myapp_'
ActiveRecord::Base.table_name_suffix = '_def'
ActiveRecord::Base.sequence_name = 'dev_company'
ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore # or :table_name
</pre>
<h4 id='safety'><span class="caps">III</span>. Safety Net</h4>
<p>
<p>Load the <a href="http://agilewebdevelopment.com/plugins/safety_net">SafetyNet Plugin</a> into your rails app to prevent from destroying your development database.</p>
<h4 id='schema'>IV. Duplicate Your Database Schema</h4>
<p>
<p>OK , with your dev and test development databases set correctly, let’s try duplicting the schema.</p>
<code>rake db:test:clone</code>
If this works right off the bat, you’re one of the lucky ones. Skip to <a href="#extract">Extracting Development Data</a>
<p>
<p>If you recieved errors from the clone command, do steps A through C .</p>
<p>A. You can now correct any errors you may have had by manually modifying schema.rb . See <a href="/articles/2007/03/29/rails-on-oracle">Oracle Errors</a> for some problems I had with the Oracle Database. Since schema.rb is generated, it’s a good idea to rename it when you modify it manually.</p>
B. The clone command above may have started loading data; you need may to purge the test db.
<code>
rake db:test:purge
</code>
<p>C. Tell rake to load your new schema file by passing the new name, relative to <span class="caps">RAILS</span>_ROOT in the <span class="caps">SCHEMA</span> environment variable:</p>
<code>
rake db:schema:load RAILS_ENV=test SCHEMA=db/oracle\_schema.rb
</code>
<h4 id='extract'>V. Extracting Development Data</h4>
<p>
<p>Copy the code from
Rails Recipe #42: <a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake">extract_fixtures.rake</a> to your <span class="caps">RAILS</span>\_ROOT/lib/tasks directory.</p>
<blockquote>
<p><b><span class="caps">TIP</span>: If your develoment database has lots of data, Modify the <span class="caps">SQL</span> Select in this code to limit the amount of data you copy using the <span class="caps">SQL</span> limit statement:</b></p>
</blockquote>
<blockquote>
<code>sql = "SELECT * FROM %s limit = 100"</code>
</blockquote>
<blockquote>
<p>Or for a proprietary database, the equivalent command:</p>
</blockquote>
<blockquote>
<code>sql = "SELECT * FROM %s WHERE ROWNUM<=100"</code>
</blockquote>
Then run
<code>
rake extract_fixtures
</code>
<p>
<ol>
<li>You’ve made your testbed so you can lie in it.</li>
</ol>
<p>You now have and exact copy of your development database, and a collection of sample data in Yaml format, and are ready to begin writing some Unit Tests. Stay tuned for some examples. Rail on!
<span>
<script type="text/javascript">
digg_url = ‘http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails’;
digg_title = ‘Testing a Non-Rails Application Using Rails’;</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</span></p>Mike Blakeurn:uuid:d3a23de4-6d31-4890-aaf1-7e6e04e61f322007-02-14T21:07:00+00:002007-02-14T21:09:18+00:00The Unselfish Act of Writing Tests<p>
####ORUG
At this months <a href="http://www.orug.org">Orlando Ruby User’s Group</a> meeting, I did my <a href="http://www.apptrain.net/testing/img0.html">Testing on Rails</a> presentation. In it I touched on just some of the benefits of having an automated suite of Unit, Functional, and Integration tests.
<p>
<p>!<a href="http://www.apptrain.net/testing/img4.jpg">Benefits of Testing</a></p>
<p>
Later I thought of another major benefit. Tests help other people on the project, in addition to the author of the test.
<ol>
<li>Good Habits are Contagious</li>
</ol>
<p>Recently I had the opportunity to work on a project with <a href="http://blog.harrisreynolds.net/">Harris Reynolds</a>. Harris did something very important that sadly, you don’t see on every Rails Project. He wrote a unit test. Lot’s of us wrote tests , but this particular test stood out for two reasons. The first was when it was written.</p>
<ol>
<li>Writing Tests First</li>
</ol>
<p>Harris had found a bug in a model object that I had created. To notify me, he checked in an assertion that failed, then emailed me. This saved me time searching thought and remembering code I had written. I simply typed <code>rake test:recent</code> which runs all recently changed tests in a Rails application . The error took me right to the problem, and I immediately fixed the bug.</p>
<ol>
<li>Testing a Deployment</li>
</ol>
<p>The second reason I remember this test happened at least a month later. The application was being deployed to a new machine, and the test failed. The particular model object being tested pointed to a different database that the rest of the application, and it turned out that that database hadn’t been installed correctly on the new machine. Harris’s test told the problem straight away, and saved time once again.</p>
<p><b>Unit Testing: the gift that keeps on giving.</b></p>
<p>Thanks Harris!</p>Mike Blakeurn:uuid:a9b2f624-31cf-4d7a-b807-636c42cdc9c02006-09-21T17:25:00+00:002006-10-14T11:59:26+00:00New Rails Site: FanNation<p>The folks at <a href="http://www.fannation.com">FanNation</a> have done something that no one else in the sports world has done.
<span style="float:right;">
<map id ="favorite_team_map" name="favorite_team_map"><area shape ="rect" coords ="0,0,160,80" href="http://www.fannation.com" target ="_blank" alt="FanNation: The Republic of Sport" title="FanNation: The Republic of Sport" /><area shape ="rect" coords ="10,82,150,102" href ="http://www.fannation.com/users/view_user_profile/530" target ="_blank" alt="mblake" title="mblake" /><area shape ="rect" coords ="10,102,150,260" href ="http://www.fannation.com/player_trackers/team_overview/530" target ="_blank" alt="mblake's Favorite Teams" title="mblake's Favorite Teams" /><area shape ="rect" coords ="0,265,160,300" href="http://www.fannation.com/remote/favorite_teams" target ="_blank" alt="Get your own Favorite Teams Widget!" title="Get your own Favorite Teams Widget!" /></map><image src="http://www.fannation.com/remote/favorite_teams_image/530" alt="mblake's Favorites Teams" usemap="#favorite_team_map" border="0" />
</span>
They’ve created a <a href="http://www.fannation.com">comprehensive site</a> for sports fans to gather and share information about their favorite teams and players (fantasy too) , and it’s all free.</p>
<p>I spent some time with the <a href="http://www.fannation.com/fannation/about">developers at FanNation</a> working on the map on the home page. They are a sharp bunch, and I’m constantly amazed at the features they are adding each time I visit. Fannation is now one of the biggest websites around developed on <a href="http://www.rubyonrails.org/">Ruby on Rails</a>. Tag clouds, in place edits, Dynamic news updates using <span class="caps">AJAX</span> calls, personalized blogging. This site is a tremendous example of practical usage of some of the latest technologies. And it’s loads of fun.</p>
<p>There’s tons of other functionality I haven’t explored yet, but definitely keep an eye on FanNation!</p>Mike Blakeurn:uuid:cc7c8d73-4271-44eb-860e-545367b0a9d62006-09-12T14:30:00+00:002006-12-12T19:59:49+00:00Attend a Hackfest!<p>If you’re a programmer like me you’re a little intimidated by the <a href="http://www.flickr.com/photos/45241414@N00/sets/72157594276834801/">Hackfest</a>. That is , you’ve had several programming gigs over the years, attended a few more day <a href="http://www.pluckthepetal.com/blackdaisies/2004/02/long_meetings/">long meetings</a> than you’d have liked, and fear that you’ll be embarrassed by your perceived lack of skill compared to the young <a href="http://www.rubyonrails.org/core">whiz kids</a> with a burning passion for creating software.</p>
<p>We’ll let me assure you, the spirit of the hackfest is one of <a href="http://redhanded.hobix.com/cult/everyoneIsHereInTheFuture2.html">learning and cooperation</a> and not at all that of oneupmanship or competition. At least not at the <a href="http://www.orug.org"><span class="caps">ORUG</span></a> .</p>
<p>I had a blast this past weekend and learned a lot from my piers, and left with the feeling that I had helped out as well.</p>
<p>So my advice, fear not and attend a Hackfest!</p>Mike Blakeurn:uuid:1adb0c92-2859-42ba-822a-1ef44bf0ad032006-07-15T10:30:00+00:002008-04-05T01:44:03+00:00Ruby Namespace Conflicts<p><b>Declare new Classes Inside Modules to Avoid Conflicts</b></p>
<p>In the ruby language you cannot have a Module with the same name as a Class. The reason is that in any instance of the ruby interpreter, all root level accessible Objects are stored in a Hash of constants as either Modules or Classes. This can get cluttered pretty quickly in a large application. Try throwing the line <code>'p Module.constants'</code> in the middle of a medium sized Rails application.</p>
<p>However, if a class is part of a Module, which acts as a namespace in ruby, it’s name never makes it to Module.constants. So it can have the same name as an existing Module.</p>
To see a demonstration of this, I executed the following ruby code.
<div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">module </span><span class="module">One</span>
<span class="keyword">end</span>
<span class="keyword">class </span><span class="class">Two</span>
<span class="keyword">def </span><span class="method">self.to_s</span>
<span class="punct">"</span><span class="string">2</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="ident">p</span> <span class="constant">Two</span>
<span class="keyword">class </span><span class="class">One::Two</span>
<span class="keyword">def </span><span class="method">self.to_s</span>
<span class="punct">"</span><span class="string">1::2</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="ident">p</span> <span class="constant">One</span><span class="punct">::</span><span class="constant">Two</span>
<span class="keyword">class </span><span class="class">One::Three</span>
<span class="keyword">def </span><span class="method">self.to_s</span>
<span class="punct">"</span><span class="string">1::3</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="ident">p</span> <span class="constant">One</span><span class="punct">::</span><span class="constant">Three</span>
<span class="ident">p</span> <span class="constant">Module</span><span class="punct">.</span><span class="ident">constants</span>
<span class="keyword">class </span><span class="class">One::One</span>
<span class="keyword">def </span><span class="method">self.to_s</span>
<span class="punct">"</span><span class="string">1::1</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="ident">p</span> <span class="constant">One</span><span class="punct">::</span><span class="constant">One</span>
<span class="keyword">class </span><span class="class">One</span>
<span class="keyword">def </span><span class="method">self.to_s</span>
<span class="punct">"</span><span class="string">1</span><span class="punct">"</span>
<span class="keyword">end</span>
<span class="keyword">end</span>
<span class="ident">p</span> <span class="constant">One</span>
</code></pre></div>
<p>Here’s what happened. (output below). A Module named <strong>One</strong> was created and so was a class named <strong>Two</strong>. And the interpreter saw that it was good. Then, in the <strong>One</strong> module, classes <strong>One</strong>, <strong>Two</strong>, and <strong>Three</strong> were created. And the interpreter saw that it was good.</p>
<p>Then I took a peek at <strong>Module.constants</strong>. The <strong>One</strong> Module, and the <strong>Two</strong> Class were saved as constants. But the <strong>Three</strong> Class was not stored as a constant because it is not at the root level. Since Classes created within Modules are not stored as constants,I was allowed to create a <strong>One</strong> Class inside the <strong>One</strong> Module without a getting a TypeError.</p>
<p>But all is not well in paradise. When the program tried to create a class named One it got the error</p>
<font color='red'>-:35: One is not a class (TypeError)</font>
<p>That’s because it’s already stored in constants as a Module.</p>
<p>As Ruby grows in popularity, this could become a problem. Especially in the Rails framework where all Model objects are at the root level. Considering this, it’s a good habit to <b>use modules as namespaces when creating new classes</b> in things like plugins and gems to avoid a TypeError.</p>
<p><br></p>
<p><b>Output:</b></p>
<p>
<p><br></p>
<p>1::2</p>
<p>1::3</p>
<p>2</p>
<code>["TrueClass", "FloatDomainError", "Fixnum", "TOPLEVEL\_BINDING", "SignalException", "String", "SystemCallError", "UnboundMethod", "Buffering", "Rational", "ThreadGroup", "CROSS\_COMPILING", "ScriptError", "MatchData", "Thread", "IndexError", "STDOUT", <font color='red'>"One"</font>, "SecurityError", "Integer", "SingleForwardable", "Config", "RELEASE\_DATE", "Exception", "NoMethodError", "Proc", "GC", "TypeError", "Binding", "Signal", "FALSE", "RUBY\_PLATFORM", "Forwardable", "Bignum", "SystemExit", "Date", "NotImplementedError", "EOFError", "FileTest", "TRUE", "Numeric", "Interrupt", "ARGF", "Array", "SyntaxError", "MatchingData", "RUBY_VERSION", "ParseDate", "Time", "RangeError", "ENV", "NIL", "Enumerable", "Module", "PLATFORM", "STDERR",<font color='red'>"Two"</font>, "NoMemoryError", "Float", "Regexp", "DateTime", "Data", "ZeroDivisionError", "ARGV", "Dir", "Process", "Range", "ThreadError", "ArgumentError", "Object", "IO", "Comparable", "LocalJumpError", "Math", "Marshal", "RuntimeError", "STDIN", "Method", "VERSION", "RegexpError", "Hash", "Precision", "Kernel", "Continuation", "File", "SystemStackError", "OpenSSL", "FalseClass", "Errno", "NilClass", "StandardError", "LoadError", "ObjectSpace", "RUBY\_RELEASE\_DATE", "Gem", "IOError", "Symbol", "Struct", "NameError", "Class"]
1::1
<font color='red'>-:35: One is not a class (TypeError)</font>Mike Blakeurn:uuid:9a854828-1ccd-404d-80bb-29f41e7d67502006-06-23T12:16:00+00:002006-06-23T12:19:21+00:00Rails Conference 2006 - Day 1<p>I’m registered now at Rails Conference. The first day was awesome, though I got in late. I forgot what a great speaker <a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi">Dave Thomas</a> is.</p>
<p>Someone during the install fest commented that they didn’t like the way layouts are in a separate directory under views in a default Rails application. Dave said that he questions little things like that in the framework. But made the following suggestion. <b>Just go with the flow.</b> Because if you don’t use the defaults, one day 2 years from now, another Rails developer will open up that app, and say “Where the hell are the layouts?”</p>
<p>Dave’s response really captured what’s going on here. The Rails team has created an awesome framework that has so simplified development of a web application so that we can all focus on <a href="http://www.martinicity.net/pics/germany/m1.html">more important things</a>. It’s like when you first start out on a development team, everyone agrees on certain standards that simplify comunication. It doesn’t really matter exactly what these are, everyone is now on the same page, and you function better as a team. </p>
<pre><code>by releasing the framework as open source, have in effect included us all on one big team. <a href="http://clarkware.com/cgi/blosxom">Mike Clark</a> commented what a great community this is and it's true. At install fest, people were all just helping each other out, regardless of experience or noteriety. It's going to be a great conference.</code></pre>
<p>And if you haven’t stared learning Ruby or Rails, it’s time. I’ve noticed a huge spike in the number of ruby and rails jobs on dice and monster in the last few weeks. And rails projects are keeping my own dance card full.</p>
<p>So come on, <a href="http://www.rubyonrails.org/">go with the flow!</a></p>