<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Coding Daily</title>
	<atom:link href="http://codingdaily.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingdaily.wordpress.com</link>
	<description>Answers to my own coding problems</description>
	<lastBuildDate>Fri, 03 May 2013 02:22:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codingdaily.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Coding Daily</title>
		<link>http://codingdaily.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codingdaily.wordpress.com/osd.xml" title="Coding Daily" />
	<atom:link rel='hub' href='http://codingdaily.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Monitoring Rails with statsd and graphite</title>
		<link>http://codingdaily.wordpress.com/2013/05/02/monitoring-rails-with-statsd-and-graphite/</link>
		<comments>http://codingdaily.wordpress.com/2013/05/02/monitoring-rails-with-statsd-and-graphite/#comments</comments>
		<pubDate>Fri, 03 May 2013 02:22:34 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[graphite]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[statsd]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=112</guid>
		<description><![CDATA[Assuming you&#8217;ve googled this topic at all, you are bound to have come across this article from 37signals, pssst-your-rails-application-has-a-secret-to-tell-you. You can basically build an initializer doing all the things in that post to get started sending data to statsd. Statsd &#8230; <a href="http://codingdaily.wordpress.com/2013/05/02/monitoring-rails-with-statsd-and-graphite/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=112&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Assuming you&#8217;ve googled this topic at all, you are bound to have come across this article from 37signals, <a href="http://37signals.com/svn/posts/3091-pssst-your-rails-application-has-a-secret-to-tell-you" target="_blank">pssst-your-rails-application-has-a-secret-to-tell-you</a>. You can basically build an initializer doing all the things in that post to get started sending data to <a href="https://github.com/etsy/statsd/" target="_blank">statsd</a>.</p>
<p>Statsd is a fairly small node.js application that listens for UDP requests on a configured port and collects them for a short period of time before then sending them over TCP to a <a href="http://graphite.wikidot.com/" target="_blank">graphite</a> server. The upshot is if your statsd daemon goes down, your application continues to operate, thanks to UDP&#8217;s fire and forget nature.</p>
<p>If you want to save a little time, try checking out <a href="https://github.com/jnunemaker/nunes" target="_blank">Nunes</a>, a gem to automatically cause Rail&#8217;s builtin instrumentation to send data to statsd, see <a href="https://github.com/jnunemaker/nunes" target="_blank">https://github.com/jnunemaker/nunes</a>.</p>
<p>Graphite is an interesting beast worthy of some time reading the documentation to become familiar with the whisper database structure and to learn how you might tweak the configuration. A busy graphite server can be as taxing as a database server, making use of SSD drives, etc. Graphite is horizontally scalable, but documentation on scaling graphite in this manner is particularly lacking. <a href="http://rcrowley.org/articles/federated-graphite.html" target="_blank">This post by Richard Crowley</a> on federated graphite is all I&#8217;ve found at the time of this writing.</p>
<p>Now go and instrument all the things.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=112&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2013/05/02/monitoring-rails-with-statsd-and-graphite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby connection pooling made easy</title>
		<link>http://codingdaily.wordpress.com/2012/11/03/ruby-connection-pooling-made-easy/</link>
		<comments>http://codingdaily.wordpress.com/2012/11/03/ruby-connection-pooling-made-easy/#comments</comments>
		<pubDate>Sun, 04 Nov 2012 03:25:45 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=106</guid>
		<description><![CDATA[If you ever want to have a connection pool, you need not reinvent the wheel because Mike Perham has done all the work for you already. Browse on over to connection_pool on github.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=106&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you ever want to have a connection pool, you need not reinvent the wheel because Mike Perham has done all the work for you already.  Browse on over to <a href="https://github.com/mperham/connection_pool">connection_pool</a> on github.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/106/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=106&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2012/11/03/ruby-connection-pooling-made-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Write your own custom RSpec formatter</title>
		<link>http://codingdaily.wordpress.com/2012/11/03/write-your-own-custom-rspec-formatter/</link>
		<comments>http://codingdaily.wordpress.com/2012/11/03/write-your-own-custom-rspec-formatter/#comments</comments>
		<pubDate>Sun, 04 Nov 2012 03:10:27 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=104</guid>
		<description><![CDATA[Earlier this year I spent some time writing a custom RSpec formatter. The purpose in my case was to be able to automatically update a remote service with the results of testing some code. You can find that project here, &#8230; <a href="http://codingdaily.wordpress.com/2012/11/03/write-your-own-custom-rspec-formatter/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=104&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Earlier this year I spent some time writing a custom RSpec formatter.  The purpose in my case was to be able to automatically update a remote service with the results of testing some code.  You can find that project here, <a href="https://github.com/bithckr/Jaribio/tree/master/jaribio_formatter" target="_blank">jaribio_formatter</a>.</p>
<p>The code for a basic no-op formatter is below.  If you want to output a message when a group is started or finished, or an example starts or ends, or simply record failures to summarize at the end, all the hooks are well defined by RSpec though not well documented.</p>
<pre class="brush: ruby; title: ; notranslate">
class MyRSpecFormatter

    attr_reader :output, :results, :example_group

    def initialize(output)
      @output = output || StringIO.new
      @results = {}
    end

    def start(example_count)
    end

    def example_group_started(example_group)
    end

    def example_group_finished(example_group)
    end

    def example_started(example)
    end

    def example_passed(example)
    end

    def example_pending(example)
    end

    def example_failed(example)
    end

    def message(message)
    end

    def stop
    end

    def start_dump()
    end

    def dump_pending()
    end

    def dump_failures()
    end

    def dump_summary(duration, example_count, failure_count, pending_count)
    end

    # no-op
    def seed(seed)
    end

    def close()
    end

end
</pre>
<p>You can also add your own custom configuration to RSpec.  To do that, you need to call <code>add_setting</code>.  An example from jaribio_formatter below.  One more line of code calls this method when the formatter is required.  The relevant source code to the jaribio_formatter <a href="https://github.com/bithckr/Jaribio/blob/master/jaribio_formatter/lib/jaribio/rspec_formatter.rb" target="_blank">is here if needed</a> for reference.</p>
<pre class="brush: ruby; title: ; notranslate">
    def self.configure()
      RSpec.configure do |c|
        c.add_setting :jaribio_url
        c.add_setting :jaribio_api_key
        c.add_setting :jaribio_plans, :default =&gt; []
        c.add_setting :jaribio_auto_create, :default =&gt; false
        c.add_setting :jaribio_timeout, :default =&gt; 5
      end
    end
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=104&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2012/11/03/write-your-own-custom-rspec-formatter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby&#8217;s Singleton and Custom Rails Application Configuration</title>
		<link>http://codingdaily.wordpress.com/2012/03/14/rubys-singleton-and-custom-rails-application-configuration/</link>
		<comments>http://codingdaily.wordpress.com/2012/03/14/rubys-singleton-and-custom-rails-application-configuration/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 01:55:42 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=97</guid>
		<description><![CDATA[A relatively simple way to load some configuration once and safely, is through the use of the Singleton module provided with Ruby. Accessing the instance is easy, call FooConfig.instance. This method is also thread-safe so no two threads are going &#8230; <a href="http://codingdaily.wordpress.com/2012/03/14/rubys-singleton-and-custom-rails-application-configuration/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=97&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A relatively simple way to load some configuration once and safely, is through the use of the Singleton module provided with Ruby.</p>
<pre class="brush: ruby; title: ; notranslate">
class FooConfig
  include Singleton  
  attr_reader :foo

  def initialize()
    @foo = true
  end
end
</pre>
<p>Accessing the instance is easy, call <code>FooConfig.instance</code>.  This method is also thread-safe so no two threads are going to create/access a different instance.  The example is trite, but imagine instead loading data from a remote service.  Now combine this with Mike Perham&#8217;s <a href="http://blog.carbonfive.com/2011/11/23/configuration-for-rails-the-right-way/">Configuration for Rails, the Right Way</a>.</p>
<pre class="brush: ruby; title: ; notranslate">
MyApp::Application.config.foo = FooConfig.instance
</pre>
<p>There is really little reason to access the configuration via Rails.application.config.foo, except that this way you can change it for things like tests more easily.</p>
<p>Speaking of testing, if you find you need to reset a singleton for some reason, the following seems to do the trick, which comes from <a href="http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby">http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby</a>.  Just paste into a file like spec/support/singleton_reset.rb (if using rspec) and require it in spec/spec_helper.rb</p>
<pre class="brush: ruby; title: ; notranslate">
require 'singleton'

class &lt;&lt;Singleton
  def included_with_reset(klass)
    included_without_reset(klass)
    class &lt;&lt;klass
      def reset_instance
        Singleton.send :__init__, self
        self
      end
    end
  end
  alias_method :included_without_reset, :included
  alias_method :included, :included_with_reset
end
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=97&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2012/03/14/rubys-singleton-and-custom-rails-application-configuration/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Require a Gemfile from a Gemfile</title>
		<link>http://codingdaily.wordpress.com/2012/02/27/require-a-gemfile-from-a-gemfile/</link>
		<comments>http://codingdaily.wordpress.com/2012/02/27/require-a-gemfile-from-a-gemfile/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 00:56:50 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bundler]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=96</guid>
		<description><![CDATA[It is probably best I don&#8217;t bother explaining how or why I ended up wanting to reference an existing Gemfile from another Gemfile, but suffice to say it is something I sort of needed to do. After a bit of &#8230; <a href="http://codingdaily.wordpress.com/2012/02/27/require-a-gemfile-from-a-gemfile/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=96&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>It is probably best I don&#8217;t bother explaining how or why I ended up wanting to reference an existing Gemfile from another Gemfile, but suffice to say it is something I sort of needed to do. After a bit of searching I came across the following post, <a href="http://madebynathan.com/2010/10/19/how-to-use-bundler-with-plugins-extensions/" target="_blank">http://madebynathan.com/2010/10/19/how-to-use-bundler-with-plugins-extensions/</a>.</p>
<p>The most relevant bit is the use of eval like below in the Gemfile you want to include another Gemfile in.</p>
<pre class="brush: ruby; title: ; notranslate">
filename=&quot;...&quot;
eval(IO.read(filename), binding)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/96/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=96&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2012/02/27/require-a-gemfile-from-a-gemfile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby segfault with net/http and openssl</title>
		<link>http://codingdaily.wordpress.com/2011/12/13/ruby-segfault-with-openssl/</link>
		<comments>http://codingdaily.wordpress.com/2011/12/13/ruby-segfault-with-openssl/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 02:46:23 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby openssl]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=90</guid>
		<description><![CDATA[I was attempting to push a new gem this evening and ruby 1.9.3 segfaulted. The dump indicated openssl and net/http as the issue. Pushing gem to https://rubygems.org... /Users/bjones/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:799: [BUG] Segmentation fault After a bit of googling I found a nice &#8230; <a href="http://codingdaily.wordpress.com/2011/12/13/ruby-segfault-with-openssl/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=90&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was attempting to push a new gem this evening and ruby 1.9.3 segfaulted.  The dump indicated openssl and net/http as the issue.  </p>
<p><code>Pushing gem to <a href="https://rubygems.org" rel="nofollow">https://rubygems.org</a>...<br />
/Users/bjones/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:799: [BUG] Segmentation fault</code></p>
<p>After a bit of googling I found a nice post describing how to resolve the issue.</p>
<p><a href="http://www.christopherirish.com/2011/09/02/ruby-1-9-2-segmentation-fault-and-openssl/">http://www.christopherirish.com/2011/09/02/ruby-1-9-2-segmentation-fault-and-openssl/</a></p>
<p>The key part for me was adding &#8211;with-openssl-dir=/opt/local to the end of the command when running `rvm install ruby-1.9.3`.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=90&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2011/12/13/ruby-segfault-with-openssl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Updated safe_attributes now works with devise</title>
		<link>http://codingdaily.wordpress.com/2011/11/21/updated-safe_attributes-now-works-with-devise/</link>
		<comments>http://codingdaily.wordpress.com/2011/11/21/updated-safe_attributes-now-works-with-devise/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 16:56:49 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[devise]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[safe_attributes]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=71</guid>
		<description><![CDATA[After getting someone interested in using devise with safe_attributes to post a little information on the issue to github, I investigated and found something new. The model validators in ActiveRecord can be used on things other than attributes of the &#8230; <a href="http://codingdaily.wordpress.com/2011/11/21/updated-safe_attributes-now-works-with-devise/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=71&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>After getting someone interested in using devise with safe_attributes to post a little information on the issue to github, I investigated and found something new.  The model validators in ActiveRecord can be used on things other than attributes of the model.  Devise uses them to check things about the password before it is eventually encrypted and stored in the database in that form only.  So this required a small update to safe_attributes and should now be resolved.  </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=71&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2011/11/21/updated-safe_attributes-now-works-with-devise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails + Kaminari with RSpec</title>
		<link>http://codingdaily.wordpress.com/2011/09/16/rails-kaminari-with-rspec/</link>
		<comments>http://codingdaily.wordpress.com/2011/09/16/rails-kaminari-with-rspec/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 01:11:31 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[kaminari]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=69</guid>
		<description><![CDATA[The default rspec view specs don&#8217;t work so well for index pages if you have added Kaminari pagination. The reason is the assigned variable in the controller is not an array, but is instead expected to respond to additional methods: &#8230; <a href="http://codingdaily.wordpress.com/2011/09/16/rails-kaminari-with-rspec/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=69&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The default rspec view specs don&#8217;t work so well for index pages if you have added Kaminari pagination.</p>
<p>The reason is the assigned variable in the controller is not an array, but is instead expected to respond to additional methods: current_page, num_pages, and limit_value.</p>
<p>Modifying the view spec like below appears to address the issue.</p>
<pre class="brush: ruby; title: ; notranslate">
@books = [
  Factory.build(:book),
  Factory.build(:book)
]
@books.stub!(:current_page).and_return(1)
@books.stub!(:num_pages).and_return(1)
@books.stub!(:limit_value).and_return(1)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=69&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2011/09/16/rails-kaminari-with-rspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails + Devise and Rspec</title>
		<link>http://codingdaily.wordpress.com/2011/09/13/rails-devise-and-rspec/</link>
		<comments>http://codingdaily.wordpress.com/2011/09/13/rails-devise-and-rspec/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 01:49:28 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[devise]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=64</guid>
		<description><![CDATA[So I tried using Devise on a project and ran into an issue with the default generated controller specs. PlansController POST create with invalid params re-renders the 'new' template Failure/Error: response.should render_template("new") expecting &#60;"new"&#62; but rendering with &#60;""&#62; # ./plans_controller_spec.rb:73:in &#8230; <a href="http://codingdaily.wordpress.com/2011/09/13/rails-devise-and-rspec/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=64&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>So I tried using Devise on a project and ran into an issue with the default generated controller specs.</p>
<p><code>PlansController POST create with invalid params re-renders the 'new' template<br />
     Failure/Error: response.should render_template("new")<br />
       expecting &lt;"new"&gt; but rendering with &lt;""&gt;<br />
     # ./plans_controller_spec.rb:73:in `block (4 levels) in &lt;top (required)&gt;'</p>
<p>PlansController PUT update with invalid params re-renders the 'edit' template<br />
     Failure/Error: response.should render_template("edit")<br />
       expecting &lt;"edit"&gt; but rendering with &lt;""&gt;<br />
     # ./plans_controller_spec.rb:117:in `block (4 levels) in &lt;top (required)&gt;'<br />
</code></p>
<p>The problem is that my application is requiring a valid user here, so the controller is redirecting rather than rendering what is expected.</p>
<p>If you look you&#8217;ll find helpful advise on the Devise wiki about how to resolve this issue for your controller specs.</p>
<ul>
<li><a href="https://github.com/plataformatec/devise" title="https://github.com/plataformatec/devise" target="_blank">https://github.com/plataformatec/devise</a> (see &#8220;Test helpers&#8221;)</li>
<li><a href="https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29" title="https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29" target="_blank">https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29</a></li>
</ul>
<p>I&#8217;m using FactoryGirl, so this is the solution I went with:</p>
<p><code>$ cat spec/support/controller_macros.rb</code></p>
<pre class="brush: ruby; title: ; notranslate">
module ControllerMacros
  def login_user
    before(:each) do
      @request.env[&quot;devise.mapping&quot;] = Devise.mappings[:user]
      user = Factory.create(:user)
      # or set a confirmed_at inside the factory. Only necessary if you
      # are using the confirmable module
      user.confirm!
      sign_in user
    end
  end
end
</pre>
<p>In spec/spec_helper.rb:</p>
<pre class="brush: ruby; title: ; notranslate">
RSpec.configure do |config|
...
  config.include Devise::TestHelpers, :type =&gt; :controller
  config.extend ControllerMacros, :type =&gt; :controller
end
</pre>
<p>Now what about those integration tests, so-called request specs in rspec parlance. It turns out you need a means of logging in for those specs too.</p>
<p><code>1) Plans GET /plans works! (now write some real specs)<br />
Failure/Error: response.status.should be(200)</p>
<p>expected #&lt;Fixnum:401&gt; =&gt; 200<br />
got #&lt;Fixnum:605&gt; =&gt; 302</code></p>
<p>After quite a bit of googling I came across the following which seems to work great.</p>
<p><code><br />
$ cat spec/support/request_macros.rb</code></p>
<pre class="brush: ruby; title: ; notranslate">
module RequestMacros
  def login(user)
    page.driver.post user_session_path, 
      :user =&gt; {:email =&gt; user.email, :password =&gt; user.password}
  end
end
</pre>
<p>In spec/spec_helper.rb:</p>
<pre class="brush: ruby; title: ; notranslate">
RSpec.configure do |config|
...
  config.include RequestMacros, :type =&gt; :request
end
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=64&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2011/09/13/rails-devise-and-rspec/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
		<item>
		<title>Test a gem with the Rails 3 stack</title>
		<link>http://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/</link>
		<comments>http://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 04:31:41 +0000</pubDate>
		<dc:creator>Brian Jones</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://codingdaily.wordpress.com/?p=63</guid>
		<description><![CDATA[My previous post covers testing a gem that makes some change to how ActiveRecord works. But what if you want to test a gem that supplies some new behavior to another part of rails, like say ActionController. How do you &#8230; <a href="http://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=63&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My previous post covers testing a gem that makes some change to how ActiveRecord works.  But what if you want to test a gem that supplies some new behavior to another part of rails, like say ActionController.  How do you properly setup that environment without including an entire, mostly blank, rails application into your gem&#8217;s sources?</p>
<p>The answer is to essentially do what would normally be done if you were testing inside of a Rails application, by looking at what files get required by your testing framework.  I use rspec, and here is my spec_helper.rb.</p>
<pre class="brush: ruby; title: ; notranslate">
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

ENV[&quot;RAILS_ENV&quot;] ||= 'test'

require 'rubygems'
gem 'actionpack', '&gt;= 3.0.0'
gem 'activesupport', '&gt;= 3.0.0'
gem 'activemodel', '&gt;= 3.0.0'
gem 'railties', '&gt;= 3.0.0'

# Only the parts of rails we want to use
# if you want everything, use &quot;rails/all&quot;
require &quot;action_controller/railtie&quot;
require &quot;rails/test_unit/railtie&quot;

root = File.expand_path(File.dirname(__FILE__))

# Define the application and configuration
module Config
  class Application &lt; ::Rails::Application
    # configuration here if needed
    config.active_support.deprecation = :stderr 
  end
end

# Initialize the application
Config::Application.initialize!

require 'rspec/rails'

RSpec.configure do |config|
end

require 'my_gem'
</pre>
<p>Due to Rails 3 modularity, we can now include only the parts of it we need for our tests.  So instead of requiring &#8220;rails/all&#8221; you can see me bringing in only action_controller and some test unit helpers.</p>
<p>I banged my head against another problem, which is not visible from the code here.  Rails looks for its root directory by looking for a specific file starting with the directory of the file in which you&#8217;ve done this configuration.  I&#8217;ve seen evidence that some code looks for script/rails and other code looks for config.ru.  It isn&#8217;t necessary for either of these files to have anything in them, but simply to exist as you&#8217;d expect in a rails application.  I put them in my &#8216;spec&#8217; directory.</p>
<ul>
<li>spec/config.ru</li>
<li>spec/script/rails</li>
</ul>
<p>I&#8217;m testing a gem that does something with controllers, so I also setup spec/config/routes.rb.  All of my tests use the same two controllers, so I set those up in spec/app/controllers/.</p>
<p>After all this setup, spec tests work as usual in a Rails application for spec/controllers, spec/routing, etc.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codingdaily.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codingdaily.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codingdaily.wordpress.com&#038;blog=13888009&#038;post=63&#038;subd=codingdaily&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/eba99fe27fd8fab37e5d81c6dba43999?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cbjones1</media:title>
		</media:content>
	</item>
	</channel>
</rss>
