суббота, 29 августа 2009 г.

Rails Code Snippets



Hi,

In my rails application I usually copy some usefull scripts from my previous project.
May be it's very interesting for you.

def stylesheet(*args)
content_for(:header) { stylesheet_link_tag(*args.map(&:to_s)) }
end

def javascript(*args)
args = args.map { |arg| arg == :defaults ? arg : arg.to_s }
content_for(:header) { javascript_include_tag(*args) }
end

def script(&block)
content = capture(&block)
concat("", block.binding)
end

def content_for_script(place = :header, &block)
content = capture(&block)
content_for place do
script(&block)
end
end

def google_analytics(code)
%(
script type='text/javascript'
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
var pageTracker = _gat._getTracker("#{code}");
pageTracker._trackPageview();
} catch(err){}
/script

)
end


class String
def self.random_alphanumeric(size=64)
s = ""
size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
s
end
end



See you.

Комментариев нет: