Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Chris Anderson http://jchris.mfdz.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Speeding up your acts_as_tokenized inserts

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!


Index: vendor/plugins/token_generator/lib/token_generator.rb
===================================================================
--- vendor/plugins/token_generator/lib/token_generator.rb       (revision 1233)
+++ vendor/plugins/token_generator/lib/token_generator.rb       (working copy)
@@ -1,7 +1,7 @@
 module TokenGenerator
   def generate_token(size = 12, &validity)
     begin
-      token = Digest::MD5.hexdigest("#{inspect}#{Time.now}").first(size)
+      token = Digest::MD5.hexdigest("#{inspect}#{Time.now}#{rand}").first(size)
     end while !validity.call(token) if block_given?
     

recursive Rails style serialization for javascript objects

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.

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.

The original Rails recursive Javascript object serializer.

var serializer = {
 
  serialize : function(object) {
    var values = []; 
    var prefix = '';
    
    values = this.recursive_serialize(object, values, prefix);
    
    param_string = values.join('&');
    return param_string;
  },
  
  recursive_serialize : function(object, values, prefix) {
    for (key in object) {
      if (typeof object[key] == 'object') {
        
        if (prefix.length > 0) {
          prefix += '['+key+']';         
        } else {
          prefix += key;
        }
        
        values = this.recursive_serialize(object[key], values, prefix);
        
        prefixes = prefix.split('[');
        
        if (prefixes.length > 1) {
          prefix = prefixes.slice(0,prefixes.length-1).join('[');
        } else {
          prefix = prefixes[0];
        }
        
      } else {
        value = encodeURIComponent(object[key]);
        if (prefix.length > 0) {
          prefixed_key = prefix+'['+key+']'          
        } else {
          prefixed_key = key
        }
        prefixed_key = encodeURIComponent(prefixed_key);
        if (value) values.push(prefixed_key + '=' + value);
      }
    }
    return values;
  }
}


Usage:

payload = new Object;
payload.comment = new Object;
payload.comment.title = "The Title";
payload.comment.body = "The body of the post.";
post_string = serializer.serialize(payload);


Result:

comment%5Btitle%5D=The%20Title&comment%5Bbody%5D=The%20body%20of%20the%20post.


Which gives you this in Rails:

params[:comment] #=>
{:title => "The Title", :body => "The body of the post."}

List files that would be changed in a Capistrano deploy

// description of your code here
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.


desc "List the files that would be updated or changed in a deploy."
task :svn_st_up, :roles => :app do
  run "cd #{current_path} && " + "svn st -u"
end


from my blog

Mongrel Spinner and Restart tasks for Capistrano

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.

h2. Spinner

desc <<-DESC
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.
DESC
task :spinner, :roles => :app do
  application_port = xxxx #get this from your friendly sysadmin
  run "mongrel_rails start -e production -p #{application_port} -d -c #{current_path}"
end


h2. Restart

desc "Restart the web server"
task :restart, :roles => :app do
  begin
    run "cd #{current_path} && mongrel_rails restart"
  rescue RuntimeError => e
    puts e
    puts "Probably not a big deal, so I'll just keep trucking..."
  end
end


There's a more complete article on my blog.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS