Flowdock

Recent notes

RSS feed
August 6, 2009
4 thanks

Documentation bug

When adding the :target option, the documentation states that you should user :href_options like so:

auto_link(post_body, :href_options => { :target => '_blank' })

However, I could only get it to work using :html instead:

auto_link(post_body, :html => { :target => '_blank' })

I’m using Rails 2.2.2, but I believe that this also happens for more recent version .

August 6, 2009
2 thanks

Second example is correct

@taryneast, the second example is correct. The receiver’s #size limits the result’s.

nachokb

August 6, 2009
1 thank

*Described above

The documentation is referring to the module documentation: ActionController::Cookies

August 4, 2009 - (>= v2.2.1)
1 thank

Binding parameter deprecated in > 2.2

Supplying the binding argument produces a deprecation warning in 2.2 and newer:

DEPRECATION WARNING: The binding argument of #concat is no longer needed. Please remove it from your views and helpers.

August 3, 2009 - (v1_8_6_287)
0 thanks

Using YAML

YAML library must be required.


Example to display an array as yaml formatted output

require 'yaml'

puts [100, [99, 98, 97], 96, 95].to_yaml
August 3, 2009
0 thanks

Separating date and time

Option :datetime_separator can be set too. Default is ‘ — ’

July 31, 2009 - (v1_8_7_72)
1 thank

Example

File.exist?(“/path/to/file_or_dir”)

July 31, 2009 - (v1_8_7_72)
0 thanks

synonym

Synonym for File.exist?

July 30, 2009
2 thanks

How to set default value to NULL

To set default value to NULL you can use change_column method instead, for example:

change_column :suppliers, :qualification, :string, :default => nil

Just make sure you don’t change data type accidentally ;-)

July 30, 2009
0 thanks

can we use both sortable_element and drop_recieving_element on same list

I had a sortable_element that was also a drop_receiving_element. element it’s dropping while dropping element into selected container ,but not element dragging is not viewble to end users. it’s dragging only in sortable list area. but mu droppble contanier is another one.i used scroll => true in sortable_element . but its not working for sorting and dropping element into contianer. could u letme know how to use both methods on same list

July 29, 2009
0 thanks

Paginating grouped records

If you are grouping similar records and paginating you might need to use :group You’ll want to :select only the field you’re collapsing on probably.

Model.count(:select => :attribute, :group => :attribute)

This will return an OrderedHash of your attributes with a count for each.

{"Column Content" => 6, "Another Column's Content" => 8}

You’ll need a second query to pull all of your records out.

July 28, 2009
8 thanks

Return True

As is the case with the before_validation and before_save callbacks, returning false will break the callback chain. For example, the expire_cache_id method will not run if Rails.cache.expire returns false (as it will if the key is not cached with memcache).

Returning False Example (Bad)

after_save :expire_cache_by_name
after_save :expire_cache_by_id

def expire_cache_by_name
  Rails.cache.expire("my_object:name:#{self.name}")
end

def expire_cache_by_id
  Rails.cache.expire("my_object:#{self.id}")
end

Returning True Example (Good)

def expire_cache_by_name
  Rails.cache.expire("my_object:name:#{self.name}")
  return true
end

def expire_cache_by_id
  Rails.cache.expire("my_object:#{self.id}")
  return true
end
July 27, 2009
0 thanks

Include the 'abbrev' module

To get this access to this method you must:

require 'abbrev'
July 27, 2009 - (>= v2.2.1)
3 thanks

Overriding default validation messages

Before Rails 2.2 you could globally customize the default validation error messages by changing AR::Base.default_error_messages. The messages have now been moved to i18n, so to customize them in 2.2 and up, just create a locales/ folder in your config/ folder, copy activerecord/lib/active_record/locale/en.yml (in Rails source) to config/locales/en.yml, and then change the strings inside. As szeryf indicated below, the strings of interest are activerecord.errors.messages.

July 24, 2009
1 thank

Instance method

Please note that this is an instance method, not a class method (which seemed more logical for me and took me a while to see what’s wrong). So, you call it like this:

User.new.from_json '{"id": 1, "name": "DHH"}' # RIGHT!

not like this:

User.from_json '{"id": 1, "name": "DHH"}' # WRONG!
July 23, 2009 - (>= v1.0.0)
0 thanks

Format meaning:

%a - The abbreviated weekday name (“Sun”)

%A - The full weekday name (“Sunday”)

%b - The abbreviated month name (“Jan”)

%B - The full month name (“January”)

%c - The preferred local date and time representation

%d - Day of the month (01..31)

%H - Hour of the day, 24-hour clock (00..23)

%I - Hour of the day, 12-hour clock (01..12)

%j - Day of the year (001..366)

