<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users548</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/548" rel="self"/>
  <title>mislav</title>
  <updated>Thu Jun 19 19:46:20 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor11187</id>
    <published>2008-06-19T19:46:20-07:00</published>
    <title>[Ruby] On ActiveRecord: named_scope for a boolean</title>
    <content type="html">&lt;p&gt;When you have to support NULL value and treat is as false in this particular case:&lt;/p&gt;

&lt;pre&gt;class MyModel
  named_scope :not_hidden, :conditions =&amp;gt; ['hidden IS NULL OR hidden = ?', false]
end&lt;/pre&gt;</content>
    <author>
      <name>mislav</name>
      <email>mislav.marohnic@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/332-activerecord-named_scope-for-a-boolean/refactors/11187" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor11186</id>
    <published>2008-06-19T19:44:59-07:00</published>
    <title>[Ruby] On ActiveRecord: named_scope for a boolean</title>
    <content type="html">&lt;p&gt;When NULL value is not allowed, this is adequate:&lt;/p&gt;

&lt;pre&gt;class MyModel
  named_scope :not_hidden, :conditions =&amp;gt; { :hidden =&amp;gt; false }
end&lt;/pre&gt;</content>
    <author>
      <name>mislav</name>
      <email>mislav.marohnic@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/332-activerecord-named_scope-for-a-boolean/refactors/11186" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code332</id>
    <published>2008-06-19T18:46:44-07:00</published>
    <updated>2008-07-17T22:58:34-07:00</updated>
    <title>[Ruby] ActiveRecord: named_scope for a boolean</title>
    <content type="html">&lt;p&gt;Can ActiveRecord be used to abstract this more nicely across databases?&lt;/p&gt;

&lt;pre&gt;class MyModel
  named_scope :not_hidden, :conditions =&amp;gt; ['hidden IS NULL OR hidden = 0 OR hidden = ?', 'f']
end&lt;/pre&gt;</content>
    <author>
      <name>mislav</name>
      <email>mislav.marohnic@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/332-activerecord-named_scope-for-a-boolean" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code281</id>
    <published>2008-04-12T15:12:14-07:00</published>
    <updated>2011-11-24T03:31:38-08:00</updated>
    <title>[Ruby] Given a hash of variables, render an ERB template</title>
    <content type="html">&lt;p&gt;This works. Now, is there a simpler way than my solution?&lt;/p&gt;

&lt;pre&gt;require 'erb'

# GOAL: render the template with the following vars
vars = { :first =&amp;gt; 'Mislav', 'last' =&amp;gt; 'Marohnic' }
template = 'Name: &amp;lt;%= first %&amp;gt; &amp;lt;%= last %&amp;gt;'

# create anonymous module which binding we will use
m = Module.new do
  # I want the singleton class, thank you
  class &amp;lt;&amp;lt; self
    public :binding
    
    def meta
      class &amp;lt;&amp;lt; self; self; end
    end
  end

  class &amp;lt;&amp;lt; meta
    public :define_method
  end

  # define singleton method for each var
  vars.each do |name, value|
    meta.define_method(name) { value }
  end
end

puts ERB.new(template).result(m.binding)
#=&amp;gt; &amp;quot;Name: Mislav Marohnic&amp;quot;
&lt;/pre&gt;</content>
    <author>
      <name>mislav</name>
      <email>mislav.marohnic@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/281-given-a-hash-of-variables-render-an-erb-template" rel="alternate"/>
  </entry>
</feed>

