<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en-US">
  <id>tag:luckysneaks.com,2008:/feeds</id>
  <link type="text/html" rel="alternate" href="/blog" />
  
  <title>Lucky Sneaks - Blog</title>
  <updated>2008-11-04T21:36:05Z</updated>
  <author>
    <name>Lucky Sneaks</name>
  </author>
  <link rel="self" href="http://feeds.feedburner.com/luckysneaks" type="application/atom+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
    <id>tag:luckysneaks.com,2008:Entry/251</id>
    <published>2008-11-04T21:36:05Z</published>
    <updated>2008-11-05T12:17:56Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/A47oV-rROHY/stringex-available-as-gem" />
    <title>Stringex Available As Gem</title>
    <content type="html">&lt;p&gt;Per the instructions at Github:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;gem sources -a http://gems.github.com
sudo gem install rsl-stringex&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then in your &lt;code&gt;config/environment.rb&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;config.gem "rsl-stringex", :version =&amp;gt; "~&amp;gt; 0.9.2", :lib =&amp;gt; "stringex"&lt;/code&lt;/pre&gt;

&lt;p&gt;That should do it. You won&amp;#8217;t need to add the github gem source if you&amp;#8217;ve done so previously.&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/stringex-available-as-gem</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/233</id>
    <published>2008-08-22T15:54:57Z</published>
    <updated>2008-09-24T21:05:37Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/WOraIkfTjb4/updated-never" />
    <title>Updated? &lt;em&gt;Never&lt;/em&gt;</title>
    <content type="html">&lt;p&gt;This one goes out to &lt;del&gt;the one I love&lt;/del&gt; anyone else who gets tired of seeing times for &lt;code&gt;updated_at&lt;/code&gt; when the object hasn&amp;#8217;t actually been &lt;em&gt;updated&lt;/em&gt;. [&lt;code&gt;updated_at&lt;/code&gt; gets set at creation.]&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;module UpdatedOrNever
  def updated?
    updated_at != created_at
  rescue NoMethodError
    false
  end

  def updated_or_never
    updated? ? updated_at : "never" 
  end
end

ActiveRecord::Base.send :include, LuckySneaks::UpdatedOrNever&lt;/code&gt;&lt;/pre&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/updated-never</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/232</id>
    <published>2008-08-21T15:50:54Z</published>
    <updated>2008-09-08T11:38:45Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/ukNPPOmM-U4/overriding-default-error-messages-new-styley" />
    <title>Overriding Default Error Messages, New Styley!</title>
    <content type="html">&lt;p&gt;I&amp;#8217;m posting this for anyone who&amp;#8217;s googling how to replace their old school error message overrides with the new Rails&amp;#8217; I18n-compatible version.&lt;/p&gt;


&lt;p&gt;Old school [please note this is the old, deprecated way just shown here for google to hook into for people searching on various lines of the old code]:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;module ActiveRecord
  class Errors
    @@default_error_messages.merge!({
      :invalid =&amp;gt; "doesn't appear to be valid",
      :too_long =&amp;gt; "can't be more than %d characters",
      :too_short =&amp;gt; "can't be less than %d characters",
      :wrong_length =&amp;gt; "must be exactly %d characters",
      :taken =&amp;gt; "is already taken",
    })
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;New school [updated to latest edge Rails]:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;I18n.backend.store_translations "en-US", {
  :activerecord =&amp;gt; {
    :errors =&amp;gt; {
      :messages =&amp;gt; {
        :invalid =&amp;gt; "doesn't appear to be valid",
        :too_long =&amp;gt; "can't be more than {{count}} characters",
        :too_short =&amp;gt; "can't be less than {{count}} characters",
        :wrong_length =&amp;gt; "must be exactly {{count}} characters",
        :taken =&amp;gt; "is already taken" 
      }
    }
  }
}&lt;/code&gt;&lt;/pre&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/overriding-default-error-messages-new-styley</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/228</id>
    <published>2008-07-31T12:10:43Z</published>
    <updated>2008-07-31T12:18:34Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/MUXO3ZAD5VI/when-now-is-not-now" />
    <title>How Soon Is Now? [Too Soon, Apparently]</title>
    <content type="html">&lt;pre&gt;&lt;code&gt;&amp;gt; Foo.create 
