<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cowboy Coded</title>
	<atom:link href="http://www.cowboycoded.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cowboycoded.com</link>
	<description>Tech stuff.. Ruby, Rails, MySQL, Nginx, yada yada yada..</description>
	<lastBuildDate>Tue, 24 Apr 2012 03:47:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>My Journey into the world of vim (from Textmate)</title>
		<link>http://www.cowboycoded.com/2012/03/06/my-journey-into-the-world-of-vim-from-textmate/</link>
		<comments>http://www.cowboycoded.com/2012/03/06/my-journey-into-the-world-of-vim-from-textmate/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 16:03:04 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=1038</guid>
		<description><![CDATA[I used Textmate for almost 3 years before deciding to switch to vim.  I watched one of my coworkers use it and was impressed by the fact that his hands never had to leave the keyboard and navigating around files was much faster with vim.
I started out using macvim and the janus vim distrubution.  It [...]]]></description>
			<content:encoded><![CDATA[<p>I used Textmate for almost 3 years before deciding to switch to vim.  I watched one of my coworkers use it and was impressed by the fact that his hands never had to leave the keyboard and navigating around files was much faster with vim.</p>
<p>I started out using macvim and the <a href="https://github.com/carlhuda/janus">janus</a> vim distrubution.  It is packed with all kinds of goodies for Ruby/Rails development.  The first 2 weeks were painful and I had to force myself to stick with it.  I was definitely less productive during those 2 weeks, but after I got past the learning curve my productivity started increasing and now I am much more productive than I was with Textmate.  I am far from mastering it and I think those productivity gains will continue to increase as I learn more and get more comfortable with the keyboard shortcuts.</p>
<p>2 weeks ago, I tried to upgrade Janus and it caused my environment to become unstable.. crashes, performance problems, etc..  I decided to trash my whole vim environment and start over.  In the process I found another vim distribution &#8211; <a href="https://github.com/skwp/dotfiles">YADR</a>.  I had to use it after reading the documentation.  IMO it is better than Janus, but it is also very opinionated.. so definitely not for everyone.</p>
<p>I will be keeping a log of customizations, workarounds, etc in this blog post mainly for my own reference.</p>
<h3><strong>Problems:</strong></h3>
<p><strong>1. SCSS syntax checker fails to recognize SCSS imports from a Rails Engine</strong></p>
<p>I am using thoughtbot&#8217;s bourbon SCSS mixins.  The mixins are part of an engine that outputs to the asset pipeline, so the vim plugin syntastic was not able to recognize the includes.  For some reason it would kill the syntax highlighting and throw the cursor to the start of the file every time I saved w/ Command+s.  I don&#8217;t really need the syntax checking for SCSS, since the asset pipeline will automatically show me errors.  To disable it, I just deleted the the syntax checker (I guess you could rename it also if you wanted to keep it around):</p>
<div class="rbox"><span class='shell'>cowboycoded$&nbsp;&nbsp;</span>rm ~/.vim/bundle/scrooloose-syntastic/syntax_checkers/scss.vim</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2012/03/06/my-journey-into-the-world-of-vim-from-textmate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Change the default shell in OSX to zsh</title>
		<link>http://www.cowboycoded.com/2012/03/03/change-the-default-shell-in-osx-to-zsh/</link>
		<comments>http://www.cowboycoded.com/2012/03/03/change-the-default-shell-in-osx-to-zsh/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 03:18:56 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=1036</guid>
		<description><![CDATA[I recently started using YADR as a replacement for the janus macvim setup.  They recommend switching from bash to zsh so I figured I would give it a try.  It is easy to change the default shell from bash to zsh in OSX (Lion).  Just run the following command:
cowboycoded$&#160;&#160;chsh -s /bin/zsh
Open up [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started using <a href="http://skwp.github.com/dotfiles/">YADR</a> as a replacement for the janus macvim setup.  They recommend switching from bash to zsh so I figured I would give it a try.  It is easy to change the default shell from bash to zsh in OSX (Lion).  Just run the following command:</p>
<div class="rbox"><span class='shell'>cowboycoded$&nbsp;&nbsp;</span>chsh -s /bin/zsh</div>
<p>Open up a new terminal and you should be on zsh</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2012/03/03/change-the-default-shell-in-osx-to-zsh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Patching Formtastic to append HTML to input list elements</title>
		<link>http://www.cowboycoded.com/2011/07/18/patching-formtastic-to-append-html-to-input-list-elements/</link>
		<comments>http://www.cowboycoded.com/2011/07/18/patching-formtastic-to-append-html-to-input-list-elements/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 16:09:17 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[formtastic]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=999</guid>
		<description><![CDATA[Formtastic is pretty killer when it comes to creating nice looking forms very quickly.  I love the short syntax compared to the default Rails form helpers, but sometimes I find the black box helpers a bit restricting when it comes to customizing the forms.  Usually modifying the CSS is enough to get desired [...]]]></description>
			<content:encoded><![CDATA[<p>Formtastic is pretty killer when it comes to creating nice looking forms very quickly.  I love the short syntax compared to the default Rails form helpers, but sometimes I find the black box helpers a bit restricting when it comes to customizing the forms.  Usually modifying the CSS is enough to get desired results, but I ran into a case today where it would have been an ugly hack to make it work with CSS.</p>
<h4>Problem</h4>
<p>I need to add HTML inside the &lt;li&gt; tag that is produced by the formtastic input helper.  The helper produces HTML that looks like this:</p>
<p><script src="https://gist.github.com/1089725.js?file=formtastic_output.html"></script></p>
<p>I want to insert a link in betweent the input field and the inline hints.  This is not possible, since the formtastic input helper outputs this entire HTML snippet.  You can customize plenty of things within the helper, but I did not see a way to add more html content.</p>
<h4>Solution</h4>
<p>As I started to dig through the formtastic source I noticed that all of the input elements are using a wrapper module that produces the content inside the &lt;li&gt; tag.  Take a look at this module:</p>
<p><script src="https://gist.github.com/1089744.js?file=wrapping.rb"></script></p>
<p>All of the Formtastic input classes (StringInput, TextInput, HiddenInput,etc..) pass a block to the input_wrapping method inside their to_html method.  The content_tag method takes the element name, content and element options as arguments.  See here for more info:  <a href="http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag">http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag</a><br />
In order to add my content, I will need to add a String element (my HTML) to this array:</p>
<p><script src="https://gist.github.com/1089933.js?file=array.rb"></script></p>
<p>As you can see in the code above, it is just an array of strings that are joined together by carriage returns and marked as safe using the Rails String core_ext html_safe.  So I will have to insert my HTML code in the desired position of this array.  In my scenario, it makes sense to put the extra HTML between the input tag and the error html.  I decided to monkey patch this method (oops, maybe dangerous if I ever upgrade formtastic for this app).  I added this code to /app/config/initializers/formtastic_monkey_patch.rb:</p>
<p><script src="https://gist.github.com/1089852.js?file=formtastic_monkey_patch.rb"></script></p>
<p>The first thing I needed to patch was the Html module.  3 methods were added to this module to allow me to pass :extra_html and :extra_html_class to the formtastic input helper.  This was pretty much a copy/paste from &#8220;Formtastic::Inputs::Base::Hints&#8221;.  Then I just had to add the &#8220;extra_html&#8221; to the array inside the &#8220;input_wrapping&#8221; method.  Now I can do this in my form:</p>
<p><script src="https://gist.github.com/1089938.js?file=formtastic_input.haml"></script></p>
<p>This will output:</p>
<p><script src="https://gist.github.com/1089887.js?file=formtastic_patch_output.html"></script></p>
<p>I can take this a step further by adding another option for the order that the extra html will appear inside the &lt;li&gt; tag.  New monkey patch:</p>
<p><script src="https://gist.github.com/1089909.js?file=new_formtastic_monkey.rb"></script></p>
<p>I added one method inside the Html class to set a default extra_html_position within the options hash.  Then I modified &#8220;input wrapping&#8221; so it inserts the extra_html into the specified position.  Here is the new formtastic input call in my view:</p>
<p><script src="https://gist.github.com/1089941.js?file=input.haml"></script></p>
<p>By passing the :extra_html_position option with a value of zero, formtastic will now insert the extra_html before the input tag.  So there ya go&#8230;  monkey-patched and ready to roll.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/07/18/patching-formtastic-to-append-html-to-input-list-elements/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Testing Facebook authentication with Rails 3 Cucumber, Capybara, Selenium</title>
		<link>http://www.cowboycoded.com/2011/05/06/testing-facebook-authentication-with-rails-3-cucumber-capybara-selenium/</link>
		<comments>http://www.cowboycoded.com/2011/05/06/testing-facebook-authentication-with-rails-3-cucumber-capybara-selenium/#comments</comments>
		<pubDate>Fri, 06 May 2011 15:57:35 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[capybara]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=949</guid>
		<description><![CDATA[I am in the process of creating an engine that will simplify adding single signon to my app using omniauth &#38; devise.  One of the requirements is testing this with Cucumber, and it took me a little while to figure this out, so I figured I would pass it on.
Add a test app on [...]]]></description>
			<content:encoded><![CDATA[<p>I am in the process of creating an engine that will simplify adding single signon to my app using omniauth &amp; devise.  One of the requirements is testing this with Cucumber, and it took me a little while to figure this out, so I figured I would pass it on.</p>
<h4>Add a test app on Facebook</h4>
<p>The first thing you need to do is set up a test app on the <a href="http://www.facebook.com/developers/">Facebook developers site</a>.  This will allow you to use localhost as the callback url.  Otherwise, FB API will not let you authenticate, b/c the FB App configuration URL will not match your app&#8217;s URL.  You will probably want 3 different FB apps.  One for dev, test, and prod using localhost:3000, locahost:3001 and yourprodurl.com.</p>
<p>In your test FB app settings, click the &#8220;Web Site&#8221; tab and make the Site URL = localhost:3001</p>
<div style="width: 100%; float: left; margin: 20px 0;"><a href="http://www.cowboycoded.com/wp-content/uploads/2011/05/fb-app-settings.png"><img class="alignleft size-full wp-image-952" title="fb-app-settings" src="http://www.cowboycoded.com/wp-content/uploads/2011/05/fb-app-settings.png" alt="" width="750" height="265" /></a></div>
<p>Save your setttings.</p>
<h4>Add mongrel to Gemfile</h4>
<p>I added mongrel to my test environment instead of using webrick (b/c of issues with header size of google apps callback).  I would recommend doing the same.  Just add it to your gemfile under the dev and test groups.  Here is my Gemfile (with a bunch of other testing gems included):</p>
<p><script src="https://gist.github.com/959161.js?file=Gemfile"></script> Make sure you run:</p>
<div class="rbox"><span class='shell'>cowboycoded$&nbsp;&nbsp;</span>bundle install</div>
<h4>Change Capybara server settings</h4>
<p>Add these lines to &#8220;features/support/env.rb&#8221;:<br />
<script src="https://gist.github.com/959181.js?file=env.rb"></script></p>
<p>You need to specify the port so mongrel is forced to start on 3001.  You also need to specify the app_host as localhost, because the default &#8220;127.0.0.1&#8243; was causing problems with FB API (url encoding I think)</p>
<h4>Use Selenium in your scenarios</h4>
<p>Preface your cucumber scenarios with @selenium, so you can navigate through external sites like Facebook</p>
<p><script src="https://gist.github.com/959238.js?file=your.feature"></script></p>
<p>Of course you need to set up your cucumber step, based on how you are authenticating against facebook.  I don&#8217;t provide the implementation details here.  Just know that cuc is hitting the facebook URL in the first step of this scenario.</p>
<h4>Run cucumber scenarios</h4>
<div class="rbox"><span class='shell'>cowboycoded$&nbsp;&nbsp;</span>bundle exec cucumber features/</div>
<p>You should be good to go now.  Facebook will use the correct callback URL and your server will be running on the correct port to handle it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/05/06/testing-facebook-authentication-with-rails-3-cucumber-capybara-selenium/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Round to half intervals (round with fractions) in Ruby</title>
		<link>http://www.cowboycoded.com/2011/04/27/round-to-half-intervals-5-in-ruby/</link>
		<comments>http://www.cowboycoded.com/2011/04/27/round-to-half-intervals-5-in-ruby/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 19:29:45 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[rounding]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=938</guid>
		<description><![CDATA[Let&#8217;s say you need to round to half numbers in Ruby, because you are making a rating system and want to display half stars for the ratings (1, 1.5, 2, 2.5, etc..).  You want the following output when you are trying to round to the half fraction:

round_to_half( 1.6 )
=> 1.5
round_to_half( 1.7 )
=> 1.5
round_to_half( 1.8 [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you need to round to half numbers in Ruby, because you are making a rating system and want to display half stars for the ratings (1, 1.5, 2, 2.5, etc..).  You want the following output when you are trying to round to the half fraction:</p>
<div class="rbox">
round_to_half( 1.6 )<br />
=> 1.5</p>
<p>round_to_half( 1.7 )<br />
=> 1.5</p>
<p>round_to_half( 1.8 )<br />
=> 2.0</p>
<p>round_to_half( 2.1)<br />
=> 2.0</p>
<p>round_to_half( 2.3 )<br />
=> 2.5</p>
<p>etc&#8230;..
</p></div>
<p>It is much simpler than I anticpated.  You just need to multiply by the the inverse of the fraction (am I saying that right??), round the number and divide by the same number.  So if you want to round to half places, you would do this:</p>
<div class="rbox">
<strong>1. </strong>flip 1/2 around to make it 2/1 (or 2)<br />
<strong>2. </strong>multiply by the number you want to round<br />
<strong>3. </strong>round the result from step 2<br />
<strong>4. </strong>divide the result from step 3 by the number you got in step 1 (2 in this case)
</div>
<p>Lets start with the first one in my example and run through the steps:</p>
<div class="rbox">
<strong>1. </strong> 2/1 => 2<br />
<strong>2. </strong>5.6 * 2 => 11.2<br />
<strong>3. </strong>11.2.round => 11<br />
<strong>4. </strong>11 / 2.0 => 5.5
</div>
<p>And here it is in a Ruby method:<br />
<script src="https://gist.github.com/944985.js?file=round_by_fraction.rb"></script></p>
<p>So you can pass any fraction to this method and it will round to that fraction.  For example, you can try this with 20ths:</p>
<div class="rbox">
round_to_fraction(2.224, 1.0/20.0)<br />
=> 2.2</p>
<p>round_to_fraction(2.225, 1.0/20.0)<br />
=> 2.25
</p></div>
<p>Remember to add the .0 to the end of the numbers you are dividing, so it forces ruby to treat it as a float.  1.0 / 20.0</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/04/27/round-to-half-intervals-5-in-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Add existing records to the versions table in paper_trail</title>
		<link>http://www.cowboycoded.com/2011/04/19/add-existing-records-to-the-versions-table-in-paper_trail/</link>
		<comments>http://www.cowboycoded.com/2011/04/19/add-existing-records-to-the-versions-table-in-paper_trail/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 19:05:00 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[paper_trail]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=920</guid>
		<description><![CDATA[I was working with paper_trail today.  It is an excellent gem that saves a history of your creates, updates, and destroys for Rails ActiveRecord models.  I am adding this gem to an existing project that already has records in the database.  I need to put these records in the paper_trail&#8217;s versions table, [...]]]></description>
			<content:encoded><![CDATA[<p>I was working with <a href="https://github.com/airblade/paper_trail">paper_trail</a> today.  It is an excellent gem that saves a history of your creates, updates, and destroys for Rails ActiveRecord models.  I am adding this gem to an existing project that already has records in the database.  I need to put these records in the paper_trail&#8217;s <code>versions</code> table, so I can use a query that checks to see how many versions exist for a particular model instance.  Just to make it clean, so I don&#8217;t have to query both the model and the versions table, I want to add that initial &#8220;create&#8221; version for all of my existing records.  After reading through the source, I could not figure out an easy way to do it with the existing codebase, so I decided to create a simple monkey patch and rake task that will version all of my existing records.</p>
<p><script src="https://gist.github.com/929262.js?file=create_paper_trail_versions.rb"></script></p>
<p>This rake task adds a new instance method, so the model can have access to the private method &#8220;record_create&#8221; in paper_trail.  Then it takes all model classes, iterates through them and detects the classes that have paper_trail enabled.  If the record has no versions, then it creates the initial version and prints out the result.  Now I have a fresh set of data that makes it look like paper_trail was installed to begin with (minus any history I&#8217;ve already lost).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/04/19/add-existing-records-to-the-versions-table-in-paper_trail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lock down a Rails 3 app with a single password using lock</title>
		<link>http://www.cowboycoded.com/2011/04/11/lock-down-a-rails-3-app-with-a-single-password-using-lock/</link>
		<comments>http://www.cowboycoded.com/2011/04/11/lock-down-a-rails-3-app-with-a-single-password-using-lock/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 19:17:55 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[rails engines]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=882</guid>
		<description><![CDATA[This tutorial shows you more about the power and simplicity of Rails 3 Engines.  When I release a new feature for one of my  production Rails apps (or an entirely new app) that needs to be beta tested by a select group of people, it is usually necessary to password protect part or all [...]]]></description>
			<content:encoded><![CDATA[<p><a href="https://github.com/cowboycoded/lock"><img class="alignleft" style="margin-right: 15px;" title="Lock Engine" src="https://github.com/cowboycoded/lock/raw/master/logo.png" alt="" width="168" height="76" /></a>This tutorial shows you more about the power and simplicity of Rails 3 Engines.  When I release a new feature for one of my  production Rails apps (or an entirely new app) that needs to be beta tested by a select group of people, it is usually necessary to password protect part or all of the application.  A quick and dirty way to do this is to use basic http authentication with Nginx.  Previously, I would choose this method because it is fairly easy to set up and it allowed me to create a single username and password for an entire group of testers, without having to modify the user authentication system that I use inside my Rails apps. If you are using nginx, then you can add something like this to the nginx.conf file:<br />
<script src="https://gist.github.com/913884.js?file=nginx.conf"></script> It is also necessary to create the htpasswd file and add the user name and password hash.  The password hash can be generated using Ruby&#8217;s String crypt method.  When users visit the protected URL, the basic http auth browser popup is displayed and if they enter the correct login information, they gain access to the protected URL.  While this setup is fairly simple, I prefer staying within Rails to achieve this and having more control over the login page, etc.  HTTP Basic Authentication is a bit too basic for me.  I decided to create a simple Rails 3 Engine that would allow me to easily lock down controller/actions and customize the login and error pages.  I call this one <a href="https://github.com/cowboycoded/lock">lock</a>.  Note that this will not protect your public assets, only the Rails actions&#8230; so you may want to lock down your server using http basic if you need public assets protected as well.</p>
<p>Here is how you use it in your own application:</p>
<h4>1. Install the gem</h4>
<div class="rbox">#Gemfile<br/>gem &#8220;lock&#8221;</div>
<div class="rbox"><span class='shell'>cowboycoded$&nbsp;&nbsp;</span>bundle install</div>
<h4>2. Generate the password file</h4>
<div class="rbox"><span class='shell'>cowboycoded$&nbsp;&nbsp;</span>rails g lock:create_password_file yourpasswordhere</div>
<h4>3. Add the lock configuration to application_controller.rb</h4>
<p><script src="https://gist.github.com/913924.js?file=lock_tutorial.rb"></script></p>
<h4>4. Unlock the app</h4>
<p>visit this URL and enter the password you created with the generator</p>
<div class="rbox">http://www.yoursitehere.com/lock/login</div>
<h4>5. Override the views:</h4>
<div class="rbox"><strong>Login Page:</strong> /app/views/lock/login.html.erb  #NOTE: POST to /lock/unlock and use a field named &#8220;password&#8221;<br />
<strong>Refused Access Page:</strong> /app/views/lock/refused.html.erb<br />
<strong>Unlock Confirmation Page:</strong> /app/views/lock/unlock.html.erb</div>
<p>By default, these views will render inside your default layout. To create a custom layout for these files, just add /app/views/layouts/lock.html.erb The layout must contain a yield.</p>
<h2>TECH DETAILS ABOUT THE GEM</h2>
<h4>(if you are interested in how the gem was created)</h4>
<p>The design around this was simple.  Use a class method within the application_controller that accepts controllers &amp; actions as parameters.   The class method creates a before_filter and that takes these controllers &amp; actions and compares them against the requested URL.  If the URL matches the controller/action and the user has not unlocked the app, then access to the page is denied.   Using Rails 3 engines makes it easy to add views and a LockController to handle the login, failed login and unlock confirmation pages.  Here are the gory details about how this gem was created:</p>
<h4>A Note about TESTING</h4>
<p>I am using RSpec and Cucumber to test this engine.  Engines are a different beast than other gems, because they are so closely tied to Rails.  As a result of this, I prefer to create a new Rails 3 app inside my gem called &#8220;test_app&#8221; for any testing.  I have seen a few things out there about creating a dummy app, that I am guessing is a stripped down version of a real rails app, but I am not sure what the advantage is over a real Rails app for testing.  The footprint of a blank Rails app is not that large, so I doubt it is a concern about gem file size (which should not matter, since the test files are not packaged with the gem).  I still need to read up and give it a fair comparison, but for now I dont see a problem with using a real Rails app to test engines.</p>
<p>First, I bootstrap the &#8220;test_app&#8221; with rspec and cucumber.  Then I include the local path to the gem in the Gemfile:</p>
<div class="rbox">#Gemfile<br/><br />
gem &#8220;lock&#8221;, :path=&gt;&#8221;/my_plugins/lock&#8221;</div>
<p>Providing the path to the local gem, will tell Bundler to load the files in that directory as opposed to installed the packaged gem.  This is necessary, in order to avoid building your gem after every code change.  If I remember correctly, you will need to generate a gemspec in the root of the gem before you bundle install with the &#8220;path&#8221; option.</p>
<h4>Start with a gem skelaton and make it an engine</h4>
<p>The skelaton can be as simple as a &#8220;lib&#8221; directory and a file inside the lib directory named &#8220;lock.rb&#8221;.  To make it an engine only one file is needed.  I always call it &#8220;engine.rb&#8221; and place it within a subdirectory of lib that matches the name of my engine.  So here is my directory structure so far:</p>
<div class="rbox">/lock/lib/lock.rb<br />
/lock/lib/lock/engine.rb</div>
<p>When the gem is loaded, it will first execute lock.rb, so the engine.rb file must be required in lock.rb:</p>
<p><script src="https://gist.github.com/913966.js?file=lock.rb"></script> The engine.rb file needs to subclass Rails::Engine, require rails and lock itself, in order to make it an engine that Rails can load:  <script src="https://gist.github.com/913973.js?file=engine.rb"></script></p>
<p>Now the gem can function as an engine.  So what does that really mean?  Well for one, you use the same directory structure as you would in a normal Rails app.  And you can add initializers to engine.rb to interact with rails when it is loading at boot.</p>
<h4>Add the Rails-like directory structure</h4>
<p>Take a look at my Rails-like directory structure:</p>
<div style="width: 100%; float: left;"><a href="http://www.cowboycoded.com/wp-content/uploads/2011/04/Screen-shot-2011-04-11-at-2.17.37-PM.png"><img class="alignleft size-full wp-image-894" title="Screen shot 2011-04-11 at 2.17.37 PM" src="http://www.cowboycoded.com/wp-content/uploads/2011/04/Screen-shot-2011-04-11-at-2.17.37-PM.png" alt="" width="262" height="224" /></a></div>
<p>The helper &amp; model directories are not necessary for this gem, but I went ahead and added them in case I need them later.</p>
<h4>Add the views &amp; controllers</h4>
<p>As I mentioned earlier I have 3 views that I use for the login form, unlock confirmation, and refusal page&#8230; there is not much to these views.  Rails Engines also allow you to add routes, so I placed mine in /lock/config/routes.rb.  Most of the code (and its not really much) is in the controllers.  I have a lock_controller that handles the login.  As you can see, it just checks to see if the form password is equal to the generated password.  I placed the &#8220;password_match?&#8221; method in /lib/lock.rb, to reduce the logic in the controller.</p>
<p><script src="https://gist.github.com/913995.js?file=lock_controlle.rb"></script> <script src="https://gist.github.com/913997.js?file=lock.rb"></script></p>
<p>I wrote a few blogs in March about how Rails prevents you from reopening engine classes.  Basically, if you have the file in the parent Rails app, then the one in the engine is ignored.  For this reason, I was not able to create a file named &#8220;application_contoller.rb&#8221; and simply add my methods to it.  In order to make this work, I have to use a different file name (lock_application_controller.rb) and include/extend the methods into action_controller using an initializer.  Here is what the module looks like:</p>
<p><script src="https://gist.github.com/914016.js?file=lock_application_controller.rb"></script> When you create a class method and put it in action_controller, then application_controller can call this method and make it look like a nice DSL (as you can see in step #3 of the gem installation tutorial).  So this class method just adds a before_filter and an instance method is created that matches what the before_filter is calling.  lock_filter checks to see if it is a locked action and if the user has already unlocked the app.  It redirects them to the refusal URL if they have not unlocked and the action is on the lock list.  Note that you may prefer to use ActiveSupport::Concern when coding a module that includes instance methods and extends class methods.  I am not using it here, but many find the syntax a bit sweeter, so if you dont know about it then google it.</p>
<h4>Use an initializer to include the instance methods and extend the class methods in action_controller</h4>
<p>There is a pretty cool feature in Rails 3, which allows you to lazy load code in initializers.  Check out Simone Carletti&#8217;s tutorial on <a href="http://www.simonecarletti.com/blog/2011/04/understanding-ruby-and-rails-lazy-load-hooks/">Lazy Load Hooks</a> for details on how this works.  In short, when action_controller is loaded, our code can be placed in that class.  So I want to include/extend my methods in action_controller.  Here is what the engine.rb file looks like after I add this code:<br />
<script src="https://gist.github.com/914040.js?file=engine.rb"></script></p>
<p>I find that the engine.rb is the best place to put code that dynamically modifies classes at boot.  It gives you a central location where you can place all of these calls, and IMO makes engines easier to read.  You know exactly what modifications are being made to Rails, just by looking at the engine file.</p>
<h4>Creating the password generator</h4>
<p>The last piece of the puzzle is to create a Rails generator to spit out our hashed password file.  When you make your gem an engine, Rails automatically picks up your generator if you put it in the default path that is expected from an engine.  Here is what the directory structure looks like:</p>
<div style="width: 100%; float: left;"><a href="http://www.cowboycoded.com/wp-content/uploads/2011/04/Screen-shot-2011-04-11-at-3.02.18-PM.png"><img class="alignleft size-full wp-image-899" title="Screen shot 2011-04-11 at 3.02.18 PM" src="http://www.cowboycoded.com/wp-content/uploads/2011/04/Screen-shot-2011-04-11-at-3.02.18-PM.png" alt="" width="291" height="114" /></a></div>
<p>So Rails is expecting this path for the generator class (expressing variables in uppercase):</p>
<div class="rbox">
<pre>/GEM_NAME/lib/generators/GEM_NAME/GENERATOR_NAME/GENERATOR_NAME_generator.rb</pre>
</div>
<p>USAGE will allow rails to ouput some documentation for it, and the templates directory (which I don&#8217;t use in lock) can house any template files needed by the generator.  The generator class is pretty basic:</p>
<p><script src="https://gist.github.com/914090.js?file=create_password_file_generator.rb"></script></p>
<p>Subclassing &#8220;Rails::Generators::Base&#8221; gives you the functionality you need to make this a generator.  Any methods contained within this class will be executed.  Notice that the argument class method is necessary to pull in the command line arg for the password.  source_root will tell the generator where your templates are stored.  This code generates a salt and a password hash from the password &amp; salt combination and writes it to a file using the create_file method.  You can read more about <a href="http://guides.rubyonrails.org/generators.html">Rails generators HERE</a>.</p>
<p>So there you have it.  Pretty basic, but IMO useful.  I hope you find this gem useful and also hope you learned something about Rails Engines.  If you have any problems with this gem, leave me a comment or open a github issue.  As always, suggestions are welcome, as well as criticism!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/04/11/lock-down-a-rails-3-app-with-a-single-password-using-lock/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Application-wide variables for your Rails 3 app using Yettings</title>
		<link>http://www.cowboycoded.com/2011/04/08/application-wide-variables-for-your-rails-3-app-using-yettings/</link>
		<comments>http://www.cowboycoded.com/2011/04/08/application-wide-variables-for-your-rails-3-app-using-yettings/#comments</comments>
		<pubDate>Sat, 09 Apr 2011 01:54:29 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails plugins]]></category>
		<category><![CDATA[rubygems]]></category>
		<category><![CDATA[yettings]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=866</guid>
		<description><![CDATA[Application-wide settings for Rails apps can be very useful in many different scenarios.  For example, let&#8217;s say you want a place to store API keys for various 3rd party services used in your app.  Instead of hard-coding them in the model, I think it is much cleaner to have a settings file where you store [...]]]></description>
			<content:encoded><![CDATA[<p>Application-wide settings for Rails apps can be very useful in many different scenarios.  For example, let&#8217;s say you want a place to store API keys for various 3rd party services used in your app.  Instead of hard-coding them in the model, I think it is much cleaner to have a settings file where you store all of this information.  And to make things even easier, you should be able to define different values for dev, test, and production.  There are many ways to achieve this.  You can set some Ruby constants and namespace them or create a model that stores these settings in the database.  I prefer a simpler solution that does not require much setup.  Initially I was using <a href="https://github.com/binarylogic/settingslogic">settingslogic</a>, which I found very useful, but I decided to take this a bit further and build a plugin that I could maintain and one that fits my needs.  <a href="https://github.com/cowboycoded/yettings">Yettings</a> is what I came up with.  While very much inspired by settingslogic, the key difference is not having to define a class along with your YAML file.  The class you will use to access your key/value pairs is created dynamically at boot time, based on the name of the yml file in your config directory.</p>
<p>Here is how you use Yettings in your own app:</p>
<h4>Install the gem</h4>
<p>Add Yettings to your Gemfile and &#8220;bundle install&#8221;</p>
<div class="rbox">
#Gemfile<br />
gem &#8220;yettings&#8221;
</div>
<div class="rbox">
#command line<br />
> bundle install
</div>
<h4>Create a YAML file: <code>app/config/yetting.yml</code></h4>
<p>This file will define all of your key/value pairs.  Include all relevant key/value pairs in the default section and anything related to a specific environment in the dev, test, production sections.  Note that custom environments will also work without any additional setup.  staging for example.  Any valid YAML will work, and you can also use erb within the YAML.</p>
<p><script src="https://gist.github.com/911019.js?file=yetting.yml"></script></p>
<h4>Access the values anywhere in your Rails app</h4>
<p><script src="https://gist.github.com/911024.js?file=yettings_example.rb"></script></p>
<h4>Creating custom named Yettings</h4>
<p>If you need separate Yettings for different pieces of your app (or if you are using this in a rails plugin), then you can add the yml file to a subdirectory named &#8220;yettings&#8221;.  For instance:</p>
<div class="rbox">
app/config/yettings/main.yml<br />
app/config/yettings/secondary.yml
</div>
<p>Yettings will append the name you specified in camel case to the class that you access the values with:</p>
<p><script src="https://gist.github.com/911026.js?file=yettings_example2.rb"></script></p>
<p>I am using this with several of my rails apps and so far it seems to be working well.  If you have any suggestions or issues, please leave me a comment or open up a github issue in the repo.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/04/08/application-wide-variables-for-your-rails-3-app-using-yettings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Customizing your embedded gists with CSS</title>
		<link>http://www.cowboycoded.com/2011/04/08/customizing-your-embedded-gists-with-css/</link>
		<comments>http://www.cowboycoded.com/2011/04/08/customizing-your-embedded-gists-with-css/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 18:16:50 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[gist]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=859</guid>
		<description><![CDATA[I have always been less than pleased with the way my blog looks, especially relating to the code snippets, but honestly changing the template  hasn&#8217;t been that high on my priority list.  I started writing a new blog today and decided to start using github gists instead of looking for a better syntax [...]]]></description>
			<content:encoded><![CDATA[<p>I have always been less than pleased with the way my blog looks, especially relating to the code snippets, but honestly changing the template  hasn&#8217;t been that high on my priority list.  I started writing a new blog today and decided to start using github gists instead of looking for a better syntax highlighter plugin for wordpress.  The gists always look slick and handle just about every language syntax out there.  But I have been liking the rounded box look more and more recently&#8230; and the gists are plain old rectangle boxes.  So I decided to spruce up the gists a bit with CSS.  When you create a gist on github, they give you a javascript snippet that will embed the gist into your website.  So I pasted that in my blog post, looked at the preview and using Chrome&#8217;s built in developer tools I was able to see that the snippet simply adds elements to the DOM and pops in a CSS file.  It is easy to override the default styles of the gist.  Note that it is important that you use &#8220;!important&#8221; in each CSS attribute, since the gist stylesheet will be loaded after your sites stylesheet, giving it priority over your styles, unless you use the &#8220;!important&#8221;.  Here is what I added to my stylesheet to make it a nice looking rounded box:</p>
<p><script src="https://gist.github.com/910409.js?file=rounded_box_gist.css"></script></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/04/08/customizing-your-embedded-gists-with-css/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why you can&#8217;t reopen Rails 3 Engine classes from the parent app</title>
		<link>http://www.cowboycoded.com/2011/02/28/why-you-cant-reopen-rails-3-engine-classes-from-the-parent-app/</link>
		<comments>http://www.cowboycoded.com/2011/02/28/why-you-cant-reopen-rails-3-engine-classes-from-the-parent-app/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 04:19:17 +0000</pubDate>
		<dc:creator>cowboycoded</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[engines]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://www.cowboycoded.com/?p=821</guid>
		<description><![CDATA[My main frustration with Rails 3 engines is not being able to reopen a class from the parent application.  The first time I used Engines, I tried this:

#authentication stuff in my engine
/my_engine/app/controllers/users_controller.rb
#reopen the users_controller.rb in the parent app and override a method or add a new method
/my_app/app/controllers/users_controller.rb

This can be useful for several reasons:
1. Adding methods [...]]]></description>
			<content:encoded><![CDATA[<p>My main frustration with Rails 3 engines is not being able to reopen a class from the parent application.  The first time I used Engines, I tried this:</p>
<div class="rbox">
#authentication stuff in my engine<br />
/my_engine/app/controllers/users_controller.rb</p>
<p>#reopen the users_controller.rb in the parent app and override a method or add a new method<br />
/my_app/app/controllers/users_controller.rb
</p></div>
<p>This can be useful for several reasons:<br />
1. Adding methods to your engine&#8217;s models, controllers etc from the parent app.<br />
2. Overriding engine methods from the parent app.  In the example above, I wanted to add some behavior to the create method.</p>
<p>Although not everyone will care about the internals of a widely used open source engine, many developers will find this useful when they write their own engines.  You don&#8217;t want to have to copy the source from an engine controller or model over to your parent app, just to add or overwrite a method.</p>
<p>If you try to reopen the class using the method at the beginning of this post, then your engine UsersController class is never loaded.  The app will instead load the users_controller.rb within its own &#8220;/app/controllers&#8221; directory.  I figured their must be a sane reason for this&#8230; and I wanted to get to the root of the problem, so I started digging in the Rails source.  From first glance, it looks like this restriction was possibly unintentional (although I don&#8217;t know enough about the internals to say for sure).  Let&#8217;s see what is happening inside Rails:</p>
<p><script src="https://gist.github.com/910361.js?file=engine.rb"></script></p>
<p>The engine&#8217;s app directory will be in the eager_load_paths and it will hit the &#8220;require_dependency&#8221; line.  &#8220;require_dependency&#8221; is part of ActiveSupport and it will search for the filename by iterating through the autoload paths and finding the first match in that autoload paths list.  Here is the code in ActiveSupport (look at &#8220;search_for_file&#8221;):</p>
<p><script src="https://gist.github.com/910364.js?file=dependencies.rb"></script></p>
<p>debugging the autoload_paths produced the following result (with a few things removed for readability):</p>
<p><script src="https://gist.github.com/910367.js?file=autoload_paths.rb"></script></p>
<p>As you can see from the array above, the app paths show up before the engine paths.  As a result, Rails will always search for the file in the parent app&#8217;s paths before it searches in the engine&#8217;s paths, so *OUCH* it is not possible to re-open the class.  There are probably ways this can be done.. like maybe requiring the files explicitly in an initializer in the engine, but I would rather not resort to such hacks.  My temporary fix is to use class_eval in the parent app, but this is still really ugly.  Anyways.. its getting late and I am done for tonight.  I guess I will keep poking around to see if there is a suitable workaround.</p>
<p><strong>UPDATE</strong></p>
<p>After looking around SO, I found a patch for ActiveSupport::Dependencies</p>
<p><script src="https://gist.github.com/910368.js?file=as_dependencies_patch.rb"></script></script></p>
<p>With this patch, the require_or_load method is aliased and the engine classes are loaded before the original method is called.  Gave it a quick test and so far so good.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowboycoded.com/2011/02/28/why-you-cant-reopen-rails-3-engine-classes-from-the-parent-app/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
