Flowdock

Notes posted by grosser

RSS feed
June 17, 2011 - (>= v3.0.0)
0 thanks

require it

require ‘action_dispatch/testing/test_process’

September 9, 2009
3 thanks

Will discard any order option

order_by(:created_at).find_each == FAIL!!!

class ActiveRecord::Base
  # normal find_each does not use given order but uses id asc
  def self.find_each_with_order(options={})
    raise "offset is not yet supported" if options[:offset]

    page = 1
    limit = options[:limit] || 1000

    loop do
      offset = (page-1) * limit
      batch = find(:all, options.merge(:limit=>limit, :offset=>offset))
      page += 1

      batch.each{|x| yield x }

      break if batch.size < limit
    end
  end
end
June 6, 2009
9 thanks

add index with :quiet=>true option for indices that are possibly already added

# Allows you to specify indices to add in a migration that will only be created if they do not # already exist, or to remove indices only if they already exist with :quiet=>true module ActiveRecord::ConnectionAdapters::SchemaStatements

def add_index_with_quiet(table_name, column_names, options = {})
  quiet = options.delete(:quiet)
  add_index_without_quiet table_name, column_names, options
rescue
  raise unless quiet and $!.message =~ /^Mysql::Error: Duplicate key name/i
  puts "Failed to create index #{table_name} #{column_names.inspect} #{options.inspect}"
end
alias_method_chain :add_index, :quiet

def remove_index_with_quiet(table_name, column_names, options = {})
  quiet = options.delete(:quiet)
  raise "no options allowed for remove_index, except quiet with this hack #{__FILE__}:#{__LINE__}" unless options.empty?
  remove_index_without_quiet table_name, column_names
rescue
  raise unless quiet and $!.message =~ /^Mysql::Error: Can't DROP/i
  puts "Failed to drop index #{table_name} #{column_names.inspect}"
end
alias_method_chain :remove_index, :quiet

end

February 24, 2009 - (>= v2.2.1)
5 thanks

ATM does not work in Rails 2.3 Edge

add to test/spec_helper to make it work again…

#spec_helper / test_helper
include ActionController::TestProcess
November 6, 2008
4 thanks

current_url

exact url from browser window:

def current_url
  url_for :only_path=>false,:overwrite_params=>{}
end
October 25, 2008
3 thanks
October 25, 2008
2 thanks

Alternative: use 1000.humanize

1.humanize == “1″ 1000000.humanize == “1.000.000″ 1000.12345.humanize == “1.000,12″

http://pragmatig.wordpress.com/2008/10/25/numbers-for-humans-humanize-for-numeric/

October 20, 2008
3 thanks

number_to_euro

in small cells:

12-->
12def number_to_euro(amount)
  number_to_currency(amount,:unit=>'').gsub(' ',nbsp)
end
October 17, 2008
1 thank

Use encode64!

b64encode will print to the commandline, what a useful feature…

September 21, 2008
1 thank

resourceful

auto_discovery_link_tag :atom, movies_url(:format=>‘atom’), :title=>‘New movies’

to produce the feed:

respond_to do |wants|
  wants.html
  wants.atom {render :action=>'index',:layout=>false}
end
September 18, 2008
1 thank

unobstrusive label tag

just use

label_tag('a_a','a_a') 

and it works, just not ment for pure decorative labels :)

September 17, 2008
0 thanks

removes underscores -> do not use for images etc

example

#does not work
label_tag('aa'+image_tag('x_x.gif')) 
August 4, 2008
3 thanks

Detailed messages for a nested model

Detailed messages for a nested model

<%@address = @order.address%>
<%=error_messages_for :address%>
August 1, 2008
2 thanks

email_to('xxx@xxx','xxx@xxx',:encode=>'javascript') does NOT work

as i always want the email in the link text, email_to does not help me…

so here comes the rescue!

#http://unixmonkey.net/?p=20
# Takes in an email address and (optionally) anchor text,
# its purpose is to obfuscate email addresses so spiders and
# spammers can't harvest them.
def js_antispam_email_link(email, linktext=email)
  user, domain = email.split('@')
  # if linktext wasn't specified, throw email address builder into js document.write statement
  linktext = "'+'#{user}'+'@'+'#{domain}'+'" if linktext == email 
  out =  "<noscript>#{linktext} #{user}(ät)#{domain}</noscript>\n"
  out += "<script language='javascript'>\n"
  out += "  <!--\n"
  out += "    string = '#{user}'+'@'+''+'#{domain}';\n"
  out += "    document.write('<a href='+'m'+'a'+'il'+'to:'+ string +'>#{linktext}</a>'); \n"
  out += "  //-->\n"
  out += "</script>\n"
  return out
end
July 28, 2008 - (v2.1.0)
0 thanks

Bug? does not encode options

polymorphic_path(item,options) =polymorphic_path(item)+hash_to_url_query(options)

def hash_to_url_query(hash)
  url = []
  hash.each{|k,v| url << "#{k}=#{v}"}
  "=" + (url * '&')
end
July 25, 2008 - (v1.0.0 - v2.1.0)
4 thanks

select_options_tag - no more worries...

no more explicit options_for_select calls..

def select_options_tag(name='',select_options={},options={})
  #set selected from value
  selected = ''
  unless options[:value].blank?
    selected = options[:value]
    options.delete(:value)
  end
  select_tag(name,options_for_select(select_options,selected),options)
end

select_options_tag(‘name’,[[‘oh’,‘no’]],:value=>‘no’)

July 10, 2008
0 thanks

watch out for urls with &

image_tag(‘x.com/aaa?a=1&b=2’) = x.com/aaa?a=1&amp;b=2