# =&amp;gt; #&amp;lt;Foo id: 1, created_at: "2008-07-31 11:37:02", updated_at: "2008-07-31 11:37:02"&amp;gt;
&amp;gt; Foo.find :all, :conditions =&amp;gt; {:created_at =&amp;gt; 1.day.ago..Time.now}
# =&amp;gt; []
&amp;gt; Foo.find :all, :conditions =&amp;gt; {:created_at =&amp;gt; 1.day.ago..Time.zone.now}
# =&amp;gt; [#&amp;lt;Foo id: 1, created_at: "2008-07-31 11:37:02", updated_at: "2008-07-31 11:37:02"&amp;gt;]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If it&amp;#8217;s not obvious why that first find didn&amp;#8217;t work, it&amp;#8217;s because I&amp;#8217;m using &lt;span class="caps"&gt;UTC&lt;/span&gt; on my timestamps and Time.now is 4 hours behind that where I live at this time of year. Swaddling it with time zone action fixes it right up though.&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/when-now-is-not-now</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/220</id>
    <published>2008-06-27T15:36:14Z</published>
    <updated>2008-06-27T15:38:12Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/RaoE-SJw41E/my-stubcurrentuser-rspec-helper" />
    <title>My stub_current_user RSpec Helper</title>
    <content type="html">&lt;p&gt;The other day I came up with a really useful spec helper for stubbing out the ubiquitous &lt;code&gt;current_user&lt;/code&gt; all over my specs and thought I&amp;#8217;d share it with the world. It might end up as part of &lt;a href="http://github.com/rsl/skinny_spec/tree/master"&gt;Skinny Spec&lt;/a&gt; but might be a little too tied in a specific authentication pattern. Dunno. Here it is anyhow:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def stub_current_user(stubs = {})
  returning stub_model(User, stubs) do |user|
    object = case self
      when Spec::Rails::Example::ViewExampleGroup : template
      when Spec::Rails::Example::HelperExampleGroup : helper
      when Spec::Rails::Example::ControllerExampleGroup
        User.stub!(:find).with(user.id).and_return(user)
        # Because params values are actually strings
        User.stub!(:find).with(user.id.to_s).and_return(user)
        controller
    end
    object.stub!(:current_user).and_return(user)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It uses the new &lt;code&gt;stub_model&lt;/code&gt; method available in RSpec versions greater than 1.1.4 instead of &lt;code&gt;mock_model&lt;/code&gt;. You can totally just replace that call with one to &lt;code&gt;mock_model&lt;/code&gt; but I encourage you to try it out because it allows you a lot more flexibility in your view specs. You can read a little more about it in &lt;a href="http://blog.davidchelimsky.net/articles/2008/05/27/rspec-1-1-4"&gt;David Chelimsky&amp;#8217;s post about RSpec 1.1.4&lt;/a&gt;. Love that new &lt;code&gt;hash_including&lt;/code&gt; action as well. Been looking for something like that for a long time.&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/my-stubcurrentuser-rspec-helper</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/219</id>
    <published>2008-06-19T14:51:07Z</published>
    <updated>2008-06-19T14:53:43Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/cu_pQi2v968/how-to-spec-flash-dot-now-in-one-easy-step" />
    <title>How to Spec flash.now in One Easy Step</title>
    <content type="html">&lt;p&gt;Was screwing around this morning trying to successfully spec that &lt;code&gt;flash[:error]&lt;/code&gt; is set when the controller actually sets &lt;code&gt;flash.now[:error]&lt;/code&gt;. A couple of different Google results had some different approaches none of which thrilled me. Then I tried completely mocking out the &lt;code&gt;flash&lt;/code&gt; completely and got this lovely error:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;Mock 'Flash now!' received unexpected message :sweep with (no args)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which gave me the brilliant idea to just stub &lt;code&gt;:sweep&lt;/code&gt; on the &lt;code&gt;flash&lt;/code&gt;. Lo and behold, it works and with a minimum of effort too.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;it "should set flash[:error]" do
  flash.stub!(:sweep)
  post :create, params[:foo] # You'd have set this up for failure in the before block or whatever
  flash[:error].should_not be_nil # Spec'ing a specific error message is way brittle
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Just thought I&amp;#8217;d share. Hope you can benefit from it.&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/how-to-spec-flash-dot-now-in-one-easy-step</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/19</id>
    <published>2008-04-10T04:13:48Z</published>
    <updated>2008-04-10T04:26:08Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/xZuQhXUngVc/split-captures" />
    <title>Split Captures</title>
    <content type="html">&lt;p&gt;I was working on some RMagick magic the other day and used &lt;code&gt;String#split&lt;/code&gt; to parse the width and height from a string and was a little surprised by the outcome:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# where string = "800x600" 
