<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Jchris's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 22:33:53 GMT</pubDate>
    <description>DZone Snippets: Jchris's Code Snippets</description>
    <item>
      <title>Speeding up your acts_as_tokenized inserts</title>
      <link>http://snippets.dzone.com/posts/show/4565</link>
      <description>You might want to apply this diff to your token_generator plugin. Otherwise for saving identical models the minimum time taken is one per second. Not fun!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Index: vendor/plugins/token_generator/lib/token_generator.rb&lt;br /&gt;===================================================================&lt;br /&gt;--- vendor/plugins/token_generator/lib/token_generator.rb       (revision 1233)&lt;br /&gt;+++ vendor/plugins/token_generator/lib/token_generator.rb       (working copy)&lt;br /&gt;@@ -1,7 +1,7 @@&lt;br /&gt; module TokenGenerator&lt;br /&gt;   def generate_token(size = 12, &amp;validity)&lt;br /&gt;     begin&lt;br /&gt;-      token = Digest::MD5.hexdigest("#{inspect}#{Time.now}").first(size)&lt;br /&gt;+      token = Digest::MD5.hexdigest("#{inspect}#{Time.now}#{rand}").first(size)&lt;br /&gt;     end while !validity.call(token) if block_given?&lt;br /&gt;     &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Sep 2007 21:01:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4565</guid>
      <author>jchris (Chris Anderson)</author>
    </item>
    <item>
      <title>recursive Rails style serialization for javascript objects</title>
      <link>http://snippets.dzone.com/posts/show/2426</link>
      <description>Have you ever wanted to post from your javascript program to a Rails app so that the params are available to your application in a properly nested hash? Prototype makes this easy for form elements with it's Form Serializer, but it makes no provision for standard javascript objects.&lt;br /&gt;&lt;br /&gt;Suffer no longer. With the following code you can serialize arbitrarily nested objects (eg, the sort of thing you get when you parse a JSON statement), so that it is ready to be posted via http to a Rails app.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://jchris.mfdz.com/articles/2006/08/15/rails-style-recursive-javacript-object-serialization"&gt;The original Rails recursive Javascript object serializer.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var serializer = {&lt;br /&gt; &lt;br /&gt;  serialize : function(object) {&lt;br /&gt;    var values = []; &lt;br /&gt;    var prefix = '';&lt;br /&gt;    &lt;br /&gt;    values = this.recursive_serialize(object, values, prefix);&lt;br /&gt;    &lt;br /&gt;    param_string = values.join('&amp;');&lt;br /&gt;    return param_string;&lt;br /&gt;  },&lt;br /&gt;  &lt;br /&gt;  recursive_serialize : function(object, values, prefix) {&lt;br /&gt;    for (key in object) {&lt;br /&gt;      if (typeof object[key] == 'object') {&lt;br /&gt;        &lt;br /&gt;        if (prefix.length &gt; 0) {&lt;br /&gt;          prefix += '['+key+']';         &lt;br /&gt;        } else {&lt;br /&gt;          prefix += key;&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        values = this.recursive_serialize(object[key], values, prefix);&lt;br /&gt;        &lt;br /&gt;        prefixes = prefix.split('[');&lt;br /&gt;        &lt;br /&gt;        if (prefixes.length &gt; 1) {&lt;br /&gt;          prefix = prefixes.slice(0,prefixes.length-1).join('[');&lt;br /&gt;        } else {&lt;br /&gt;          prefix = prefixes[0];&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;      } else {&lt;br /&gt;        value = encodeURIComponent(object[key]);&lt;br /&gt;        if (prefix.length &gt; 0) {&lt;br /&gt;          prefixed_key = prefix+'['+key+']'          &lt;br /&gt;        } else {&lt;br /&gt;          prefixed_key = key&lt;br /&gt;        }&lt;br /&gt;        prefixed_key = encodeURIComponent(prefixed_key);&lt;br /&gt;        if (value) values.push(prefixed_key + '=' + value);&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    return values;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;payload = new Object;&lt;br /&gt;payload.comment = new Object;&lt;br /&gt;payload.comment.title = "The Title";&lt;br /&gt;payload.comment.body = "The body of the post.";&lt;br /&gt;post_string = serializer.serialize(payload);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Result:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;comment%5Btitle%5D=The%20Title&amp;comment%5Bbody%5D=The%20body%20of%20the%20post.&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Which gives you this in Rails:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;params[:comment] #=&gt;&lt;br /&gt;{:title =&gt; "The Title", :body =&gt; "The body of the post."}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Aug 2006 08:13:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2426</guid>
      <author>jchris (Chris Anderson)</author>
    </item>
    <item>
      <title>List files that would be changed in a Capistrano deploy</title>
      <link>http://snippets.dzone.com/posts/show/2058</link>
      <description>// description of your code here&lt;br /&gt;Sometimes you want to know before hand which files have been added or changed since your last deploy. Maybe you have to run a migration. Assuming you're using subversion for you source control, this task will list the pending changes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "List the files that would be updated or changed in a deploy."&lt;br /&gt;task :svn_st_up, :roles =&gt; :app do&lt;br /&gt;  run "cd #{current_path} &amp;&amp; " + "svn st -u"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://jchris.mfdz.com/articles/2006/05/09/deploying-with-capistrano-and-mongrel-on-planet-argon"&gt;from my blog&lt;/a&gt;</description>
      <pubDate>Wed, 17 May 2006 10:21:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2058</guid>
      <author>jchris (Chris Anderson)</author>
    </item>
    <item>
      <title>Mongrel Spinner and Restart tasks for Capistrano</title>
      <link>http://snippets.dzone.com/posts/show/2057</link>
      <description>These tasks should work with the default deploy.rb file. I assume you've at least read the Capistrano manual at rubyonrails.com, also, you should get your app running on mongrel first with manual booting and restarting before you try to automate.&lt;br /&gt;&lt;br /&gt;h2. Spinner&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc &lt;&lt;-DESC&lt;br /&gt;Spinner is run by the default cold_deploy task. Instead of using script/spinner, we're just gonna rely on Mongrel to keep itself up.&lt;br /&gt;DESC&lt;br /&gt;task :spinner, :roles =&gt; :app do&lt;br /&gt;  application_port = xxxx #get this from your friendly sysadmin&lt;br /&gt;  run "mongrel_rails start -e production -p #{application_port} -d -c #{current_path}"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;h2. Restart&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Restart the web server"&lt;br /&gt;task :restart, :roles =&gt; :app do&lt;br /&gt;  begin&lt;br /&gt;    run "cd #{current_path} &amp;&amp; mongrel_rails restart"&lt;br /&gt;  rescue RuntimeError =&gt; e&lt;br /&gt;    puts e&lt;br /&gt;    puts "Probably not a big deal, so I'll just keep trucking..."&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;There's a more complete article &lt;a href="http://jchris.mfdz.com/articles/2006/05/09/deploying-with-capistrano-and-mongrel-on-planet-argon"&gt;on my blog.&lt;/a&gt;</description>
      <pubDate>Wed, 17 May 2006 10:18:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2057</guid>
      <author>jchris (Chris Anderson)</author>
    </item>
  </channel>
</rss>
