Posted by
kwi | Posted in
Rails, Tricks | Posted on 28-03-2011 | Comments : 5
Hello there! Almost 10 months without any post due to a very busy period: I moved from Paris to San Francisco.
Ok, so now facts has been said, this will hopefully change! I plan to write a new series of Rails3 posts, starting with a quick one on the new way Rails handles notifications.
The new version of ActiveSupport shipped with Rails brings along a new notification system which is heavily used by Rails3 internarlly. Rails doesn’t write directly to logs anymore, instead of that, it publishes a notification which can be caught by any observers.
In production, Rails will by default publish deprecation warnings through this notification system. Last week I was looking for a way to play with that and didn’t find a clear example on the web, so here is a small snippet of code if you are also looking for a nice way to log your deprecation warnings:
# In config/initializers/deprecations_logger.rb
DeprecationLogger = Logger.new(Rails.root.join('log/deprecations.log'))
ActiveSupport::Notifications.subscribe(/deprecation/) do |type, date,b,c, event|
DeprecationLogger.info("#{date} - #{event[:message]}")
end
Posted by
kwi | Posted in
Rails | Posted on 27-02-2010 | Comments : 0
Here is a little thing I find good to know ! Maybe you are curious too and you want to know how rails 2.3 know if your strings are html safe or not.
In order to prevent XSS injections, Rails 2.3 implement a way to determine if a string is safe : It’s just a little extension of the core string class given by active support (output_safety.rb).
This extension add a @_rails_html_safe instance variable to the majority of each string created by rails helper in your application !! (I find this way kind of brutal personally)
This variable is a boolean, and if it’s true, your string is HTML safe. So on each concatenation your string will be marked as html safe or not.
If you want to mark yourself a string as safe, use the method : html_safe!
And if you want to know if a string is safe, use : htm_safe?
ruby-1.8.7-p249 > helper.tag('br').html_safe?
=> true
ruby-1.8.7-p249 > "<script>Not safe</script>".html_safe?
=> nil
ruby-1.8.7-p249 > "<script>Not safe</script>".html_safe!.html_safe?
=> true
ruby-1.8.7-p249 > Marshal::dump(helper.tag('br'))
=> "\004\bI\"\v<br />\006:\026@_rails_html_safeT"
# Here you can see the @_rails_html_safe variable with the string.
Be careful, this code completely change in Rails3 and the little I seen, it’s a lot cleaner
Posted by
kwi | Posted in
Ruby, Tricks | Posted on 08-12-2009 | Comments : 1
First tricks today, here is an easy one :
If you are using Active Support (shipped with Rails), or a ruby version superior or equal to 1.8.7, you can use the symbol proc shortcut :
Here is the standard way declaring a block :
>> ['a', 'b', 'c'].collect {|letter| letter.capitalize}
=> ["A", "B", "C"]
Here is the handy method :
>> ['a', 'b', 'c'].collect(&:capitalize)
=> ["A", "B", "C"]
But, keep in mind that the shortcut method is a little bit slower in term of performance than the normal way cause it creates a new Proc on each call !
Benchmark :
t = Benchmark.realtime do
(['a'] * 1000000).collect(&:to_s)
end
puts "Time using to_proc: #{t}"
t = Benchmark.realtime do
(['a'] * 1000000).collect do |e|
e.to_s
end
end
puts "Time using normal block: #{t}"
# Time using to_proc: 0.631899118423462
# Time using normal block: 0.246822834014893
# Results are the same if you test the normal block first