 


<!-- generator="wordpress/2.0.7" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>New Auburn Personal Computer Services LLC</title>
	<link>http://www.napcsweb.com/blog</link>
	<description>professional web development and consulting</description>
	<pubDate>Wed, 02 Jul 2008 13:58:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.7</generator>
	<language>en</language>
			<item>
		<title>RSpec helped me refactor my code.</title>
		<link>http://www.napcsweb.com/blog/2008/06/10/rspec-helped-me-refactor-my-code/</link>
		<comments>http://www.napcsweb.com/blog/2008/06/10/rspec-helped-me-refactor-my-code/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 17:12:52 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>Rails</category>

		<category>Howto</category>

		<category>Testing</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/06/10/rspec-helped-me-refactor-my-code/</guid>
		<description><![CDATA[I&#8217;ve been extremely against using RSpec. I always found it rather clunky, but it turns out that resources to really help a person learn how RSpec works are dificult to find.  The examples you find out on the web are just poorly written or just contrived and impractical, or they&#8217;re so hopelessly overengineered that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been extremely against using RSpec. I always found it rather clunky, but it turns out that resources to really help a person learn how RSpec works are dificult to find.  The examples you find out on the web are just poorly written or just contrived and impractical, or they&#8217;re so hopelessly overengineered that a newcomer would be overwhelemed. </p>
<p> This weekend I took it upon myself to really learn RSpec and so I started rewriting some of the tests for <a target="_blank" href="http://www.feelmyskills.com/">FeelMySkills</a>.  I started with the Account model which is used all over the app. The account_id is stored in the session and I use the Restful_authentication plugin to get access to a <b>current_account</b> method which returns the Account object.  I want to be able to determine whether or not that account is an Admin, and I have an entry in the roles called &#8220;admin&#8221;.  Nothing too special about all this, as many apps use a similar bit of functionality.</p>
<p>To make this easy on myself, I wrote a method called  is_admin? which returns true if the admin role is associated with my account and nil if it&#8217;s not, and as we all know, nil evaluates to false, and anything other than nil or false evaluates to true.  </p>
<p>When an account is created, I give them a role called &#8220;user&#8221;. Eventually, Pro users will have a different role, giving them access to more stuff in the system.  Here&#8217;s what i have so far:</p>
<pre>
&nbsp;&nbsp;class Account &lt; ActiveRecord::Base
&nbsp;&nbsp;&nbsp;&nbsp;has_and_belongs_to_many :roles
&nbsp;&nbsp;&nbsp;&nbsp;after_create :add_user_role
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;def is_admin?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.roles.detect{|r| r.name == &quot;admin&quot;}
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;def is_user?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.roles.detect{|r| r.name == &quot;user&quot;}
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;def add_user_role
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.roles &lt;&lt; Role.user
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;
&nbsp;&nbsp;end
</pre>
<p>So, when I create a new user, I need to make sure that user gets the user role. My original Test/Unit test looked like this:</p>
<pre>
&nbsp;&nbsp;def test_new_account_should_have_user_role
&nbsp;&nbsp;&nbsp;&nbsp;account = Account.create(:login =&gt; &quot;test&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :password=&gt;&quot;test&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :password_confirmation =&gt; &quot;test&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :email =&gt; &quot;test@test.com&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;assert account.is_user?
&nbsp;&nbsp;end
</pre>
<p>This test passes without any issues, so I know the code is right.  Here&#8217;s what I tried with RSpec:</p>
<pre>
&nbsp;&nbsp;describe &quot;when creating an account&quot; do
&nbsp;&nbsp;&nbsp;&nbsp;fixtures :accounts, :roles
&nbsp;&nbsp;&nbsp;&nbsp;before(:each) do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@account = Account.create(:login =&gt; &quot;test&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :password=&gt;&quot;test&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :password_confirmation =&gt; &quot;test&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :email =&gt; &quot;test@test.com&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;it &quot;should have the user role&quot; do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@account.is_user?.should be_true
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;
&nbsp;&nbsp;end
</pre>
<p>Imagine my surprise when I ran this spec and it failed!  The reason why makes perfect sense when you start thinking about it.</p>
<p>First, RSpec&#8217;s matcher  be_true evaluates the response of the method to be equal to true.  The code for <b>is_admin?</b> actually returns an instance of <b>Role</b>, and not <b>true</b> like I asserted earlier.  While that method evaluates to true, it does not <b>equal</b> true.  So it&#8217;s interesting that the <b>assert</b> method has no probelm making the evaluation, but RSpec&#8217;s matchers are pickier.</p>
<p>A fair argument here would be &#8220;why does your is_admin? method return a Role and not just true or false?&#8221;  The answer is that I&#8217;m lazy. I rely on Ruby to work for me, and until now, #detect has been a great ally.  In my controller code, I can do </p>
<pre>
if current_user.is_admin&#46;..
</pre>
<p>and all is well, without the need to explicitly return true or false from the is_admin? or is_user? methods.</p>
<h3>A better way</h3>
<p>Looking at the spec again, I notice that I am in fact asking for the role in that specification.  So I rewrite it to grab the User role from the fixtures and ensure they&#8217;re equal and it passes.</p>
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;it &quot;should have the user role&quot; do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@account.is_user?.should equal roles(:user)
&nbsp;&nbsp;&nbsp;&nbsp;end
</pre>
<p>But something about that bothers me.  What am I really testing?  I&#8217;m testing to make sure that the account is a regular user.  Maybe I really do need a method that returns true or false.</p>
<p>It turns out that if you have a method in your model that ends with a question mark (?) and returns true or false, then RSpec can dynamically create a matcher for it.  I rewrote the spec like this:</p>
<pre>
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;it &quot;should be a user&quot; do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@account.should be_a_user
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;
</pre>
<p>and then added this method to my model:</p>
<pre>
&nbsp;&nbsp; # calls is_user? and returns true if is_user? returns a result, 
&nbsp;&nbsp; # or false if it returns nil
&nbsp;&nbsp; def user?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.is_user? != nil
&nbsp;&nbsp; end
</pre>
<p>and I ended up with something I am much more comfortable with. I think future refactorings might change this around even more, but I found this exploration to be extremely enlightening.  </p>
<p>P.S. For those that are interested, I actually have several roles in my system and I don&#8217;t manually declare these methods like is_admin? and is_user? by hand. I use this instead:</p>
<pre>
class Account &lt; ActiveRecord::Base
&nbsp;
&nbsp;&nbsp;# &#46;..
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;# constant containing all of the role names
&nbsp;&nbsp;&nbsp;&nbsp;# in the system
&nbsp;&nbsp;&nbsp;&nbsp;Role::ROLE_NAMES.each do |r| 
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;class_eval &lt;&lt;-CODE
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def is_#{r}?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.roles.detect{|role| role.name == &quot;#{r}&quot;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{r}?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.is_#{r}? != nil
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;CODE
&nbsp;&nbsp;end
&nbsp;
&nbsp;&nbsp;#&#46;.. 
&nbsp;
end
</pre>
<p>That way I don&#8217;t need to add new methods when I implement factchecker or pro or business roles later. Just thought I&#8217;d share that.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/06/10/rspec-helped-me-refactor-my-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Slides and Materials from &#8220;Web Design for Programmers&#8221;</title>
		<link>http://www.napcsweb.com/blog/2008/06/04/slides-and-materials-from-web-design-for-programmers/</link>
		<comments>http://www.napcsweb.com/blog/2008/06/04/slides-and-materials-from-web-design-for-programmers/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 04:13:38 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>News</category>

		<category>web</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/06/04/slides-and-materials-from-web-design-for-programmers/</guid>
		<description><![CDATA[The slides from my RailsConf 2008 tutorial session are now available. Grab the PDF version.
Unfortunately, the handouts I sent were printed in black and white so some of the color examples don&#8217;t work as well.  Grab color ones instead.
If there are additional materials from the presentation that you want to see, let me know [...]]]></description>
			<content:encoded><![CDATA[<p>The slides from my RailsConf 2008 tutorial session are now available. <a href="http://www.napcsweb.com/files/presentations/railsconf2008/slides.pdf">Grab the PDF version.</a></p>
<p>Unfortunately, the handouts I sent were printed in black and white so some of the color examples don&#8217;t work as well.  <a href="http://www.napcsweb.com/files/presentations/railsconf2008/handouts.pdf">Grab color ones</a> instead.</p>
<p>If there are additional materials from the presentation that you want to see, let me know and I&#8217;ll see what I can do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/06/04/slides-and-materials-from-web-design-for-programmers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Create a new Edge Rails project</title>
		<link>http://www.napcsweb.com/blog/2008/04/24/create-a-new-edge-rails-project/</link>
		<comments>http://www.napcsweb.com/blog/2008/04/24/create-a-new-edge-rails-project/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 14:16:40 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>News</category>

		<category>Rails</category>

		<category>snacks</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/04/24/create-a-new-edge-rails-project/</guid>
		<description><![CDATA[In a previous post, I provided scripts that made the creation of a new Edge Rails project easy. Since then, Rails has moved from Subversion to Git, which means that the scripts I provided no longer work as expected.  Fortunately, very little has changed and I was able to make the script a little [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.napcsweb.com/blog/2007/09/20/creating-a-new-edge-rails-project-in-windows-and-nix-osx-too/">previous post</a>, I provided scripts that made the creation of a new Edge Rails project easy. Since then, Rails has moved from Subversion to Git, which means that the scripts I provided no longer work as expected.  Fortunately, very little has changed and I was able to make the script a little bit better so that it acts like the original <b>rails</b> command.</p>
<h2>Prerequisites</h2>
<p>First, you&#8217;re going to need git.  I could have written the script to grab the latest version via a zipfile, but I wanted something that was fast and worked on all platforms. Windows can unzip files, but then I&#8217;d have to make Windows users go grab commandline tools to unzip files.</p>
<h2>Installing Git</h2>
<p>Mac users with XCode and Macports installed can do it with </p>
<pre>sudo port install git-core +svn</pre>
<p>Windows users can install <a href="http://code.google.com/p/msysgit/downloads/list">Msysgit</a>.</p>
<p>Linux users should install Git using their package manager or from source.<br />
Here&#8217;s the script. Instructions for running it are after the code.</p>
<pre>
#!/bin/ruby
git_repo = &quot;git://github.com/rails/rails.git&quot;

help = %Q{
Rails Info:
&nbsp;&nbsp;&nbsp;&nbsp;-v, &#45;-version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Show the Rails version number and quit.
&nbsp;&nbsp;&nbsp;&nbsp;-h, &#45;-help&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Show this help message and quit.
&nbsp;
General Options:
&nbsp;&nbsp;&nbsp;&nbsp;-p, &#45;-pretend&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Run but do not make any changes.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#45;-force&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Overwrite files that already exist.
&nbsp;&nbsp;&nbsp;&nbsp;-s, &#45;-skip&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Skip files that already exist.
&nbsp;&nbsp;&nbsp;&nbsp;-q, &#45;-quiet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Suppress normal output.
&nbsp;&nbsp;&nbsp;&nbsp;-t, &#45;-backtrace&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debugging: show backtrace on errors.
&nbsp;
Description:
&nbsp;&nbsp;&nbsp;&nbsp;The &#039;edge_rails&#039; command creates a new Rails application with a default
&nbsp;&nbsp;&nbsp;&nbsp;directory structure and configuration at the path you specify, using the
&nbsp;&nbsp;&nbsp;&nbsp;very latest version of Rails.
&nbsp;
Example:
&nbsp;&nbsp;&nbsp;&nbsp;edge_rails ~/Code/Ruby/weblog
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
&nbsp;&nbsp;&nbsp;&nbsp;See the README in the newly created application to get going.&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;
}

require &#039;fileutils&#039;
&nbsp;
if ARGV.empty?
&nbsp;&nbsp;puts help
&nbsp;&nbsp;exit
end
&nbsp;
dir = ARGV.shift
args = ARGV.join (&quot; &quot;)
&nbsp;
FileUtils::mkdir(dir)
FileUtils::mkdir(&quot;#{dir}/vendor&quot;)

puts &quot;Exporting EdgeRails from #{git_repo}&quot;
# system &quot;svn export http://svn.rubyonrails.org/rails/trunk #{dir}/vendor/rails&quot;
system &quot;git clone &#45;-depth=1 #{git_repo} #{dir}/vendor/rails&quot;
system &quot;rm -rf #{dir}/vendor/rails/.git*&quot;
&nbsp;
system &quot;ruby #{dir}/vendor/rails/railties/bin/rails #{dir} #{args}&quot;
</pre>
<h2>How this works for Mac and Linux users</h2>
<p>Save the script to your home folder as <b>edge_rails</b>, and set the execute bit:</p>
<pre>
&nbsp;&nbsp;chmod 644 ~/edge_rails
</pre>
<p>Run it with</p>
<pre>
&nbsp;&nbsp;~/edge_rails your_app
</pre>
<p>You could symlink it to your /usr/local/bin if you are feeling clever.</p>
<h2>How this works for the Windows crowd</h2>
<p>Save this script as <b>c:\ruby\bin\edge_rails.bat</b></p>
<pre>
@echo off
goto endofruby
#!/bin/ruby
git_repo = &quot;git://github.com/rails/rails.git&quot;

help = %Q{
Rails Info:
&nbsp;&nbsp;&nbsp;&nbsp;-v, &#45;-version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Show the Rails version number and quit.
&nbsp;&nbsp;&nbsp;&nbsp;-h, &#45;-help&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Show this help message and quit.
&nbsp;
General Options:
&nbsp;&nbsp;&nbsp;&nbsp;-p, &#45;-pretend&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Run but do not make any changes.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#45;-force&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Overwrite files that already exist.
&nbsp;&nbsp;&nbsp;&nbsp;-s, &#45;-skip&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Skip files that already exist.
&nbsp;&nbsp;&nbsp;&nbsp;-q, &#45;-quiet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Suppress normal output.
&nbsp;&nbsp;&nbsp;&nbsp;-t, &#45;-backtrace&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debugging: show backtrace on errors.
&nbsp;
Description:
&nbsp;&nbsp;&nbsp;&nbsp;The &#039;edge_rails&#039; command creates a new Rails application with a default
&nbsp;&nbsp;&nbsp;&nbsp;directory structure and configuration at the path you specify, using the
&nbsp;&nbsp;&nbsp;&nbsp;very latest version of Rails.
&nbsp;
Example:
&nbsp;&nbsp;&nbsp;&nbsp;edge_rails ~/Code/Ruby/weblog
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
&nbsp;&nbsp;&nbsp;&nbsp;See the README in the newly created application to get going.&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;
}

require &#039;fileutils&#039;
&nbsp;
if ARGV.empty?
&nbsp;&nbsp;puts help
&nbsp;&nbsp;exit
end
&nbsp;
dir = ARGV.shift
args = ARGV.join (&quot; &quot;)
&nbsp;
FileUtils::mkdir(dir)
FileUtils::mkdir(&quot;#{dir}/vendor&quot;)

puts &quot;Exporting EdgeRails from #{git_repo}&quot;
# system &quot;svn export http://svn.rubyonrails.org/rails/trunk #{dir}/vendor/rails&quot;
system &quot;git clone &#45;-depth=1 #{git_repo} #{dir}/vendor/rails&quot;
system &quot;rm -rf #{dir}/vendor/rails/.git*&quot;
&nbsp;
system &quot;ruby #{dir}/vendor/rails/railties/bin/rails #{dir} #{args}&quot;
__END__
:endofruby
&quot;%~d0%~p0ruby&quot; -x &quot;%~f0&quot; %*
</pre>
<p>Now, open a new command prompt and type</p>
<pre>
edge_rails my_new_app
</pre>
<p>If it doesn&#8217;t work, check that you have Git installed properly and have added Git&#8217;s command line utilities to your path.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/04/24/create-a-new-edge-rails-project/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Reverse Proxy Fix 1.0.5.0 released</title>
		<link>http://www.napcsweb.com/blog/2008/03/07/reverse-proxy-fix-1050-released/</link>
		<comments>http://www.napcsweb.com/blog/2008/03/07/reverse-proxy-fix-1050-released/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 01:52:45 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>News</category>

		<category>Rails</category>

		<category>Products</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/03/07/reverse-proxy-fix-1050-released/</guid>
		<description><![CDATA[The reverse_proxy_fix plugin allows a Rails application to live behind a proxy like the one provided by HeliconTech&#8217;s ISAPI_Rewrite plugin as outlined in Deploying Rails Applications. It allows you to configure the base URL that will be prepended to any URL generated by the Rails link_to method and friends. This is useful if you want [...]]]></description>
			<content:encoded><![CDATA[<p>The reverse_proxy_fix plugin allows a Rails application to live behind a proxy like the one provided by HeliconTech&#8217;s ISAPI_Rewrite plugin as outlined in <a href="http://www.pragprog.com/titles/fr_deploy">Deploying Rails Applications</a>. It allows you to configure the base URL that will be prepended to any URL generated by the Rails link_to method and friends. This is useful if you want to force all requests through a frontend or if you want to graft your Rails application onto an existing IIS URL scheme.</p>
<p>This release fixes an issue with named routes and Rails 2.0.  Previous versions of the plugin did not support rewriting of named routes in Rails 2.0 due to the optimization code for named routes. This version of the plugin disables the optimizations.</p>
<h2>Installation</h2>
<p>Installation is simple:</p>
<pre>
&nbsp;&nbsp;ruby script/plugin install http://svn.napcsweb.com/public/reverse_proxy_fix
</pre>
<p>Then provide your base URL, which is the URL you want prepended to all of your URLs. For example, if you are trying to mount your Rails application at http://www.mydomain.com/myapp, you&#8217;d enter that as your base URL.</p>
<p>Next you need to specify which version of Rails you are using. </p>
<p>Comments are welcome, and so are patches if you see something that doesn&#8217;t make sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/03/07/reverse-proxy-fix-1050-released/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Working with Docbook on Windows</title>
		<link>http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-windows/</link>
		<comments>http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-windows/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 01:18:54 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>Howto</category>

		<category>snacks</category>

		<category>tips</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-windows/</guid>
		<description><![CDATA[Setting up a toolchain for working with Docbook on Windows often requires setting things up using Cygwin. Many people are just simply not willing to do that.  This tutorial will show you how to set up a native environment to work with Docbook, and show you how to make CHM and PDF files on [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up a toolchain for working with Docbook on Windows often requires setting things up using Cygwin. Many people are just simply not willing to do that.  This tutorial will show you how to set up a native environment to work with Docbook, and show you how to make CHM and PDF files on Windows.</p>
<p>Thanks to <a href="http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/docbooksys/segmentedhtml/ch03s03.html#DocBookSys-Chapter3-XML-Install-libxml-Windows">http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/docbooksys/segmentedhtml/ch03s03.html#DocBookSys-Chapter3-XML-Install-libxml-Windows</a> for much of this information.</p>
<h2>Getting the tools</h2>
<p>The tools you need to work with Docbook XML and XSTL are all available on Windows. The first thing you need to do is visit <a target="_blank" href="http://www.zlatkovic.com/pub/libxml/">http://www.zlatkovic.com/pub/libxml/</a> (new window) and grab the latest versions of </p>
<ul>
<li>libxml2</li>
<li>libxslt</li>
<li>iconv</li>
</ul>
<p>Download each and unzip the contents of the <bin> folder to <b>c:\windows</b> or another location on your path. For reference, these files are the ones you&#8217;re looking for:</p>
<pre>
iconv.exe
libexslt.dll
libxml2.dll
libxslt.dll
xmlcatalog.exe
xmllint.exe
xsltproc.exe
</pre>
<p>If you feel better about putting these in their own folder, that&#8217;s fine as long as you add the new folder to your path.</p>
<h2>Getting the Stylesheets</h2>
<p>In order to build a book, you need to have the XSLT stylesheets so you can transform your XML into a pretty-looking book with a table of contents and nicely formatted text.<br />
Download the docbook-xml-ns files from sourceforge: <a href="http://sourceforge.net/project/showfiles.php?group_id=21935">http://sourceforge.net/project/showfiles.php?group_id=21935</a></p>
<p>Unpack to your c:\ drive and then rename the extracted folder to <b>c:\docbook-xsl</b></p>
<h2>Generating PDFs</h2>
<p>In order to create a PDF, you have to first convert to the FO format and then use a Java library to convert the FO to a PDF. Apache FOP does this for you. You&#8217;ll need to have a JRE (Java Runtime) installed though. Visit <a href="http://java.sun.com/">http://java.sun.com/</a> for that.</p>
<h3>Get FOP to build PDFs</h3>
<p>Download FOP at <a href="http://www.uniontransit.com/apache/xmlgraphics/fop/fop-0.94-bin-jdk1.4.zip">http://www.uniontransit.com/apache/xmlgraphics/fop/fop-0.94-bin-jdk1.4.zip</a> and unzip it to a temp location. Copy all .jar files  in <strong>build/</strong> and <strong>lib/</strong> to your Java installation&#8217;s lib/ext folder. On my system it&#8217;s <b>C:\Program Files\Java\jre1.5.0_11\lib\ext</b>. Your system will differ depending on your installed version of Java.</p>
<p>Next, download OFFO-hyphenation from <a href="http://offo.sourceforge.net/index.html ">http://offo.sourceforge.net/index.html </a>and grab the <b>offo-hyphenation-fop-stable.zip</b> file from the downloads page and put the jar files in the same folder as the FOP files.</p>
<h2>Building your first book</h2>
<p>Create a project folder called &#8220;my_book&#8221; and create a new file called &#8220;book.xml&#8221; in this folder.</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE book PUBLIC &quot;-//OASIS//DTD DocBook XML V4.5//EN&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd&quot;&gt;
&lt;book&gt;
&nbsp;&nbsp;
&nbsp;&nbsp;&lt;bookinfo&gt;
&nbsp;&nbsp;&lt;title&gt;My Simple Book&lt;/title&gt;
&nbsp;&nbsp;&lt;/bookinfo&gt;

&nbsp;&nbsp;&lt;xi:include xmlns:xi=&quot;http://www.w3.org/2001/XInclude&quot; href=&quot;chapter1.xml&quot;/&gt;
&nbsp;
&lt;/book&gt;
</pre>
<p>Then create a chapter for your book. Create the file <b>chapter1.xml</b> in your project folder with this content:</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE chapter PUBLIC &quot;-//OASIS//DTD DocBook XML V4.5//EN&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd&quot;&gt;
&nbsp;
&lt;chapter id=&quot;chapter1&quot;&gt;
&nbsp;&nbsp;&lt;title&gt;Introduction&lt;/title&gt;
&nbsp;&nbsp;&lt;para&gt;This is just a simple book.&lt;/para&gt;
&nbsp;
&lt;/chapter&gt;
&nbsp;
</pre>
<p>Notice that the chapter and book each have their own doctype, This is really important. Each chapter file needs to have this structure in order to work properly.</p>
<h2>Generating HTML from the document</h2>
<p>The easiest way to use Docbook is to export to HTML. Execute this command to create an HTML version of your book:</p>
<pre>
&nbsp;&nbsp; xsltproc &#45;-xinclude &#45;-output book.html c:/docbook/xsl/html/docbook.xsl book.xml
</pre>
<h2>Creating a makefile to build the PDF</h2>
<p>The PDF creation process is similar to the HTML process but it does require two steps. You need to first convert the document to the <b>FO</b> file format. Then you use FOP to convert it to the PDF. We can automate this by using Ruby.</p>
<p>Create a Ruby file in your project folder called &#8220;make&#8221;. You&#8217;ll use this file to build the PDF of your book.</p>
<pre>
file = ARGV[0]
cmd1 = &quot;xsltproc &#45;-xinclude &#45;-output #{file}.fo c:/docbook-xsl/fo/docbook.xsl #{file}.xml &quot;
cmd2 = &quot;java org.apache.fop.cli.Main -fo #{file}.fo -pdf #{file}.pdf&quot;
&nbsp;
puts &quot;Building FO file&quot;
`#{cmd1}`
&nbsp;
puts &quot;Building PDF&quot;
`#{cmd2}`
&nbsp;
puts &quot;Cleaning up&quot;
`del #{file}.fo`
&nbsp;
puts &quot;Done&quot;
&nbsp;
</pre>
<p>Now, build your book:</p>
<pre>
ruby make book
</pre>
<h2>Creating a Help File</h2>
<p>Generating a Windows HTML Help file (CHM) is pretty similar to the way you make a PDF. You first need to make the HLP file using xsltproc, and then you use a commandline tool to build the CHM.</p>
<p>Grab a copy of Microsoft&#8217;s HTML Help Workshop <a href="http://go.microsoft.com/fwlink/?LinkId=14188">here</a> and install it.  Open a command prompt and copy the <b>hhc</b> file to the c:\windows directory so that the file is on your path.</p>
<pre>
copy &quot;c:Program FilesHTML Help Workshop&quot;hhc.exe c:windows
</pre>
<p>Next, we can use Ruby to make a file to create another build file. Create a file called &#8220;make_chm&#8221; in your project folder.</p>
<pre>
file = ARGV[0]
cmd1 = &quot;xsltproc &#45;-xinclude c:/docbook-xsl/htmlhelp/htmlhelp.xsl #{file}.xml&quot;
cmd2 = &quot;hhc htmlhelp.hhp&quot;
&nbsp;
puts &quot;Building HLP temporary files&quot;
`#{cmd1}`
&nbsp;
puts &quot;Building CHM&quot;
`#{cmd2}`
&nbsp;
puts &quot;Cleaning up&quot;
`rename htmlhelp.chm #{file}.chm`
`del *.hhp`
`del *.hhc`
`del *.html`
&nbsp;
puts &quot;Done&quot;
&nbsp;
</pre>
<h2>Summary</h2>
<p>Docbook is a really great way to create books, tutorials, and documentation in a format that can be transformed into various other formats. It&#8217;s extremly easy to work with in Windows too!
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-windows/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Working with Docbook on the Mac</title>
		<link>http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-the-mac/</link>
		<comments>http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-the-mac/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 15:38:51 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>Howto</category>

		<category>snacks</category>

		<category>tips</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-the-mac/</guid>
		<description><![CDATA[Docbook allows you to prepare documentation by using XML markup. You can create PDFs or HTML exports of your work, and it&#8217;s really nice for collaborating with others, as you can work in a code repository easily. In this article, you&#8217;ll learn how to build a PDF using Docbook XSLT.
Getting the Stylesheets
In order to build [...]]]></description>
			<content:encoded><![CDATA[<p>Docbook allows you to prepare documentation by using XML markup. You can create PDFs or HTML exports of your work, and it&#8217;s really nice for collaborating with others, as you can work in a code repository easily. In this article, you&#8217;ll learn how to build a PDF using Docbook XSLT.</p>
<h2>Getting the Stylesheets</h2>
<p>In order to build a book, you need to have the XSLT stylesheets so you can transform your XML into a pretty-looking book with a table of contents and nicely formatted text.<br />
Download the docbook-xml-ns files from sourceforge: <a href="http://sourceforge.net/project/showfiles.php?group_id=21935">http://sourceforge.net/project/showfiles.php?group_id=21935</a></p>
<p>Unpack to your home directory and rename the folder to <b>~/docbook-xsl</b></p>
<h2>Generating PDFs with Apache FOP</h2>
<p>In order to create a PDF, you have to first convert to the FO format and then use a Java library to convert the FO to a PDF. Apache FOP does this for you.<br />
Get FOP. - <a href="http://www.uniontransit.com/apache/xmlgraphics/fop/fop-0.94-bin-jdk1.4.zip">http://www.uniontransit.com/apache/xmlgraphics/fop/fop-0.94-bin-jdk1.4.zip</a></p>
<p>Unzip to temp location and copy all .jar files in the <strong>build/</strong> and <strong>lib/</strong> folders to ~/Library/Java/Extensions.  Create that folder if it isn&#8217;t there for you already.</p>
<p>Finally, download OFFO from <a href="http://offo.sourceforge.net/index.html">http://offo.sourceforge.net/index.html</a> and grab the <b>offo-hyphenation-fop-stable.zip</b> file from the downloads page and put the jar files in ~/Library/Java/Extensions. This enables hyphenation support. </p>
<h2>Building your first book</h2>
<p>Create a project folder called &#8220;my_book&#8221; and create a new file called &#8220;book.xml&#8221; in this folder.</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE book PUBLIC &quot;-//OASIS//DTD DocBook XML V4.5//EN&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd&quot;&gt;
&lt;book&gt;
&nbsp;&nbsp;
&nbsp;&nbsp;&lt;bookinfo&gt;
&nbsp;&nbsp;&lt;title&gt;My Simple Book&lt;/title&gt;
&nbsp;&nbsp;&lt;/bookinfo&gt;

&nbsp;&nbsp;&lt;xi:include xmlns:xi=&quot;http://www.w3.org/2001/XInclude&quot; href=&quot;chapter1.xml&quot;/&gt;
&nbsp;
&lt;/book&gt;
</pre>
<p>Then creaet a chapter for your book. Create the file <b>chapter1.xml</b> in your project folder with this content:</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE chapter PUBLIC &quot;-//OASIS//DTD DocBook XML V4.5//EN&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd&quot;&gt;
&nbsp;
&lt;chapter id=&quot;chapter1&quot;&gt;
&nbsp;&nbsp;&lt;title&gt;Introduction&lt;/title&gt;
&nbsp;&nbsp;&lt;para&gt;This is just a simple book.&lt;/para&gt;
&nbsp;
&lt;/chapter&gt;
&nbsp;
</pre>
<p>Notice that the chapter and book each have their own doctype, This is really important. Each chapter file needs to have this structure in order to work properly.</p>
<h2>Generating HTML from the document</h2>
<p>The easiest way to use Docbook is to export to HTML. Execute this command to create an HTML version of your book:</p>
<pre>
&nbsp;&nbsp; xsltproc &#45;-xinclude &#45;-output book.html c:/docbook/xsl/html/docbook.xsl book.xml
</pre>
<h2>Creating a makefile to build a PDF</h2>
<p>The PDF creation process is similar to the HTML process but it does require two steps. You need to first convert the document to the <b>FO</b> file format. Then you use FOP to convert it to the PDF. We can automate this by using Ruby.</p>
<p>Create a Ruby makefile in your project folder called &#8220;make&#8221;. You&#8217;ll use this file to build the PDF of your book.</p>
<pre>
file = ARGV[0]
cmd1 = &quot;xsltproc &#45;-xinclude &#45;-output #{file}.fo ~/docbook-xsl/fo/docbook.xsl #{file}.xml &quot;
cmd2 = &quot;java org.apache.fop.cli.Main -fo #{file}.fo -pdf #{file}.pdf&quot;
&nbsp;
puts &quot;Building FO file&quot;
`#{cmd1}`
&nbsp;
puts &quot;Building PDF&quot;
`#{cmd2}`
&nbsp;
puts &quot;Cleaning up&quot;
`rm #{file}.fo`
&nbsp;
puts &quot;Done&quot;
`open #{file}.pdf`
&nbsp;
</pre>
<p>Now, build your book. In your project folder, type</p>
<pre>
ruby make book
</pre>
<h2>Summary</h2>
<p>Now you have a good introduction to how to work with the Docbook format. You may want to use a Textmate bundle to make editing the XML a little easier, but the syntax really isn&#8217;t that hard. </p>
<p>Up next&#8230; creating CHM files using Docbook on Windows.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/03/03/working-with-docbook-on-the-mac/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mini-CMS for your Rails application</title>
		<link>http://www.napcsweb.com/blog/2008/03/03/mini-cms-for-your-rails-application/</link>
		<comments>http://www.napcsweb.com/blog/2008/03/03/mini-cms-for-your-rails-application/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 15:35:31 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>Rails</category>

		<category>snacks</category>

		<category>tips</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/03/03/mini-cms-for-your-rails-application/</guid>
		<description><![CDATA[I&#8217;ve worked on several applications where an end user may need to change text on various sections of a web site. I&#8217;ve used this technique a few times and it&#8217;s worked out well so I thought I&#8217;d share.
We&#8217;re going to create a model called Content which will be backed by a database.  When a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve worked on several applications where an end user may need to change text on various sections of a web site. I&#8217;ve used this technique a few times and it&#8217;s worked out well so I thought I&#8217;d share.</p>
<p>We&#8217;re going to create a model called Content which will be backed by a database.  When a request comes in, we can look at the requested controller and action name to look up all of the content sections for a page. Some pages may have more than one content section.  </p>
<p>This solution is intended to be implemented and modified by developers. An end user would not be able to add new content regions to a system.</p>
<h2>Create the model and table</h2>
<pre>
ruby script/generate model Content
</pre>
<p>Next, modify the migration:</p>
<pre>
class CreateContents &lt; ActiveRecord::Migration
&nbsp;&nbsp;def self.up
&nbsp;&nbsp;&nbsp;&nbsp;create_table :contents do |t|
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.string :controller, :action, :name
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.text :body
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.timestamps
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;
&nbsp;&nbsp;def self.down
&nbsp;&nbsp;&nbsp;&nbsp;drop_table :contents
&nbsp;&nbsp;end
end
&nbsp;
</pre>
<p>The <strong>name</strong> field is what our end user will use to locate and edit a section.  They don&#8217;t need to know the controller and action name.  Instead, they&#8217;ll probably look for a content region like &#8220;Home Page main content&#8221;.  In order to make this work, the user will only be able to edit the <strong>body</strong> field.  The other fields will be handled by the system.</p>
<p>Now, open up the <strong>content</strong> model. We&#8217;re going to add a few methods here to easily grab the content out so we can use it in views.   Out of the box, ActiveRecord allows us to find all the sections for a controller and action name by doing</p>
<pre>
@contents = Content.find_all_by_controller_and_action(&quot;public&quot;, &quot;index&quot;)
</pre>
<p>At first glance, that looks pretty cool, but how do you identify the individual regions? You&#8217;d have to loop through that collection every time you want to output something. If you only have one content area per page, this could be good enough. However, I like things to be a little more spelled-out.</p>
<p>Add this method to the <strong>content</strong> class:</p>
<pre>
&nbsp;&nbsp;def self.get_content_for(controller, action)
&nbsp;&nbsp;&nbsp;&nbsp;c = Content.find_all_by_controller_and_action(controller, action)
&nbsp;&nbsp;&nbsp;&nbsp;content = Hash.new
&nbsp;&nbsp;&nbsp;&nbsp;c.each do |record|
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content[record.name] = record.body
&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;content
&nbsp;&nbsp;end&#039;
</pre>
<p>Now, to fetch all of the sections for the page, we only need to call</p>
<pre>
@contents = Content.get_content_for &quot;public&quot;, &quot;index&quot;
</pre>
<p>This class method fetches all of the content sections and then puts then into a hash which you can afccess via the name.</p>
<h2>Trying it out</h2>
<p>Open up the console and put two content regions in:</p>
<pre>
&nbsp;&nbsp; ruby script/console
</pre>
<pre>
Content.create :controller=&gt;&quot;public&quot;, :action=&gt;&quot;index&quot;, :name=&gt;&quot;intro&quot;, :body =&gt; &quot;Hello world&quot;
Content.create :controller=&gt;&quot;public&quot;, :action=&gt;&quot;index&quot;, :name=&gt;&quot;main_content&quot;, :body =&gt; &quot;Main content goes here&quot;
</pre>
<p>Now quickly generate a new controller and action</p>
<pre>
ruby script/generate controller public index
</pre>
<p>Add this to the public controller</p>
<pre>
before_filter :get_content
</pre>
<p>Now open <strong>application.rb</strong> and add this method:</p>
<pre>
def get_content
&nbsp;&nbsp;@content = Content.get_content_for(params[:controller], params[:action])
end
</pre>
<p>Finally, open up <strong>app/views/public/index.html.erb</strong> and change its contents.</p>
<pre>
&nbsp;
&lt;h1&gt;Home page&lt;/h1&gt;
&nbsp;
&lt;%=@contents[&quot;intro&quot;] %&gt;
&nbsp;
&lt;h2&gt;About us&lt;/h2&gt;
&lt;%=@contents[&quot;main_content&quot;] %&gt;
&nbsp;
</pre>
<h2>Summary</h2>
<p>I&#8217;ve used this approach in a few sites and it&#8217;s served me well. I&#8217;d love to hear how you&#8217;ve done the same. Post ideas and suggestions in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/03/03/mini-cms-for-your-rails-application/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsetreee gem available for Windows!</title>
		<link>http://www.napcsweb.com/blog/2008/01/01/parsetreee-gem-available-for-windows/</link>
		<comments>http://www.napcsweb.com/blog/2008/01/01/parsetreee-gem-available-for-windows/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 02:19:26 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>Rails</category>

		<category>tips</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/01/01/parsetreee-gem-available-for-windows/</guid>
		<description><![CDATA[Luis Laverna compiled and released the parsetree gem for Windows. This means that libraries like ruby2ruby, heckle, flog, and even the merb framework are now available to Windows Rubyists.
You need to look at flog. It can show you how bad your code really is.

]]></description>
			<content:encoded><![CDATA[<p>Luis Laverna compiled and released the <b>parsetree</b> gem for Windows. This means that libraries like ruby2ruby, heckle, flog, and even the merb framework are now available to Windows Rubyists.</p>
<p>You need to look at <a href="http://ruby.sadi.st/Flog.html">flog</a>. It can show you how bad your code really is.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/01/01/parsetreee-gem-available-for-windows/feed/</wfw:commentRss>
		</item>
		<item>
		<title>mongrel_service broken on Windows again.</title>
		<link>http://www.napcsweb.com/blog/2008/01/01/mongrel_service-broken-on-windows-again/</link>
		<comments>http://www.napcsweb.com/blog/2008/01/01/mongrel_service-broken-on-windows-again/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 02:14:09 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>News</category>

		<category>Rails</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/01/01/mongrel_service-broken-on-windows-again/</guid>
		<description><![CDATA[If you&#8217;re working with the mongrel_service gem then you should know that right now there&#8217;s a few kinks in the system.
Here&#8217;s a quick rundown of the setup for Rails on Windows from scratch:
Download the One Click Ruby Installer and run the setup program.
Open a command prompt and type these commands:

gem update &#45;-system
gem install rails
gem install [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working with the <b>mongrel_service</b> gem then you should know that right now there&#8217;s a few kinks in the system.</p>
<p>Here&#8217;s a quick rundown of the setup for Rails on Windows from scratch:</p>
<p>Download the<a href="https://rubyforge.org/frs/download.php/29263/ruby186-26.exe"> One Click Ruby Installer </a>and run the setup program.</p>
<p>Open a command prompt and type these commands:</p>
<pre>
gem update &#45;-system
gem install rails
gem install mongrel
gem install win32-service -v &#039;0.5.2&#039;
gem install mongrel_service
</pre>
<p>The key to this is that the win32-service gem is currently broken, so switching to a previous version (0.5.2) works great for us. Credit for this goes to Luis Laverna, the creator of the mongrel_service gem. See the <a href="http://groups.google.com/group/ruby-talk-google/browse_thread/thread/c80d9eab2377607b/45786b993a6ef9c3?lnk=raot">discussion</a>.
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/01/01/mongrel_service-broken-on-windows-again/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How To Follow Along with Agile Web Development with Rails Second Edition</title>
		<link>http://www.napcsweb.com/blog/2008/01/01/how-to-follow-along-with-agile-web-development-with-rails-second-edition/</link>
		<comments>http://www.napcsweb.com/blog/2008/01/01/how-to-follow-along-with-agile-web-development-with-rails-second-edition/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 02:05:58 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
		
		<category>Rails</category>

		<category>tips</category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/2008/01/01/how-to-follow-along-with-agile-web-development-with-rails-second-edition/</guid>
		<description><![CDATA[This is merely for reference for anyone out there new to Rails or Ruby and trying to work with the Agile book. Version 2.0.2 of Rails is out now and it&#8217;s just way too difficult to follow along with the great AWDWR book from Pragmatic.  To be successful with that book, you need to [...]]]></description>
			<content:encoded><![CDATA[<p>This is merely for reference for anyone out there new to Rails or Ruby and trying to work with the Agile book. Version 2.0.2 of Rails is out now and it&#8217;s just way too difficult to follow along with the great AWDWR book from Pragmatic.  To be successful with that book, you need to use Rails 1.2.3.  Here&#8217;s how you do it:</p>
<h2>Mac</h2>
<p>If you have a brand new macbook pro, you&#8217;re done. It comes equipped with Rails 1.2.3.  Start following along with the first Hello World program.</p>
<p>If you&#8217;ve accidentally installed Rails 2.0, you can start a Rails 1.2.3 project like this:</p>
<pre>
sudo gem update &#45;-system
sudo gem install rails -v=1.2.3
rails _1.2.3_ depot
</pre>
<p>where <b>depot</b> is the name of your project.</p>
<h2>Windows</h2>
<p>If you&#8217;ve got a Windows machine, start from scratch.  Grab <a href="https://rubyforge.org/frs/download.php/29263/ruby186-26.exe">The one click ruby installer</a> (Windows installer) and install it.</p>
<p>Next, open up a command prompt and install Rails 1.2.3, and Mongrel</p>
<pre>
gem update &#45;-system
gem install rails -v &#039;1.2.3&#039;
gem install mongrel
gem install mysql
</pre>
<p>Now just go ahead and follow along with the book. When you&#8217;re done, you can update to Rails 2.0 by doing </p>
<pre>
gem install rails
</pre>
<p>which will install the latest version of Rails.  Your current Rails apps are tied to the version of Rails they are created on.  You change the version of Rails the app uses by &#8220;freezing gems&#8221; (placing a copy of Rails in the app&#8217;s vendor/rails folder, or by editing the app&#8217;s <b>config/environment.rb</b> file and changing the version specified there. Do a web search for more info on this.</p>
<p>You can specify the version of Rails you want to use by passing it along on the commandline</p>
<pre>
rails _1.2.3_ old_rails_app
rails _1.2.6_ ready_to_update_app
rails _1.1.6_ legacy app
rails _2.0.2_ new_rails_app
</pre>
<p>Just make sure you have all of the Rails versions that you want to use on your machine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2008/01/01/how-to-follow-along-with-agile-web-development-with-rails-second-edition/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
