pahanix & IT

yet another passionate programmer

Posts tagged rails

3 notes &

Clear your logs automatically in development when they are too large.

# 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

Filed under rails ruby tips

1 note &

Hoptoad and rescue_from

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'
end

Filed under rails hoptoad rescue_from

1 note &

Replace a div.fieldWithErrors with a span in Rails

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})}

Filed under rails tricks

0 notes &

ActiveRecord, method_missing и stack level too deep

Переопределяя у 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

Filed under method_missing rails tips