Posts tagged rails
Posts tagged rails
3 notes &
# config/initializers/clear_logs.rb
# This snippet simply clears your logs when they are too large.
# Large logs mean looooong search in TextMate. You know it :)
# Every time you run rails server or rails console it checks log sizes
# and clears the logs for you if necessary.
if Rails.env.development?
MAX_LOG_SIZE = 2.megabytes
logs = File.join(Rails.root, 'log', '*.log')
if Dir[logs].any? {|log| File.size?(log).to_i > MAX_LOG_SIZE }
$stdout.puts "Runing rake log:clear"
`rake log:clear`
end
end
http://gist.github.com/885671
UPD: It will delete production logs if you mistakenly run something in dev mode on production
0 notes &
If you have a cucumber feature which depends on a resque worker make sure that
@no-txn tag1 note &
I’m using rescue_from in my controller, but now I don’t see the exceptions in Hoptoad. Whazzup with that?
Hoptoad uses alias_method_chain to hook into the rescue_action_in_public method.
# Overrides the rescue_action method in ActionController::Base,
# but does not inhibit any custom processing that is defined
# with Rails 2's exception helpers.
def rescue_action_in_public_with_hoptoad exception
notify_hoptoad(exception) unless ignore?(exception)
rescue_action_in_public_without_hoptoad(exception)
end
The rescue_from system in Rails actually catches the exceptions before rescue_action_in_public happens. The upshot is that if you want to see those exceptions in Hoptoad, you’ll need to explicitly send them along in your rescue_from method using notify_hoptoad(exception):
rescue_from SillyError, :with => :render_silly_error
def render_silly_error(e)
notify_hoptoad(e)
render :template => 'i_blowzed_up'
end0 notes &
Did you know that useful little method ActiveRecord::Base#touch will fire all of your after_save callbacks?
1 note &
Replace the stupid fieldWithErrors <div> with a <span>. YES, I know you can make DIVs inline by applying a “display: inline” style; the real problem is that you can’t simply use DIVs anywhere. Most significantly, you can’t use DIVs inside a P, which would be invalid HTML and in fact confuses some browsers more than you’d like.
ActionView::Base.field_error_proc =
Proc.new {|html_tag, instance| %(#{html_tag})}
97 notes &
When you have a model that requires additional information, if a certain condition is met and you need to validate this attribute, this little trick can help.
Say we have a user model that needs to validate the
truck_serialattribute if the user has the role of driver:class User <...
0 notes &
Переопределяя у ActiveRecord модели #method_missing важно помнить, что методы чтения атрибутов генерятся через сам #method_missing
Так, например, код
class Appearance < ActiveRecord::Base
serialize :prefereneces, HashWithIndifferentAccess
def method_missing(name, *args)
preferences[name] || super
end
end
вывалит Exception SystemStackError: stack level too deep
Обойти это можно, вызвав сначала super и перехватив NoMethodError, а потом его рейзануть обратно если не выполнилось необходимое условие.
def method_missing(name, *args)
begin
super
rescue NoMethodError
preferences[name] || raise
end
end