you are in: codestackercodes [RSS] → tag: ruby [RSS]

i18n in labels Delicious Email

rails 2.3.4 doen’t have i18n support in labels
http://lawrencesong.net/2009/04/i18n-label-in-rails-monkey-patch/

show/hide lines
   1  module ActionView 
   2    module Helpers 
   3      class InstanceTag 
   4        def to_label_tag_with_i18n(text = nil, options = {}) 
   5          text ||= object.class.human_attribute_name(method_name) if object.class.respond_to?(:human_attribute_name) 
   6   
   7          to_label_tag_without_i18n(text, options) 
   8        end 
   9   
  10        alias_method_chain :to_label_tag, :i18n 
  11      end 
  12    end 
  13  end
created by leozera — 22 November 2009 — get a short url — tags: helper rails ruby embed

paperclip refresh all images Delicious Email

run this task

show/hide lines
   1  rake paperclip:refresh class=MyClass
created by leozera — 26 October 2009 — get a short url — tags: paperclip rails ruby embed

use default_scope to set up default find conditions such as order Delicious Email

show/hide lines
   1  class MyModel < ActiveRecord::Base
   2    default_scope :order => 'created_at desc'
   3  end
created by leozera — 04 October 2009 — get a short url — tags: rails ruby embed

humanized attributes Delicious Email

show/hide lines
   1  class User < ActiveRecord::Base
   2  
   3    HUMANIZED_ATTRIBUTES = {
   4      :email => "E-mail address"
   5    }
   6  
   7    def self.human_attribute_name(attr)
   8      HUMANIZED_ATTRIBUTES[attr.to_sym] || super
   9    end
  10  
  11  end
created by leozera — 22 August 2009 — get a short url — tags: rails ruby embed

paperclip ie validation Delicious Email

show/hide lines
   1  validates_attachment_content_type :image,
   2    :content_type => [
   3      'image/jpeg',
   4      'image/pjpeg', # for progressive jpeg (IE mine-type for regular jpeg) 
   5      'image/png',
   6      'image/x-png', # IE mine-type for PNG
   7      'image/gif'
   8  ]
created by leozera — 16 August 2009 — get a short url — tags: ie paperclip rails ruby embed

succ method Delicious Email

from: http://www.ruby-doc.org/core/classes/String.html#M000795

show/hide lines
   1  >> "abb".succ
   2  => "abc"
   3  >> "1".succ
   4  => "2"
   5  >> "1999zzz".succ
   6  => "2000aaa"
   7  >> 
created by leozera — 01 July 2009 — get a short url — tags: ruby embed

hirb example Delicious Email

to install:
gem install cldwalker-hirb —source http://gems.github.com

show/hide lines
   1  >> require 'hirb'
   2  => []
   3  >> Hirb.enable
   4  => nil
   5  >> Exam.find(:all, :limit => 5)
   6  +----+----------+----------+----------+----------+---------+----------+------+
   7  | id | title    | exam     | creat... | updat... | user_id | categ... | hits |
   8  +----+----------+----------+----------+----------+---------+----------+------+
   9  | 1  | Simul... | Simul... | 2009-... | 2009-... | 1       | 1        | 152  |
  10  | 2  | Simul... | Simul... | 2009-... | 2009-... | 1       | 1        | 143  |
  11  | 3  | Espec... | Espec... | 2009-... | 2009-... | 1       | 1        | 1089 |
  12  | 4  | Espec... | Espec... | 2009-... | 2009-... | 1       | 1        | 80   |
  13  | 5  | Espec... | Espec... | 2009-... | 2009-... | 1       | 1        | 40   |
  14  +----+----------+----------+----------+----------+---------+----------+------+
  15  5 rows in set
  16  >>
created by leozera — 01 July 2009 — get a short url — tags: gem hirb rails ruby embed

listing files in a directory with rails Delicious Email

show/hide lines
   1  @files = Dir.glob("public/files/*")
   2  
   3  for file in @files
   4      puts file
   5  end
created by leozera — 28 June 2009 — get a short url — tags: rails ruby embed

Google::Translate, A Simple API In Ruby Delicious Email

install:
sudo gem install google_translate

show/hide lines
   1  require 'rubygems'
   2  require 'google_translate'
   3  
   4  tr = Google::Translate.new
   5  
   6  # from English to Spanish
   7  tr.translate :from => "en", :to => "es", :text => "Hello, World!"
   8   => Hola, Mundo!
created by leozera — 20 April 2009 — get a short url — tags: gem google ruby embed
Displaying records 1 - 10 of 55