<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Bear Den Designs - The Hacker's Musings</title>
    <link>http://www.beardendesigns.com/blogs/feed/sphinx?user=1019</link>
    <description>Blog postings by The Hacker of Bear Den Designs</description>
    <language>en-us</language>
    <atom:link type="application/rss+xml" href="http://www.beardendesigns.com/blogs/feed/sphinx?user=1019" rel="self"/>
    <item>
      <title>Flexible date input and manipulation in javascript with date.js</title>
      <description>&lt;p&gt;date.js is a great little JavaScript library that can make your life a lot easier.&lt;/p&gt;
&lt;p&gt;If your used to ruby&amp;#8217;s date functions then date.js will make you feel right at home.&lt;/p&gt;
&lt;p&gt;date.js can:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Parse strings into dates.&lt;/li&gt;
	&lt;li&gt;Add and Subtract time in days,hours,months or years.&lt;/li&gt;
	&lt;li&gt;Easily return &amp;#8220;x&amp;#8221; &amp;#8220;day of week&amp;#8221; of &amp;#8220;month&amp;#8221;&lt;/li&gt;
	&lt;li&gt;Boolean assertions for day, week, month, year.&lt;/li&gt;
	&lt;li&gt;Turn you into a JavaScript ninja.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Note: Some of the syntactic goodies require &amp;#8216;sugar.js&amp;#8217;&lt;/h2&gt;
