Register now and start sharing your code snippets.

Time ago in words (minutes, hours, days, weeks, months ago in words)

Ruby posted about 1 month ago by christian

   1  def minutes_in_words(timestamp)
   2      minutes = (((Time.now - timestamp).abs)/60).round
   3      
   4      return nil if minutes < 0
   5      
   6      case minutes
   7        when 0..4            then '&lt; 5 minutes'
   8        when 5..14           then '&lt; 15 minutes'
   9        when 15..29          then '&lt; 30 minutes'
  10        when 30..59          then '&gt; 30 minutes'
  11        when 60..119         then '&gt; 1 hour'
  12        when 120..239        then '&gt; 2 hours'
  13        when 240..479        then '&gt; 4 hours'
  14        when 480..719        then '&gt; 8 hours'
  15        when 720..1439       then '&gt; 12 hours'
  16        when 1440..11519     then '&gt; ' << pluralize((minutes/1440).floor, 'day')
  17        when 11520..43199    then '&gt; ' << pluralize((minutes/11520).floor, 'week')
  18        when 43200..525599   then '&gt; ' << pluralize((minutes/43200).floor, 'month')  
  19        else                      '&gt; ' << pluralize((minutes/525600).floor, 'år')
  20      end
  21    end

There are also similar implementations:
  1. http://www.actsasflinn.com/articles/2007/04/10/time-ago-method-for-ruby-on-rails
  2. http://timeago.yarp.com/
Tagged month, week, hour, minutes, words, rails, ruby

How to create a daemon process using Ruby and the daemons RubyGem

Ruby posted 2 months ago by christian

This snippets shows you how to create a daemon process out of an ordinary Ruby script.

First you’ll need the daemons gem:

   1  gem install daemons

Then you’ll need the daemon script, for example daemon.rb:

   1  require 'rubygems'
   2  require 'daemons'
   3  
   4  pwd = Dir.pwd
   5  file = 'lib/background_service.rb'
   6  
   7  Daemons.run_proc(
   8    'background_service', # name of daemon
   9    :dir_mode => :normal,
  10    :dir => File.join(pwd, 'tmp/pids'), # directory where pid file will be stored
  11    :backtrace => true,
  12    :monitor => true,
  13    :log_output => true
  14  ) do
  15    Dir.chdir(pwd)
  16    exec "ruby #{file}"
  17  end

Change the file variable to point to the script you want to daemonize and your good to go.

You can now execute the daemon.rb script without parameters to get a list of available commands for controlling the daemon process:

   1  ERROR: no command given
   2  
   3  Usage: lib/background_service.rb <command> <options> -- <application options>
   4  
   5  * where <command> is one of:
   6    start         start an instance of the application
   7    stop          stop all instances of the application
   8    restart       stop all instances and restart them afterwards
   9    run           start the application and stay on top
  10    zap           set the application to a stopped state
  11  
  12  * and where <options> may contain several of the following:
  13  
  14      -t, --ontop                      Stay on top (does not daemonize)
  15      -f, --force                      Force operation
  16  
  17  Common options:
  18      -h, --help                       Show this message
  19          --version                    Show version

Tagged ruby, daemons, daemon, process, background

How to use named_scope in Rails

Ruby posted 2 months ago by christian

Simple example of how to use the named_scope feature:

   1  class Feed < ActiveRecord::Base
   2   
   3    named_scope :active, :conditions => "(active = 1)"
   4    named_scope :stale,  :conditions => ["last_updated > ?", 30.minutes.ago.to_s(:db)]

Usage:

   1  Feed.active # return the active feeds

Chaining is also possible:

   1  Feed.active.stale # return the feeds that need to be updated

Tagged named_scope, rails, activerecord, ruby

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

HTML (Rails) posted 3 months ago by christian

   1  <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden" />

or

   1  <%= token_tag %> 

Tagged authenticity, token, ruby, rails

How to add a watermark to images using MiniMagick, attachment_fu and Ruby

Ruby posted 3 months ago by christian

Use this snippet to add a watermark to an image, after the image is uploaded:

   1  class Image
   2    .
   3    has_attachment ...
   4    .
   5    .
   6    after_attachment_saved do |record|    
   7      # Don't add watermarks to thumbnails
   8      if record.thumbnail.nil?
   9        full_path = File.join(RAILS_ROOT, 'public/', record.public_filename)
  10        
  11        img = MiniMagick::Image.from_file(full_path)
  12        
  13        width = img[:width]
  14        height = img[:height]
  15        
  16        if width > 150 && height > 150
  17          img.combine_options do |c|
  18            c.gravity 'SouthWest'
  19            # This is RAILS_ROOT/images/watermark.gif
  20            c.draw "image Over 0,0 0,0 \"images/watermark.gif\""
  21          end
  22  
  23          img.write(full_path)
  24          
  25        end
  26      end
  27      
  28    end

Note that after_attachment_saved is a callback added by attachment_fu, use after_save if you’re not using attachment_fu.

Tagged attachment_fu, mini_magick, ruby, watermark