%m - Month of the year (01..12)

%M - Minute of the hour (00..59)

%p - Meridian indicator (“AM” or “PM”)

%S - Second of the minute (00..60)

%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)

%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)

%w - Day of the week (Sunday is 0, 0..6)

%x - Preferred representation for the date alone, no time

%X - Preferred representation for the time alone, no date

%y - Year without a century (00..99)

%Y - Year with century

%Z - Time zone name

%% - Literal “%” character

July 23, 2009 - (>= v1.0.0)
7 thanks

Format meaning

%a - The abbreviated weekday name (“Sun”)

%A - The full weekday name (“Sunday”)

%b - The abbreviated month name (“Jan”)

%B - The full month name (“January”)

%c - The preferred local date and time representation

%d - Day of the month (01..31)

%H - Hour of the day, 24-hour clock (00..23)

%I - Hour of the day, 12-hour clock (01..12)

%j - Day of the year (001..366)

%m - Month of the year (01..12)

%M - Minute of the hour (00..59)

%p - Meridian indicator (“AM” or “PM”)

%S - Second of the minute (00..60)

%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)

%W - Week number of the current year, starting with the first Monday as the firstday of the first week (00..53)

%w - Day of the week (Sunday is 0, 0..6)

%x - Preferred representation for the date alone, no time

%X - Preferred representation for the time alone, no date

%y - Year without a century (00..99)

%Y - Year with century

%Z - Time zone name

%% - Literal “%” character

July 18, 2009
2 thanks

Sanitize in controllers, models, or libs -- *with* options

A Follow-up to k776’s note. If you want to specify tags or attributes, you should change your initializer to:

class String
  def sanitize(options={})
    ActionController::Base.helpers.sanitize(self, options)
  end
end

Then you can call it from any string like so:

'string'.sanitize(:tags => %w(table td tr), :attributes => %w(style id))
July 17, 2009
0 thanks

Like Groovy Expando

If you’re coming from Groovy/Grails: this is called an Expando in Groovy.

July 17, 2009
1 thank

Highlight keywords in a text

Case-insensitive

keywords.inject(text) { |text, keyword| text.gsub(/(#{keyword})/i, "<strong>\\1</strong>") }

<strong> can be replace by whatever HTML tag you want for hightlighting (<b>, <i>, …)

July 16, 2009
0 thanks

Also works with other markup

such as XML, not only HTML as suggested in the text.

July 16, 2009
0 thanks

Usage example

Usage example:

cube = lambda {|x| x * x * x } 
cube.call(3)  # => 27
cube.call(6)  # => 216
July 15, 2009
0 thanks

Time.now in views.

Be careful if you use Time.now in views with time zone support enabled, as this will not actually do the time zone conversion.

Instead, use Time.zone.now.

July 14, 2009
2 thanks

Not for floats

You should use assert_in_delta when comparing floating-point numbers.

July 13, 2009
1 thank

Any base logarithm

Using basic arithmetic you can get logarithm with any base:

def log_with_base base, num
  Math.log(num) / Math.log(base)
end

Examples:

>> log_with_base 2, 10
=> 3.32192809488736
>> log_with_base 2, 2
=> 1.0
>> log_with_base 2, 4
=> 2.0
>> log_with_base 2, 16
=> 4.0
>> log_with_base 4, 16
=> 2.0
July 13, 2009
0 thanks

Any base logarithm

Using basic arithmetic you can get logarithm with any base:

def log_with_base base, num
  Math.log(num) / Math.log(base)
end

Examples:

>> log_with_base 2, 10
=> 3.32192809488736
>> log_with_base 2, 2
=> 1.0
>> log_with_base 2, 4
=> 2.0
>> log_with_base 2, 16
=> 4.0
>> log_with_base 4, 16
=> 2.0
July 8, 2009
0 thanks

Change Column. pt-br

Em sua migration escreva da seguinte forma:

Exemplo de uso.

def self.up
  change_column :sua_tabela, :seu_campo, :seu_tipo_campo
end
July 8, 2009
1 thank

Change Column.

Into your migration write the follow:

Using Exemple

def self.up
  change_column :yourtable, :your_field, :your_type_field
end
July 8, 2009
1 thank

This is an alias

Please comment under the real method instead: find_index

July 8, 2009 - (<= v1_8_7_72)
5 thanks

Using block version in Ruby < 1.8.7

The block usage was added in 1.8.7, so to get the same functionality in an earlier version of Ruby, you need to utilize the find method.

Here is a quick example:

match = list.find { |l| l.owner == myself }
match_index = list.index(match)

If you do some gymnastics, you can have it on one line without extra variables:

match_index = list.index(list.find { |l| l.owner == myself })