&lt;p&gt;Going into the future is no problem for date.js&lt;/p&gt;
Date.today().add(5).days();
Date.today().next().friday();
&lt;p&gt;Interested in this Friday? April of this year?&lt;/p&gt;
Date.friday();
Date.april();
&lt;p&gt;What about the first friday of april? No Sweat!&lt;/p&gt;
Date.april().first().friday();
&lt;p&gt;Assert any date properties you want.&lt;/p&gt;
Date.today().is().friday();  // returns true orfalse
&lt;p&gt;It can parse just about anything you throw at it.&lt;/p&gt;
Date.parse(&amp;#8216;today&amp;#8217;);
Date.parse(&amp;#8216;tomorrow&amp;#8217;);
Date.parse(&amp;#8216;July 8&amp;#8217;);
Date.parse(&amp;#8216;July 8th, 2007&amp;#8217;);
Date.parse(&amp;#8216;July 8th, 2007, 10:30 PM&amp;#8217;);
// Even crazy! things like
Date.parse(&amp;#8216;last april&amp;#8217;);
Date.parse(&amp;#8216;+2days&amp;#8217;);
&lt;p&gt;There are also some fun number functions.&lt;/p&gt;
(8).days().fromNow();
(2).months().ago();
&lt;p&gt;For more reading, checkout: &lt;a href=&quot;http://www.datejs.com/2007/11/27/getting-started-with-datejs/&quot;&gt;date.js examples&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 21 Feb 2009 03:30:45 -0600</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/62</link>
      <category>rails/javascript/ruby/sugar</category>
      <guid>http://beardendesigns.com/blogs/show?62</guid>
    </item>
    <item>
      <title>GeoKit stubbing for faster tests</title>
      <description>&lt;p&gt;We recently added geocoding to after_save on an address model to keep track of peoples lat/lng, and found that it added substantial time to our tests. The solution? Easy.&lt;/p&gt;
&lt;p&gt;Don&amp;#8217;t really need to test that geolocating works, I mean.. GeoKit has its own unit tests. So Stub it! Stub it good!&lt;/p&gt;
&lt;p&gt;Place this in your test_helper.rb (and inside Test::Unit::TestCase) for tolerable test times.&lt;/p&gt;
&lt;p&gt;&amp;#8220;This was written for mocha, but its easy to adapt to rspec or flexmock&amp;#8221;&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby:nocontrols:nogutter&quot;&gt;
  setup :stub_geocoder
  def stub_geocoder
    geocode_payload = GeoKit::GeoLoc.new(:lat =&amp;gt; 123.456, :lng =&amp;gt; 123.456)
    geocode_payload.success = true
    GeoKit::Geocoders::MultiGeocoder.stubs(:geocode).returns(geocode_payload)
  end
&lt;/pre&gt;
&lt;p&gt;&amp;#8220;Just remember to turn it off if you plan to test any features that rely on actual geocoding!&amp;#8221;&lt;/p&gt;</description>
      <pubDate>Wed, 07 Jan 2009 21:40:19 -0600</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/55</link>
      <category>rails/geocoding/stubbing</category>
      <guid>http://beardendesigns.com/blogs/show?55</guid>
    </item>
    <item>
      <title>Solution to: Permission denied to get property XULElement.popupOpen</title>
      <description>&lt;p&gt;What&amp;#8217;s that firefox? A cryptic error message? Timmy&amp;#8217;s drowning at the old mill?!&lt;/p&gt;
&lt;p&gt;I came across this rather strange bug while switching from a file input to a combination of SWFUploader and a text field.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Permission denied to get property XULElement.popupOpen&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The problem seems to occur when focusing on the input via javascript.&lt;/p&gt;
&lt;p&gt;Solution? Easy, Just add this to your text input:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;autocomplete=&amp;#8220;off&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And then give firefox a nice pop on the head for being a bad fox! Bad Fox! &amp;#8230;No!&lt;/p&gt;
&lt;p&gt;I hope that saves you some headache and confusion =)&lt;/p&gt;
&lt;p&gt;- Your Friendly Neighborhood Hacker&lt;/p&gt;</description>
      <pubDate>Thu, 09 Oct 2008 11:58:02 -0500</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/53</link>
      <category>javascript/firefox</category>
      <guid>http://beardendesigns.com/blogs/show?53</guid>
    </item>
    <item>
      <title>ActiveRecord.update_attributes has_many :security_holes, :through =&gt; :unkown_features</title>
      <description>&lt;p&gt;Among you railites who have successfully learned rails, watched tutorials, and generally feel comfortable about your abilities: Probably didn&amp;#8217;t know there is a little security hole in your app.&lt;/p&gt;
&lt;p&gt;It has to deal with update_attributes, has_many relationships, and a method made available on the parent in the relation.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby:nocontrols:nogutter&quot;&gt;
class User &amp;lt; ActiveRecord::Base
  has_many :groups
end
&lt;/pre&gt;
&lt;p&gt;In your view you have your pretty form with user&amp;#8217;s name and other demographics they can enter in their profile. and a &amp;#8216;&lt;strong&gt;save&lt;/strong&gt;&amp;#8217; button that leads to a call to &amp;#8216;&lt;strong&gt;update_attributes&lt;/strong&gt;&amp;#8217;.&lt;/p&gt;
&lt;p&gt;The problem lies in the fact that has_many creates a method off your object called&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby:nocontrols:nogutter&quot;&gt; user.group_ids= &lt;/pre&gt;
&lt;p&gt;Which allows you to pass in an array of ids and create associations en-mass.&lt;br /&gt;
the problem is that I can come in with firebug and add my own fields.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;html:nocontrols:nogutter&quot;&gt;
&amp;lt;!-- im in your html source adding my inputs --&amp;gt;
&amp;lt;input type=&quot;text&quot; name=&quot;user[group_ids][]&quot;/&amp;gt;
&amp;lt;input type=&quot;text&quot; name=&quot;user[group_ids][]&quot;/&amp;gt;
&amp;lt;input type=&quot;text&quot; name=&quot;user[group_ids][]&quot;/&amp;gt;
&lt;/pre&gt;
&lt;p&gt;After filling those fields and submiting, if you inspect the params hash you will notice:&lt;br /&gt;
- &amp;#8220;parent&amp;#8221; =&amp;gt; {&amp;#8230;. &amp;#8220;association_ids&amp;#8221; =&amp;gt; [&amp;#8220;1&amp;#8221;,&amp;#8220;2&amp;#8221;,&amp;#8220;4&amp;#8221;]}&lt;/p&gt;
&lt;p&gt;And if you check your script/console and check the associations, they will be there assuming you have groups with id&amp;#8217;s of 1,2, and 4.&lt;/p&gt;
&lt;p&gt;The implications? &lt;strong&gt;If you use these groups for any kind of role based access, a user could assume a group with root/super/power user access!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The lesson?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Protect your attributes!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby:nocontrols:nogutter&quot;&gt; attr_protected :group_ids &lt;/pre&gt;
&lt;p&gt;But! a better idea would be to use:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby:nocontrols:nogutter&quot;&gt; attr_accessible :name, :bio, :etc &lt;/pre&gt;
&lt;p&gt;I hope this has helped you as much as it did me!&lt;/p&gt;
&lt;p&gt;-TheHacker&lt;/p&gt;</description>
      <pubDate>Mon, 07 Apr 2008 17:52:10 -0500</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/43</link>
      <category>rails/security/relationships/activerecord</category>
      <guid>http://beardendesigns.com/blogs/show?43</guid>
    </item>
    <item>
      <title>A Window Into Functional Tests</title>
      <description>&lt;p&gt;So like any ruby blooded human, I create functional tests for my rails applications. However, things don&amp;#8217;t always go as planned; a redirect instead of a success, but where to?, the assigns is right, but the flash was wrong, etc.&lt;/p&gt;
&lt;p&gt;Sometimes you just need a way to peek at what you get back. If you enjoy gouging your eyes out you can do a puts @response or @response.body&lt;/p&gt;
&lt;p&gt;A slightly better alternative is to spit the body to a file and preview it in firefox.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
    tmpfile = File.new(tmpname = 'tmp/test_page.html', &quot;w&quot;)
    tmpfile.puts @response.body
    tmpfile.close
    `firefox #{tmpname}`
&lt;/pre&gt;
&lt;p&gt;Put this after any get, post, etc, and you will get a decent html output of your view (sans stylesheets and valid links) Although nothing is stopping you from outputting to public, running script/server, and viewing it from there.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;If your dealing with redirects, don&amp;#8217;t forget about follow_redirect!&lt;/li&gt;
	&lt;li&gt;If your crossing controllers, use integration tests =)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It&amp;#8217;s not the be all end all of solutions, but it helps for a quick glimpse while fixing tests.&lt;br /&gt;
Hope it helps!&lt;/p&gt;</description>
      <pubDate>Mon, 07 Apr 2008 17:16:36 -0500</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/35</link>
      <category>ruby/rails/functional/testing</category>
      <guid>http://beardendesigns.com/blogs/show?35</guid>
    </item>
    <item>
      <title>Syntax Highlighting for Everyone!</title>
      <description>&lt;p&gt;I recently integrated a javascript based syntax highlighter into this blog. Its very easy to do and quiet useful. Here is a quick rundown. I also go over some alternative methods afterwards.&lt;/p&gt;
&lt;p&gt;The software I ended up using was &lt;a href=&quot;http://code.google.com/p/syntaxhighlighter/&quot;&gt;SyntaxHighlighter&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Instructions can be found &lt;a href=&quot;http://code.google.com/p/syntaxhighlighter/wiki/Usage&quot;&gt;here&lt;/a&gt; just include some files, run the javascript and your gold.&lt;/p&gt;
&lt;p&gt;Once thats in place all you have  todo is invoke:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;xml&quot;&gt;
&amp;lt;pre name=&quot;code&quot; class=&quot;yourlanguage&quot;&amp;gt;
   awesome code snippit here
&lt;/pre&gt;
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Where &amp;#8216;yourlanguage&amp;#8217; is one of ruby,perl,etc&lt;/p&gt;
&lt;p&gt;There is a very useful option which allows you to match line numbers to the file you may be refering to (for example your code may begin on line 10).&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;xml:firstline[10]&quot;&gt;
&amp;lt;pre name=&quot;code&quot; class=&quot;yourlanguage:firstline[10]&quot;&amp;gt;
   awesome code snippit here
&lt;/pre&gt;
&lt;p&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is an example from line 35 from a rails controller (Note the line numbers on the left)&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby:firstline[35]&quot;&gt;
def show
  @owner = User.find(params[:user]) || User.find(1)
  @blogs = Blog.paginate :conditions =&amp;gt; [&quot;(user_id = ?) AND NOT disabled&quot;, @owner.id], 
    :order =&amp;gt; 'updated_at DESC', :per_page =&amp;gt; 5, :page =&amp;gt; params[:page]
end
&lt;/pre&gt;
&lt;p&gt;SyntaxHighlighter supports out of the box:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Csharp&lt;/li&gt;
	&lt;li&gt;C++&lt;/li&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Delphi&lt;/li&gt;
	&lt;li&gt;JavaScript&lt;/li&gt;
	&lt;li&gt;Java&lt;/li&gt;
	&lt;li&gt;Php&lt;/li&gt;
	&lt;li&gt;Python&lt;/li&gt;
	&lt;li&gt;Ruby&lt;/li&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;VisualBasic&lt;/li&gt;
	&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; (Which works well for xhtml files)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In addition you can grab &lt;a href=&quot;http://beardendesigns.com/javascripts/shBrushPerl.js&quot;&gt;shBrushPerl.js&lt;/a&gt; which adds perl support.&lt;/p&gt;
&lt;p&gt;And thats all there is to it! Syntax Highlighting with Client Side Javascript.&lt;/p&gt;
&lt;p&gt;I would also like to point out some other ways to convert code into markup.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://coderay.rubychan.de/&quot;&gt;Coderay&lt;/a&gt; integrates well with ruby on rails.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://qbnz.com/highlighter/&quot;&gt;GeSHi&lt;/a&gt; is a &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; based generator.&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.andre-simon.de/&quot;&gt;Highlight&lt;/a&gt; is a command line (and gtk gui) based app.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the very least Highlight&amp;#8217;s console output can be grabbed and fed into your web application no matter what language. It also has a cool 256 Color Xterm output which is great for piping code into from grep or less.&lt;/p&gt;
&lt;p&gt;Highlight also comes with a slew of existing color schemes in &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; which is nice.&lt;/p&gt;
&lt;p&gt;There are many more highlighters out there, google is your friend.&lt;/p&gt;
&lt;p&gt;Enjoy the shiny colors!&lt;/p&gt;</description>
      <pubDate>Fri, 14 Dec 2007 13:41:22 -0600</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/30</link>
      <category>syntax/highlighting/javascript/ruby/perl/rails</category>
      <guid>http://beardendesigns.com/blogs/show?30</guid>
    </item>
    <item>
      <title>Google-Geocode Gem Woe's</title>
      <description>&lt;p&gt;While using  the very cool &lt;a href=&quot;http://dev.robotcoop.com/Libraries/&quot;&gt;google-geocode&lt;/a&gt; gem for ruby, I ran into a small (read: big) problem.&lt;/p&gt;
&lt;p&gt;Do a search for &amp;#8220;Trinidad&amp;#8221; by itself and you get something like:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;
Communication error: #&amp;lt;REXML::ParseException: Missing end tag for 'AdministrativeAreaName' (got &quot;AdministrativeArea&quot;)
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The problem lies not in the gem but in ruby&amp;#8217;s &lt;span class=&quot;caps&quot;&gt;REXML&lt;/span&gt; and how it deals with the xml google sends back. This only happens when international characters are involved.&lt;/p&gt;
&lt;p&gt;After doing a little googling I saw a patch for rexml which I changed into a monkey patch for google-geocodes helper library rc-rest.&lt;/p&gt;
&lt;p&gt;This monkey patch will solve all your accent mark woe&amp;#8217;s.&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
class RCRest
  
  def get(method, params = {})
    url = make_url method, params

    url.open do |xml|
      body = xml.read

      res = REXML::Document.new Iconv.conv(&quot;UTF-8//Ignore&quot;, 'UTF-8', body)
  
      check_error res

      return parse_response(res)
    end
  rescue IOError, SystemCallError, SocketError, Timeout::Error,
         REXML::ParseException =&amp;gt; e
    raise CommunicationError.new(e)
  rescue OpenURI::HTTPError =&amp;gt; e
    begin
      xml = REXML::Document.new e.io.read
      check_error xml
    rescue REXML::ParseException =&amp;gt; e
    end
    new_e = CommunicationError.new e
    new_e.message &amp;lt;&amp;lt; &quot;\n\nunhandled error:\n#{xml.to_s}&quot;
    raise new_e
  end
  
end
&lt;/pre&gt;

&lt;p&gt;The magical change is the inclusion of Iconv to make &lt;span class=&quot;caps&quot;&gt;REXML&lt;/span&gt; happy.&lt;/p&gt;</description>
      <pubDate>Fri, 14 Dec 2007 11:54:02 -0600</pubDate>
      <link>http://www.beardendesigns.com/blogs/permalink/28</link>
      <category>ruby/rails/gem/geocoding/google</category>
      <guid>http://beardendesigns.com/blogs/show?28</guid>
    </item>
  </channel>
</rss>
