Notes posted by grosser
RSS feedrequire it
require ‘action_dispatch/testing/test_process’
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
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
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
current_url
exact url from browser window:
def current_url url_for :only_path=>false,:overwrite_params=>{} end
Also for numeric
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/
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/
number_to_euro
in small cells:
12 € --> 12 € def number_to_euro(amount) number_to_currency(amount,:unit=>'€').gsub(' ',nbsp) end
Use encode64!
b64encode will print to the commandline, what a useful feature…
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
unobstrusive label tag
just use
label_tag('a_a','a_a')
and it works, just not ment for pure decorative labels :)
removes underscores -> do not use for images etc
example
#does not work label_tag('aa'+image_tag('x_x.gif'))
Detailed messages for a nested model
Detailed messages for a nested model
<%@address = @order.address%> <%=error_messages_for :address%>
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
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
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’)
watch out for urls with &
image_tag(‘x.com/aaa?a=1&b=2’) = x.com/aaa?a=1&b=2