<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title>stderr.timfischbach.de</title>
 <link href="http://stderr.timfischbach.de/atom.xml" rel="self"/>
 <link href="http://stderr.timfischbach.de/"/>
 <updated>2015-01-02T11:34:50+00:00</updated>
 <id>http://stderr.timfischbach.de/</id>

 <author>
   <name>Tim Fischbach</name>
   <email>mail@timfischbach.de</email>
 </author>

 
   <entry>
     <title>Nested Helper Classes vs Private Helper Methods</title>
     <link href="http://stderr.timfischbach.de/2014/12/26/nested-helper-classes.html"/>
     <updated>2014-12-26T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2014/12/26/nested-helper-classes</id>
     <content type="html">&lt;p&gt;While criticism towards Rails helpers has almost become folklore
(procedural, global scope, junk drawers), I still find myself reaching
for them as a low ceremony way of making templates more concise and
building simple view layer APIs.&lt;/p&gt;

&lt;p&gt;Obviously, the greatest downside of helper modules is how dangerous
extracting private methods becomes.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;module UsersHelper
  def user_section(user)
    content_tag(:section, user.name, class: css_class(user))
  end

  private

  def css_class(user)
    [&amp;#39;user&amp;#39;, user.role].join(&amp;#39; &amp;#39;)
  end
end

module PostsHelper
  def post_section
    content_tag(:section, post.text, class: css_class(post))
  end

  private

  # collides with UsersHelper#css_class
  def css_class(post)
    &amp;#39;post&amp;#39;
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While I always liked extracting classes to keep helper methods short,
only recently did I realize how a nuance in Ruby&amp;#39;s constant look-up
mechanism helps prevent namespace collisions as above.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s look at a variant of the above helpers that uses nested classes
instead of private methods.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;module UsersHelper
  def user_section(user)
    Section.new(user).render(self)
  end

  class Section &amp;lt; Struct.new(:user)
    def render(template)
      template.content_tag(:section, user.name, class: css_class)
    end

    private

    def css_class
      [&amp;#39;user&amp;#39;, user.role].join(&amp;#39; &amp;#39;)
    end
  end
end

module PostsHelper
  def post_section(post)
    Section.new(post).render(self)
  end

  class Section &amp;lt; Struct.new(:post)
    def render(template)
      template.content_tag(:section, post.text, class: css_class)
    end

    private

    def css_class
      &amp;#39;post&amp;#39;
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At first glance, one could expect the same problem as above. Instead
of both defining an equally named private method, both modules now
contain a nested class with the same name. Once both modules get
included into the view object only one can win, right? That&amp;#39;s sure the
way it looks when we try to reference one of the nested classes from a
template:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;  # app/views/users/index.html.erb
  &amp;lt;%= Section == PostsHelper::Section # =&amp;gt; true %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But in contrast to method invocations, which are resolved by the
object handling the call, constant look-up uses the lexical scope at
the point where the constant is referenced. So referring to a
&lt;code&gt;Section&lt;/code&gt; class inside &lt;code&gt;UsersHelper&lt;/code&gt; still resolves to
&lt;code&gt;UsersHelper::Section&lt;/code&gt;, no matter whether there is a colliding
&lt;code&gt;PostsHelper::Section&lt;/code&gt; class when including helper modules into the
view context.&lt;/p&gt;

&lt;p&gt;Nested helper classes thus provide a safe space for refactoring
towards small methods. Helper methods end up containing only wiring
code, keeping implementation details from leaking into tests or
templates.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>YARD Documentation with Emacs</title>
     <link href="http://stderr.timfischbach.de/2014/10/12/emacs-fill-paragraph-for-yard.html"/>
     <updated>2014-10-12T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2014/10/12/emacs-fill-paragraph-for-yard</id>
     <content type="html">&lt;p&gt;In Emacs, the
&lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/emacs/Fill-Commands.html&quot;&gt;&lt;code&gt;fill-paragraph&lt;/code&gt; command&lt;/a&gt;
lets you reformat a chunk of text to break lines at a certain
width. Bound to &lt;code&gt;M-q&lt;/code&gt; by default, it is a handy way to enforce
consistent line widths inside text documents.&lt;/p&gt;

&lt;p&gt;Recently, I&amp;#39;ve been editing a lot of inline documentation in Ruby
code using YARD. A typical doc comment looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Reverses the contents of a String or IO object. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# @param [String, #read] contents the contents to reverse &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# @return [String] the contents reversed lexically &lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
  &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As descriptions got longer, I found myself wishing to reformat
paragraphs. While Emacs knows how to fill paragraphs inside Ruby
comments, hitting &lt;code&gt;M-q&lt;/code&gt; still turned text into unreadable gibberish:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Reverses the contents of a String or IO object.  @param [String,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# #read] contents the contents to reverse @return [String] the&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# contents reversed lexically&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Turns out there is an easy way to tell Emacs how to recognize
paragraphs: &lt;code&gt;paragraph-separate&lt;/code&gt; and &lt;code&gt;paragraph-start&lt;/code&gt;.  Let&amp;#39;s define
a function that sets these variables to YARD specific values. The
&lt;code&gt;concatenate&lt;/code&gt; call is only there to improve readability.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;yard-paragraph-boundaries&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;interactive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;; Paragraphs are separated by lines containing only a # character&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;setq&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;paragraph-separate&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;[ \t]*#[ \t]*$&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;; Paragraphs start with YARD tags or list items&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;setq&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;paragraph-start&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;concatenate&lt;/span&gt;
       &lt;span class=&quot;ss&quot;&gt;&amp;#39;string&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;^[ \t]*&amp;quot;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; some whitespace&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;#[ \t]*&amp;quot;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; a # character followed by whitespace&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;\\(&amp;quot;&lt;/span&gt;
         &lt;span class=&quot;s&quot;&gt;&amp;quot;@[[:alpha:]]+&amp;quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; a YARD tag&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;\\|&amp;quot;&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; or&lt;/span&gt;
         &lt;span class=&quot;s&quot;&gt;&amp;quot;-&amp;quot;&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; a list item&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;\\)&amp;quot;&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;\\([ \t]+.*\\)?&amp;quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; an optional text&lt;/span&gt;
       &lt;span class=&quot;s&quot;&gt;&amp;quot;[ \t]*$&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; some more whitespace&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, we can call the function whenever &lt;code&gt;ruby-mode&lt;/code&gt; is entered:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;add-hook&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;&amp;#39;ruby-mode-hook&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;yard-paragraph-boundaries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now text inside YARD comments can be reformatted with a single key
stroke. Emacs even preserves custom indentation levels:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# @param [String] A really long description text which spans&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#   multiple lines and has a custom indentation, which is&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#   preserved even when the paragraph is reformatted.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
   </entry>
 
   <entry>
     <title>Resetting Has One Assocations</title>
     <link href="http://stderr.timfischbach.de/2014/09/08/resetting-has-one-associations.html"/>
     <updated>2014-09-08T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2014/09/08/resetting-has-one-associations</id>
     <content type="html">&lt;p&gt;&lt;em&gt;Tested with Rails 4.0&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Just a quick note since I could not find this information
anywhere. Making sure a &lt;code&gt;has_many&lt;/code&gt; association is reloaded on next
access is easy. Just call the
&lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-reset&quot;&gt;&lt;code&gt;reset&lt;/code&gt; method&lt;/a&gt;
provided by the collection proxy:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class Post
  has_many :comments
end

post.comments.reset
post.comments # fetches comments from database
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But how to accomplish the same for &lt;code&gt;has_one&lt;/code&gt; associations?&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class Post
  has_one :last_comment, -&amp;gt; { order &amp;#39;created_at&amp;#39; }, class_name: &amp;quot;Comment&amp;quot;
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Calling &lt;code&gt;post.last_comment&lt;/code&gt; just returns a comment (or &lt;code&gt;nil&lt;/code&gt;). Of
course, you can pass &lt;code&gt;true&lt;/code&gt; as the &lt;code&gt;force_reload&lt;/code&gt; parameter:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;post.last_comment(true) # fetches comment from database
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But what if there is no need to reload the association now, only next
time it is used?  Turns out the underlying association object can
be accessed via the &lt;code&gt;associations&lt;/code&gt; method:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;post.associations(:last_comment).reset
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now the cache is cleared and the next call to &lt;code&gt;post.last_comment&lt;/code&gt; will
fetch the record from the database.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Introducing Env Lint</title>
     <link href="http://stderr.timfischbach.de/2014/02/20/environment-liniting-for-12-factor-apps.html"/>
     <updated>2014-02-20T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2014/02/20/environment-liniting-for-12-factor-apps</id>
     <content type="html">&lt;p&gt;We&amp;#39;ve been following the
&lt;a href=&quot;http://12factor.net/config&quot;&gt;12 factor approach&lt;/a&gt; of configuring our
Rails apps through environment variables.  Tweaking options through
the &lt;code&gt;env:set&lt;/code&gt; task provided by
&lt;a href=&quot;https://github.com/tomafro/recap&quot;&gt;recap&lt;/a&gt; feels much more light weight
than manually fiddling with &lt;code&gt;yml&lt;/code&gt; files on the server.  Moreover, the
&lt;a href=&quot;https://github.com/bkeepers/dotenv&quot;&gt;dotenv&lt;/a&gt; gem makes controlling
your development environment very comfortable. While the &lt;code&gt;.env&lt;/code&gt; file
itself is not checked into version control, there is a &lt;code&gt;.env.example&lt;/code&gt;
file which acts as a starting point for new developers and documents
the available options.&lt;/p&gt;

&lt;p&gt;Still, there were some pain points left:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Whenever new variables were added to the app&amp;#39;s configuration, the
process of updating the staging and production environments turned
out to be rather error prone. Even having carefully compared the
lists of defined variables, only after booting the newly deployed
app could one be sure that nothing was missing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This was even harder as small spelling errors in variable names
either in the code or on the command line were easy to miss.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, the &lt;code&gt;.env.example&lt;/code&gt; file would quickly become outdated,
incomplete or simply incorrect.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To resolve all of these issues we came up with the
&lt;a href=&quot;https://github.com/tf/env_lint&quot;&gt;env_lint gem&lt;/a&gt;, which is comprised of
three components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A wrapper around &lt;code&gt;ENV&lt;/code&gt; called &lt;code&gt;LintedEnv&lt;/code&gt; which fails loudly
whenever variables are not defined or misspelled variable names are
used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A capistrano task to verify all required environment variables are
defined on the production machine --- even before deploying a new
revision of the app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A rake task which helps developers make sure that their local
environment is in shape.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For all of this the application&amp;#39;s &lt;code&gt;.env.example&lt;/code&gt; file becomes the
primary source of truth. Only variables explained there can be used in
the code or passed to tasks like &lt;code&gt;env:set&lt;/code&gt;. The gem also parses the
description comments to generate helpful error messages.  A typical
&lt;code&gt;.env.example&lt;/code&gt; file looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# Host name used in absolute urls 
HOST_NAME=my.app.com

# Redirect users to the https site
# ENFORCE_SSL=true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Commented out assignments signify optional variables. &lt;code&gt;LintedEnv&lt;/code&gt;
makes sure you always supply a default value when fetching one of
these.  &lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;linted_env.fetch(:enforce_ssl)
# =&amp;gt; raises an error telling you to supply a default
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All in all, it&amp;#39;s almost impossible for your example file to rot. And
when it&amp;#39;s time to deploy, a simple&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;$ cap production env:lint
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;makes sure your app won&amp;#39;t crash after restart only because an
environment variable is missing.&lt;/p&gt;

&lt;p&gt;More details can be found in the
&lt;a href=&quot;https://github.com/tf/env_lint&quot;&gt;README&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, the
&lt;a href=&quot;/2013/09/05/generating-ignored-config-files-for-ci.html&quot;&gt;ci-config-generator&lt;/a&gt;
is nice addition to this team, letting you define a &lt;code&gt;.env.ci&lt;/code&gt; file
which contains a concrete, version controlled configuration which is
verified in your continuous integration runs.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Chef Solo Provisioning with Capistrano Roundsman and Berkshelf</title>
     <link href="http://stderr.timfischbach.de/2013/11/29/chef-solo-provisioning-with-capistrano-roundsman-and-berkshelf.html"/>
     <updated>2013-11-29T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/11/29/chef-solo-provisioning-with-capistrano-roundsman-and-berkshelf</id>
     <content type="html">&lt;p&gt;&lt;a href=&quot;http://www.opscode.com/chef/&quot;&gt;Chef&lt;/a&gt; helps make server provisioning
reproducible and self documenting. Even more, a lot of the benefits
can be gained without maintaining the infrastructure surrounding a
chef server. Especially if you do not have an ops team carefully
curating a set of cookbooks representing your server landscape, a more
application-centric approach might work for you.&lt;/p&gt;

&lt;p&gt;We tend to deploy our applications to
&lt;a href=&quot;http://martinfowler.com/bliki/PhoenixServer.html&quot;&gt;phoenix servers&lt;/a&gt;
which run as &lt;a href=&quot;http://linuxcontainers.org/&quot;&gt;Linux Containers (lxc)&lt;/a&gt;
using &lt;a href=&quot;http://libvirt.org/&quot;&gt;libvirt&lt;/a&gt;. Each application contains an
application cookbook describing the required deployment environment.
Here is an excerpt from the typical directory layout of our projects.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;some_app/
  Berksfile
  Berksfile.lock
  Capfile
  deploy/
    tasks
      provision.rb
    cookbooks/
      main/
        attributes/
        recipes/
          default.rb
          nginx.rb
          mysql.rb
        templates/
        metadata.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;#39;s take a looks at the different ingredients one by one.&lt;/p&gt;

&lt;h3&gt;The Application Cookbook&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;cookbooks&lt;/code&gt; directory normally only contains a single cookbook.
It provides recipes to setup everything required to run the app: ruby
versions, databases, web servers. The recipes mostly contain
&lt;code&gt;include_recipe&lt;/code&gt; directives and some lightweight resources. Here is an
excerpt from the mysql recipe:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# deploy/cookbooks/main/recipes/mysql.rb
include_recipe &amp;#39;mysql::server&amp;#39;
include_recipe &amp;#39;mysql::client&amp;#39;
include_recipe &amp;#39;mysql::ruby&amp;#39;

mysql_connection_info = { ... }

mysql_database node[:application] do
  connection mysql_connection_info
  action :create
end

mysql_database_user node[:application] do
  connection mysql_connection_info
  password   node[:database_password]
  action     :grant
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Dependent cookbooks can simply be listed in the cookbook&amp;#39;s
&lt;code&gt;metadata.rb&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# deploy/cookbooks/main/metadata.rb
name              &amp;quot;main&amp;quot;
maintainer        &amp;quot;Codevise Solution&amp;quot;
maintainer_email  &amp;quot;me@example.com&amp;quot;
description       &amp;quot;&amp;quot;
long_description  &amp;quot;&amp;quot;
version           &amp;quot;0.0.0&amp;quot;

depends &amp;quot;rvm&amp;quot;
depends &amp;quot;mysql&amp;quot;, &amp;quot;3.0.12&amp;quot;
depends &amp;quot;database&amp;quot;
depends &amp;quot;nginx&amp;quot;

supports &amp;quot;ubuntu&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Sometimes other custom cookbooks shall be bundled in the application
repository. We then place them next to &lt;code&gt;main&lt;/code&gt; in the &lt;code&gt;cookbooks&lt;/code&gt;
directory. Most of the time though it&amp;#39;s easier to automatically
resolve dependencies.&lt;/p&gt;

&lt;h3&gt;Dependency Resolution&lt;/h3&gt;

&lt;p&gt;Just like bundler resolves gem dependencies,
&lt;a href=&quot;http://berkshelf.com/&quot;&gt;Berkshelf&lt;/a&gt; can be used to install required
cookbooks. In the &lt;code&gt;Berksfile&lt;/code&gt; alternative sources to fetch cookbooks
from can be specified.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# Berksfile
site :opscode

# Application cookbook
cookbook &amp;#39;main&amp;#39;, :path =&amp;gt; &amp;#39;deploy/cookbooks/main&amp;#39;

# Alternative sources
cookbook &amp;#39;rvm&amp;#39;, :git =&amp;gt; &amp;#39;https://github.com/fnichol/chef-rvm&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Once you run &lt;code&gt;berks install&lt;/code&gt;, all required cookbooks are downloaded
into a shared directory and a &lt;code&gt;Berksfile.lock&lt;/code&gt; is placed in the
project root. Pretty familiar.&lt;/p&gt;

&lt;p&gt;One could just as well use
&lt;a href=&quot;https://github.com/applicationsonline/librarian-chef&quot;&gt;Librarian Chef&lt;/a&gt;
to manage dependencies. But since as of recently Berkshelf appears to
be
&lt;a href=&quot;http://www.opscode.com/blog/2013/11/12/opscode-to-steward-berkshelf/&quot;&gt;backed by Opscode&lt;/a&gt;,
I figure it is a good choice.&lt;/p&gt;

&lt;h3&gt;Controlling Chef Solo&lt;/h3&gt;

&lt;p&gt;Finally we need a tool to actually run the recipes on the app&amp;#39;s
deployment server. This is where the
&lt;a href=&quot;https://github.com/iain/roundsman&quot;&gt;Capistrano Roundsman&lt;/a&gt; gem comes
in. It bootstraps the server with a version of ruby and the chef gem,
uploads the cookbooks and invokes a chef solo run.&lt;/p&gt;

&lt;p&gt;Note that the ruby version used to run chef needs not be the one used
by passenger to later run the app. We usually have a chef recipe
install rvm on server to gain flexibility.&lt;/p&gt;

&lt;p&gt;The following capistrano tasks wires everything together. When we run
&lt;code&gt;cap provision&lt;/code&gt;, we first tell berkshelf to unpack the required
cookbooks to a &lt;code&gt;tmp&lt;/code&gt; directory. The &lt;code&gt;:cookbooks_directory&lt;/code&gt; option
tells roundsman to pick up on those cookbooks. Finally the
&lt;code&gt;roundsman.run_list&lt;/code&gt; command triggers the chef solo run.&lt;/p&gt;

&lt;p&gt;By default, roundsman installs a rather outdated version of chef. This
can easily be fixed by setting the &lt;code&gt;:chef_version&lt;/code&gt; option.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# deploy/tasks/provision.rb
require &amp;#39;roundsman/capistrano&amp;#39;

set :application, &amp;#39;some_app&amp;#39;
server &amp;#39;someapp.example.com&amp;#39;, :app
set :user, &amp;#39;ubuntu&amp;#39;

set :cookbooks_directory, [&amp;#39;tmp/cookbooks&amp;#39;]
set :chef_version, &amp;#39;11.4.0&amp;#39;

namespace :provision do
  desc &amp;#39;Install cookbooks and provision server&amp;#39;
  task :default do
    install
    apply
  end

  desc &amp;#39;Install cookbooks with berkshelf&amp;#39;
  task :install do
    run_local &amp;quot;bundle exec berks install --path #{cookbook_directory}&amp;quot;
  end

  desc &amp;#39;Provision server&amp;#39;
  task :apply do
    roundsman.run_list &amp;#39;recipe[main]&amp;#39;
  end
end

def run_local(command)
  system(command)
  if($?.exitstatus != 0) then
    puts &amp;#39;exit code: &amp;#39; + $?.exitstatus.to_s
    exit
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Happy provisioning!&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Decorating Active Record Models</title>
     <link href="http://stderr.timfischbach.de/2013/10/08/decorating-models.html"/>
     <updated>2013-10-08T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/10/08/decorating-models</id>
     <content type="html">&lt;p&gt;In the quest against god object active records, decorators provide a
nice way to group functionality according to features of your
application. They help to keep your models lean and ease separation of
responsibilities.&lt;/p&gt;

&lt;h3&gt;A simplified View&lt;/h3&gt;

&lt;p&gt;Often certain parts of an application only need to act upon a
simplified projection of the actual domain model. Imagine for example
an &lt;code&gt;Entry&lt;/code&gt; model which consists of various &lt;code&gt;Revisions&lt;/code&gt;. Furthermore,
let&amp;#39;s say, for each entry, there is always at most one published
revision:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class Entry &amp;lt; ActiveRecord::Base
  has_many :revisions
  has_one :published_revision, -&amp;gt; { published }, class: &amp;#39;Revision&amp;#39;
end

class Revision &amp;lt; ActiveRecord::Base
  scope :published, -&amp;gt; { where(published: true) }
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Only the contents of the published revision is supposed to be visible
on the public site. Instead of making our controllers and views deal
with the complexity of finding the right revision to display, we
introduce a decorator to handle the delegation.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class PublishedEntry
  def initialize(entry)
    @entry = entry
    @published_revision = entry.published_revision
  end

  delegate :title, :description, :to =&amp;gt; :@published_revision
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Code using the decorator can now remain totally unaware of the
revision concept.  Even better, once we decide to change the logic
determining the correct revision for public display, there is a single
place to make that change.&lt;/p&gt;

&lt;h3&gt;A Place for Finders&lt;/h3&gt;

&lt;p&gt;Since our decorator class is tailored for a rather specific use case,
we know what data is going to be needed. This knowledge can be
captured in a custom finder which performs appropriate eager loading.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class PublishedEntry
  // ...

  def self.find(id)
    new(Entry.find(id).includes(:published_entry));
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;No need to either scatter these details across controller actions, nor
let a bunch of unrelated finder methods pile up in the &lt;code&gt;Entry&lt;/code&gt; model.&lt;/p&gt;

&lt;h3&gt;Quacking like a Model&lt;/h3&gt;

&lt;p&gt;From the controller&amp;#39;s point of view, the decorator looks a lot like
the &lt;code&gt;ActiveRecord&lt;/code&gt; model.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class EntriesController &amp;lt; ApplicationController
  def show
    @entry = PublishedEntry.find(params[:id])
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To preserve Rail&amp;#39;s magic when passing records to routing, render and
form helpers, we delegate the required &lt;code&gt;ActiveModel&lt;/code&gt; methods to the
decorated &lt;code&gt;Entry&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class PublishedEntry
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  delegate :to_model, :to_key, :persisted?, :to =&amp;gt; :@entry

  def initialize(entry)
    @entry = entry
    @published_revision = entry.published_revision
  end

  // ...
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;PublishedEntry&lt;/code&gt; now basically looks like &lt;code&gt;Entry&lt;/code&gt; to &lt;code&gt;ActionPack&lt;/code&gt;
helpers.&lt;/p&gt;

&lt;h1&gt;A Word about Testing&lt;/h1&gt;

&lt;p&gt;Whether it makes sense to test presentational decorators in isolation,
really depends on the kind of logic you end up encapsulating in
them. The more tightly they are integrated with active record specific
constructs like scopes or associations, the more a black box approach
makes sense verifying that all parts are wired up correctly.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Making Rails 4 and Sinatra share a Session</title>
     <link href="http://stderr.timfischbach.de/2013/09/14/rails-4-and-sinatra-session-sharing.html"/>
     <updated>2013-09-14T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/09/14/rails-4-and-sinatra-session-sharing</id>
     <content type="html">&lt;p&gt;In our latest project, I wanted to restrict access to the
&lt;a href=&quot;https://github.com/resque/resque&quot;&gt;resque&lt;/a&gt; web interface by reusing
the
&lt;a href=&quot;https://github.com/hassox/warden&quot;&gt;Warden&lt;/a&gt;/&lt;a href=&quot;https://github.com/plataformatec/devise&quot;&gt;Devise&lt;/a&gt;/&lt;a href=&quot;https://github.com/ryanb/cancan&quot;&gt;CanCan&lt;/a&gt;
based authorization stack of the main Rails app.  I quickly found
&lt;a href=&quot;http://neovintage.blogspot.de/2011/11/mounting-resque-web-in-rails-3-using.html&quot;&gt;some&lt;/a&gt;
&lt;a href=&quot;http://labnote.beedesk.com/sinatra-warden-rails-devise&quot;&gt;posts&lt;/a&gt;
explaining how to mount the resque Sinatra app side by side with the
Rails app in the &lt;code&gt;config.ru&lt;/code&gt; file. Still, I always ended up with an
empty &lt;code&gt;env[&amp;#39;rack.session&amp;#39;]&lt;/code&gt; inside the Sinatra app.&lt;/p&gt;

&lt;p&gt;As it turns out, Rails 4 changes the session middleware to use
encrypted cookies.  It no longer relies on
&lt;a href=&quot;https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb#L47&quot;&gt;&lt;code&gt;Rack::Session::Cookie&lt;/code&gt;&lt;/a&gt;,
but uses
&lt;a href=&quot;https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb#L54&quot;&gt;&lt;code&gt;ActionDispatch::Session::CookieStore&lt;/code&gt;&lt;/a&gt;
instead.&lt;/p&gt;

&lt;p&gt;Unfortunately, the new &lt;code&gt;CookieStore&lt;/code&gt; middleware cannot live on its own
since the &lt;code&gt;ActionDispatch::Cookies::CookieJar&lt;/code&gt; it uses to get cookies
&lt;a href=&quot;https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/cookies.rb#L219&quot;&gt;requires an &lt;code&gt;action_dispatch.key_generator&lt;/code&gt;&lt;/a&gt;
to be present on the &lt;code&gt;env&lt;/code&gt;.  If it is missing, the session cookie
cannot be decrypted leading to silent failure.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Rails::Application&lt;/code&gt; objects place the key generator on an
&lt;a href=&quot;https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L176&quot;&gt;&lt;code&gt;env_config&lt;/code&gt; hash&lt;/a&gt;,
which is
&lt;a href=&quot;https://github.com/rails/rails/blob/master/railties/lib/rails/engine.rb#L512&quot;&gt;merged into the request&amp;#39;s env&lt;/a&gt;
before processing a request.  To make the Rails session available to
our Sinatra app, we have to insert a middleware which mimics this
behaviour:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# rails_env_config_middleware.rb 
class RailsEnvConfigMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    env.merge!(Rails.application.env_config)
    @app.call(env)
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The final rack up file then looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# config.ru
require ::File.expand_path(&amp;#39;../config/environment&amp;#39;,  __FILE__)

map &amp;#39;/&amp;#39; do
  run Rails.application
end

map &amp;#39;/resque&amp;#39; do
  # This is the important line
  use RailsEnvConfigMiddleware

  use ActionDispatch::Session::CookieStore, :key =&amp;gt; &amp;#39;_&amp;lt;app_name&amp;gt;_session&amp;#39;, 
    :path =&amp;gt; &amp;#39;/&amp;#39;, :secret =&amp;gt; &amp;#39;&amp;lt;secret_key_base&amp;gt;&amp;#39;

  use Warden::Manager do |manager|
    manager.failure_app = Pageflow::Application
    manager.default_scope = Devise.default_scope
  end

  run AdminResqueServer.new
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can access the authenticated user via warden and authorize with
CanCan:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# admin_resque_server.rb
require &amp;#39;resque/server&amp;#39;
require &amp;#39;resque_scheduler/server&amp;#39;

class AdminResqueServer &amp;lt; Resque::Server
  before do
    redirect &amp;#39;/admin/login&amp;#39; unless authenticated?
    redirect &amp;#39;/&amp;#39; unless can?(:manage, Resque)
  end

  private

  def can?(*args)
    Ability.new(user).can?(*args)
  end

  def authenticated?
    request.env[&amp;#39;warden&amp;#39;].authenticated?
  end

  def user
    request.env[&amp;#39;warden&amp;#39;].user
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In our production environment, I faced a final bug.  For some reason the
&lt;a href=&quot;https://github.com/rkh/rack-protection&quot;&gt;&lt;code&gt;rack-protection&lt;/code&gt;&lt;/a&gt; middleware
which comes as
&lt;a href=&quot;https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1742&quot;&gt;part of&lt;/a&gt;
the Sinatra stack kept
&lt;a href=&quot;https://github.com/rkh/rack-protection/blob/master/lib/rack/protection/base.rb#L88&quot;&gt;dropping the session&lt;/a&gt;
as a reaction to some falsely detected attack.  Deactivating
protection inside the Sinatra app solved the issue for now. &lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;class AdminResqueServer &amp;lt; Resque::Server
  disable :protection

  ...
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is of course not the desired solution. I&amp;#39;ll post an update once I
have figured out the details.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Organizing Integration Test Helpers with Dominos</title>
     <link href="http://stderr.timfischbach.de/2013/09/10/dominos-in-integration-tests.html"/>
     <updated>2013-09-10T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/09/10/dominos-in-integration-tests</id>
     <content type="html">&lt;p&gt;The &lt;a href=&quot;https://github.com/ngauthier/domino&quot;&gt;domino&lt;/a&gt; gem by
&lt;a href=&quot;https://github.com/ngauthier&quot;&gt;Nick Gauthier&lt;/a&gt; provides a great way to
organize your integration test support methods.  Before, I used to
package test helpers as mixins.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# spec/features/admin/user_locking_spec.rb
feature &amp;#39;user locking&amp;#39; do
  include UsersTestHelper

  scenario &amp;#39;locking a user account&amp;#39; do
    user = create(:user)

    visit user_path(user)
    lock_user_button.click

    expect(page).to have_locked_notice
  end
end

# spec/support/integration/users_test_helper.rb
module UsersTestHelper
  extend RSpec::Matchers::DSL

  def lock_user_button
    page.find(&amp;#39;section.user [data-rel=lock]&amp;#39;)
  end

  matcher :have_locked_notice do |*args|
    # check if there is an element matching .lock_notice
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While those usally made for quite readable tests, there were two major
downsides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Findability&lt;/em&gt; &amp;mdash; It quickly became hard to tell where a certain
helper method was defined as soon as more than one or two mixins
were used in a test.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Naming clashes&lt;/em&gt; &amp;mdash; Extracting private methods to clean up test
helper methods came with the risk of introducing name collisions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With dominos you can take a more object oriented approach:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# spec/support/dominos/user_page.rb
module Dom 
  UserSection &amp;lt; Domino
    selector &amp;#39;section.user&amp;#39;

    def lock_button
      node.find(&amp;#39;[data-rel=lock]&amp;#39;)
    end

    def has_locked_notice?
      node.has_selector?(&amp;#39;.lock_notice&amp;#39;)
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The test from above now becomes:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# spec/features/admin/user_locking_spec.rb
feature &amp;#39;user locking&amp;#39; do
  scenario &amp;#39;locking a user account&amp;#39; do
    user = create(:user)

    visit user_path(user)
    Dom::UserSection.first.lock_button.click

    expect(Dom::UserSection.first).to have_locked_notice
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While this arguably reads a bit more cryptic at first, there are a
couple of wins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is now directly obvious where each methods is defined.&lt;/li&gt;
&lt;li&gt;Private methods can freely be extracted inside dominos.&lt;/li&gt;
&lt;li&gt;Custom matchers can be replaced by RSpec&amp;#39;s built in predicate
matchers.&lt;/li&gt;
&lt;li&gt;It is easy to adapt a component based mental model of your UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While there are other great features, the
&lt;a href=&quot;https://github.com/ngauthier/domino&quot;&gt;domino&lt;/a&gt; codebase only clocks in
at about 160 LOC including doc comments.  So its a rather lightweight
dependency to add to your testing stack.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Automating MySQL Setup for Jenkins Jobs</title>
     <link href="http://stderr.timfischbach.de/2013/09/07/jenkins-mysql-job-database-plugin.html"/>
     <updated>2013-09-07T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/09/07/jenkins-mysql-job-database-plugin</id>
     <content type="html">&lt;p&gt;Creating databases is one of those repetitive tasks when setting up
continuous integration for a project.  At work we use Jenkins.  So
we&amp;#39;ve developed the
&lt;a href=&quot;http://github.com/codevise/jenkins-mysql-job-databases-plugin&quot;&gt;jenkins-mysql-job-databases-plugin&lt;/a&gt;
to automate the required steps.&lt;/p&gt;

&lt;p&gt;The plugin creates a database and a user for the job, granting access
only to the job&amp;#39;s database.  User credentials and database name are
exported to environment variables, which is a great fit with the
&lt;a href=&quot;http://github.com/codevise/ci_config_generator&quot;&gt;ci&lt;em&gt;config&lt;/em&gt;generator&lt;/a&gt;
gem we saw in a
&lt;a href=&quot;/2013/09/05/generating-ignored-config-files-for-ci.html&quot;&gt;previous post&lt;/a&gt;.
In a Rails project, for example, you could commit the following file
to wire everything up:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# database.yml.ci
test:
  adapter: mysql2
  encoding: utf8
  database: %{MYSQL_DATABASE}
  username: %{MYSQL_USER}
  password: %{MYSQL_PASSWORD}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The plugin itself is
&lt;a href=&quot;https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+plugin+development+in+Ruby&quot;&gt;written in Ruby&lt;/a&gt;.
Since it is
&lt;a href=&quot;https://github.com/jenkinsci/jenkins.rb/issues/67&quot;&gt;unclear&lt;/a&gt; how to
handle global configuration options in Ruby plugins, the credentials
of the MySQL user that creates databases and grants access are hard
coded in the plugin at the moment.  Apart from this caveat, the plugin
has worked well for us so far.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Generating Configuration Files for CI</title>
     <link href="http://stderr.timfischbach.de/2013/09/05/generating-ignored-config-files-for-ci.html"/>
     <updated>2013-09-05T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/09/05/generating-ignored-config-files-for-ci</id>
     <content type="html">&lt;p&gt;When setting up continuous integration for a project, manually editing
host specific configuration has always annoyed me.  The
&lt;a href=&quot;http://github.com/codevise/ci_config_generator&quot;&gt;ci&lt;em&gt;config&lt;/em&gt;generator&lt;/a&gt;
gem sets out to free you from fiddling with config files in your ci
job&amp;#39;s workspace directory.&lt;/p&gt;

&lt;p&gt;Instead, it lets you create templates for ignored config files.  For
each of those &lt;code&gt;.yml&lt;/code&gt; files, simply commit an accompanying &lt;code&gt;.yml.ci&lt;/code&gt;
file and run the &lt;code&gt;ci:config:generate&lt;/code&gt; rake task at the beginning of
your ci job.  While copying files, the task interpolates environment
variables:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;# zencoder.yml.ci
test:
  output_bucket: &amp;quot;com.example.test&amp;quot;
  api_key: &amp;quot;%{API_KEY}&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That way, passwords and other secret bits of data do not need to be
stored in the repository, but can be configures as build parameters
via your ci servers admin interface.&lt;/p&gt;

&lt;p&gt;As an additional benefit, new developers on your team can follow a
self documented and continuously tested path to obtain a passing test
suite.&lt;/p&gt;
</content>
   </entry>
 
   <entry>
     <title>Sass Mixins and Icon Fonts</title>
     <link href="http://stderr.timfischbach.de/2013/09/01/sass-mixins-and-icon-fonts.html"/>
     <updated>2013-09-01T00:00:00+00:00</updated>
     <id>http://stderr.timfischbach.de//2013/09/01/sass-mixins-and-icon-fonts</id>
     <content type="html">&lt;p&gt;Sass mixins are a great tool for building abstractions and writing
intention revealing CSS code.  One application lies in using them with
icons fonts.  Instead of littering your HTML with style specific icon
classes, we can define mixins to apply icons in a declarative manner:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;a.edit {
  @include pencil-icon;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The basic idea of the mixin definition is the following:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;@mixin icon($font-name, $char) {
  &amp;amp;:before {
    content: $char;
    font-family: $font-name;
  }
}

@mixin pencil-icon { @include icon(&amp;quot;entypo&amp;quot;, &amp;quot;\270E&amp;quot;); }
@mixin warning-icon { @include icon(&amp;quot;entypo&amp;quot;, &amp;quot;\26A0&amp;quot;); }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This approach also makes it easy to swap icon fonts later on by
limiting duplication.&lt;/p&gt;

&lt;h3&gt;Taking it further&lt;/h3&gt;

&lt;p&gt;Extracting other patterns concerning icon placement and styling, an
expressive language can be built up.  The following example uses some
simple mixins defined in
&lt;a href=&quot;https://gist.github.com/tf/6485675#file-background_icon_mixins-scss&quot;&gt;this gist&lt;/a&gt;
to display icons in the background of navigation links.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;nav a {
  @include background-icon($font-size: 20px);
}

a.edit {
  @include pencil-icon;
}

a.errors {
  @include warning-icon;
  @include background-icon-color(#d00);
  @include background-icon-animation(pulse);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A careful selection of such mixins might make a great gem in the vein
of &lt;a href=&quot;http://bourbon.io&quot;&gt;Bourbon&lt;/a&gt; or other mixin libraries.&lt;/p&gt;
</content>
   </entry>
 
</feed>
