A custom Swedish time_ago_in_words implementation

Ruby posted 8 days ago by christian

This is an easily customizable time_ago_in_words implementation in Swedish that will produce this output:

   1  < 5 minuter
   2  < 45 minuter
   3  < 1 timme
   4  > 2 timmar
   5  > 1 dag, 2 timmar
   6  20 April 2010 (if more than 31 days ago)

The code:

   1  class Time
   2    def time_ago_in_words
   3      words = ''
   4      timestamp = self
   5      if timestamp
   6        time_until = (Time.now.to_datetime - timestamp.to_datetime)
   7        days = time_until.to_i
   8        hours, minutes, seconds, frac = Date.day_fraction_to_time(time_until)
   9        hours = hours % 24
  10  
  11        if days == 0
  12          if hours < 1
  13            words = case minutes
  14              when 0..4 then "< 5 minuter"
  15              when 5..14 then "< 15 minuter"
  16              when 15..29 then "< 30 minuter"
  17              when 30..44 then "< 45 minuter"
  18              when 45..59 then "< 1 timme"
  19            end
  20          else
  21            words = "> #{hours} #{'timme'.inflect(hours)}"
  22          end
  23        elsif days < 31
  24          words = "> #{days} #{'dag'.inflect(days)}"
  25          if hours > 0
  26            words += ", #{hours} #{'timme'.inflect(hours)}"
  27          end
  28        else
  29          words = timestamp.l(:format => :daymonthyear)
  30        end
  31      end
  32      words
  33    end
  34  end

You’ll need this for inflections:

   1  class String
   2    def inflect(count)
   3      count > 1 ? ActiveSupport::Inflector.pluralize(self) : self
   4    end
   5  end

Add this to config/locales/sv-SE.yml for localizing the date in Swedish:

   1  time:
   2      formats:
   3        daymonthyear: "%d %B %Y"

Add this to config/initializers/inflections.rb for Swedish inflections:

   1  ActiveSupport::Inflector.inflections do |inflect|
   2    inflect.irregular 'dag', 'dagar'
   3    inflect.irregular 'vecka', 'veckor'
   4    inflect.irregular 'månad', 'månader'
   5    inflect.irregular 'timme', 'timmar'
   6    inflect.irregular 'minut', 'minuter'
   7    inflect.irregular 'sekund', 'sekunder'
   8  end

Tagged time_ago_in_words, ruby, rails, time, swedish, svenska, inflections

How to fetch delicious data with Hpricot and OpenURI

Ruby posted 15 days ago by christian

The code:

   1  class Delicious
   2    class << self
   3      def tag(username, name, count = 15)
   4        links = []
   5        url = "http://feeds.delicious.com/v2/rss/#{username}/#{name}?count=#{count}"
   6        feed = Hpricot(open(url))
   7  
   8        feed.search("item").each do |i|
   9          item = OpenStruct.new
  10          item.link = i.at('link').next.to_s
  11          item.title = i.at('title').innerHTML
  12          item.description  = i.at('description').innerHTML rescue nil
  13  
  14          links << item
  15        end
  16  
  17        links
  18      end
  19    end
  20  end

Usage:

   1  # Return last 15 items tagged with business and news from jebus's account:
   2  Delicious.tag 'jebus', 'business+news', 15

Returns an array of items.

Tagged delicious, hpricot, ruby, rss, feed

How to prevent double form submissions with jQuery

JavaScript posted 21 days ago by christian

This snippet will disable the submit button after the user clicks on it:

   1  $('form').submit(function(){
   2      var $button = $('input[type=submit]', this);
   3      $button.attr('disabled', 'disabled');
   4      $button.attr('value', $button.attr('value') + "...");
   5  });

For Ajax forms, you need to enable the button again in your code.

Tagged ajax, submit, jquery

Fix for "530 5.7.0 Must issue a STARTTLS command first" and Ruby mail gem

Ruby posted 25 days ago by christian

I’m using the mail gem and received this error when sending emails:

   1  530 5.7.0 Must issue a STARTTLS command first

The fix was to first install the tlsmail gem:

   1  gem install tlsmail

Then I had to add this to config/initializers/email.rb:

   1  require 'tlsmail' 
   2  Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

Tagged mail, tlsmail, smtp, email

How to support optional variables in ERB templates

Ruby posted about 1 month ago by christian

This will give the variable “something” a value of “nothing” if it’s not defined:

   1  <%
   2    something = 'nothing' if local_assigns.has_key? :something
   3  %>

Tagged locals, erb, optional