width, height = string.split(/(x|or)/)
# returned =&amp;gt; ["800", "x", "600"]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For those of you who might also be surprised as well, the parentheses in my regex there are making what&amp;#8217;s known as a capture or backreference. In my case this was actually beneficial as it saved me having to run another regex on the string to determine in the &amp;#8220;operator&amp;#8221; used was &amp;#8220;x&amp;#8221; or &amp;#8220;or&amp;#8221;. The &lt;a href="http://www.ruby-doc.org/core/classes/String.html#M000818"&gt;Ruby docs&lt;/a&gt; for &lt;code&gt;String#split&lt;/code&gt; don&amp;#8217;t mention this behavior and merely state that &amp;#8220;[if] pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters.&amp;#8221; but I wouldn&amp;#8217;t call this a bug at all as once you think about it it makes perfect sense what is happening. Call it a pleasantly surprising feature.&lt;/p&gt;


&lt;p&gt;Oh, by the way. Had I not wanted the regex to capture like that I could either use &lt;code&gt;/(?:x|or)/&lt;/code&gt;, which tells the regex engine not to create a backreference, or simply &lt;code&gt;/x|or/&lt;/code&gt;, a solution [thanks apeiros!] that I was not aware of. For whatever reason I thought that alternation always had to occur within parentheses.&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/split-captures</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/18</id>
    <published>2008-04-09T04:20:10Z</published>
    <updated>2008-04-09T04:59:06Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/g0Oo7aG3J_Y/css-naked-day" />
    <title>CSS Naked Day</title>
    <content type="html">&lt;p&gt;Surprise! I&amp;#8217;m naked! Today is &lt;a href="http://naked.dustindiaz.com/"&gt;&lt;span class="caps"&gt;CSS&lt;/span&gt; Naked Day&lt;/a&gt;, a web event meant to underline the important of good markup and structure. As the event&amp;#8217;s organizer states: &amp;#8220;It&amp;#8217;s time to show off your &amp;lt;body&amp;gt;.&amp;#8221; As you can see, it&amp;#8217;s quite possible to have a nice looking site and have it fully functional and &lt;em&gt;readable&lt;/em&gt; without styling. Yay, web standards and good markup!&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/css-naked-day</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/17</id>
    <published>2008-03-26T13:24:23Z</published>
    <updated>2008-03-26T18:10:41Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/58eUoIwQnCI/class-alias" />
    <title>Class Alias</title>
    <content type="html">&lt;p&gt;I&amp;#8217;m working on a new plugin and found myself having to access a class variable &lt;em&gt;and&lt;/em&gt; class method from the instance level and wasn&amp;#8217;t too happy with how it was looking:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def instance_foo(args)
  self.class.method(self.class.accessor[args]))
end
&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;Sure, I could solve this by writing more code and adding even more complexity but, man, did I wish you could just do something like &lt;code&gt;alias self.foo foo&lt;/code&gt;. Oh, wait! This is Ruby. Just open that baby up!&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Object
  def class_alias(method_id)
    define_method(method_id) do |*args|
      self.class.send method_id, *args
    end
  end
end

class Foo
  def self.bar
    puts "I got called!" 
  end
  class_alias :bar
end

# Show me the money!
Foo.bar =&amp;gt; "I got called!" 
f = Foo.new
f.bar =&amp;gt; "I got called!" 
&lt;/code&gt;&lt;/pre&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/class-alias</feedburner:origLink></entry>
  <entry>
    <id>tag:luckysneaks.com,2008:Entry/16</id>
    <published>2008-03-02T19:33:41Z</published>
    <updated>2009-03-11T11:45:35Z</updated>
    <link type="text/html" rel="alternate" href="http://feedproxy.google.com/~r/luckysneaks/~3/p1zJak-CKz4/spec-resourceful" />
    <title>Spec Resourceful</title>
    <content type="html">&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The plugin in this post has been superceded by the &lt;a href="http://github.com/rsl/skinny_spec/tree/master"&gt;Skinny Spec&lt;/a&gt; plugin. Please use that plugin instead.&lt;/p&gt;
&lt;p&gt;I just committed a new plugin to GitHub called &lt;a href="http://github.com/rsl/spec_resourceful/tree/master"&gt;&lt;code&gt;spec_resourceful&lt;/code&gt;&lt;/a&gt;. Well, sorta new. It&amp;#8217;s actually a fork of a plugin I use all the time (make_resourceful) but with its scaffold generator generating specs with RSpec instead of Test::Unit. I also rewrote the scaffolding to fit my own aesthetic. Like, instead of using a form for &lt;code&gt;:new&lt;/code&gt; and another almost identical one for &lt;code&gt;:edit&lt;/code&gt;, I prefer using just one template (&lt;code&gt;form.html.haml&lt;/code&gt;) and letting &lt;code&gt;form_for&lt;/code&gt; do its magic. I also have a before_filter that redirects &lt;code&gt;:create&lt;/code&gt;/&lt;code&gt;:update&lt;/code&gt; requests with a &lt;code&gt;params[:cancel]&lt;/code&gt; to the &lt;code&gt;:index&lt;/code&gt; action. Not sure if anyone else out there would like it but I&amp;#8217;m offering it to the world anyhow. :)&lt;/p&gt;
&lt;p&gt;One thing I do think other people [who use RSpec] might like is the view spec helpers I wrote that help make those view specs less brittle. Instead of spec&amp;#8217;ing a specific &lt;span class="caps"&gt;HTML&lt;/span&gt; element in a form you can just &lt;code&gt;response.should allow_editing(@foo, :bar)&lt;/code&gt; and feel free to tweak your &lt;span class="caps"&gt;HTML&lt;/span&gt; with impunity knowing that you&amp;#8217;ve spec&amp;#8217;ed the behavior (that the user can edit &lt;code&gt;@foo&lt;/code&gt;&amp;#8217;s &lt;code&gt;bar&lt;/code&gt; attribute) and not what &lt;span class="caps"&gt;HTML&lt;/span&gt; element you used to edit it. You can &lt;a href="http://github.com/rsl/spec_resourceful/tree/master/generators/spec_resourceful/templates/spec_helpers.rb"&gt;check them out&lt;/a&gt; at the &lt;a href="http://github.com/rsl/spec_resourceful/tree/master"&gt;GitHub repo&lt;/a&gt;. I&amp;#8217;d love your feedback if you use this. Or if you don&amp;#8217;t. Unless your name is Pratik and I already know what you&amp;#8217;re going to say.&lt;/p&gt;</content>
  <feedburner:origLink>http://luckysneaks.com/blog/spec-resourceful</feedburner:origLink></entry>
</feed>
