davejacobs / letters

A tiny debugging library for Ruby
Other
271 stars 9 forks source link

Apparent name conflict if used with ActionPack (3.1.2) #2

Closed psobot closed 12 years ago

psobot commented 12 years ago

Just tried using Letters on an old Rails project of mine, and ran into an odd stack trace after running rake:

ActionView::Template::Error: wrong number of arguments (2 for 1)
    /Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/letters-0.2.0/lib/letters/core_ext.rb:151:in `t'
    /Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.2/lib/action_view/helpers/date_helper.rb:89:in `block in distance_of_time_in_words'
    /Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/core_ext/object/with_options.rb:41:in `with_options'
    /Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.2/lib/action_view/helpers/date_helper.rb:73:in `distance_of_time_in_words'

It seems that ActionPack 3.1.2 calls locale.t in a block called from I18n.with_options. As letters overrides t on all objects, this results in a bad method call. (Not sure if this also happens in later versions of Rails or ActionPack.)

davejacobs commented 12 years ago

Ah okay, originally, I just patched the Ruby core classes, but this morning I realized that Rails devs would probably want all objects patched.

I'm going to work on a fix for this now. As a temporary step, I would recommend manually requiring "letters/patch/core" and then running Letters.patch! on all extra objects you care about -- probably ActiveRecord::Base and ApplicationController, for example.

davejacobs commented 12 years ago

Okay, here's a potential solution. Do you get the same error if you replace require "letters" with require "letters/patch/rails"? (You will need to run bundle update letters first.)

psobot commented 12 years ago

I've just been including letters in my Gemfile with gem "letters" - and trying gem 'letters', :require => ["letters/patch/rails"] doesn't seem to work. Any ideas?

davejacobs commented 12 years ago

It may be that you have to explicitly require the patch right after Bundler.require in application.rb. Does that work for you? I may end up writing a Rails-specific gem to make this easier.

psobot commented 12 years ago

Adding require "letters/patch/rails" in my application.rb seems to not help - it results in:

rake aborted!
no such file to load -- letters/patch/rails
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/dependencies.rb:240:in `require'
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/dependencies.rb:240:in `block in require'
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/dependencies.rb:223:in `block in load_dependency'
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/dependencies.rb:640:in `new_constants_in'
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.2/lib/active_support/dependencies.rb:240:in `require'
/Volumes/why/lndry/config/application.rb:8:in `<top (required)>'
...
davejacobs commented 12 years ago

Okay, thanks for the trace. Two questions:

psobot commented 12 years ago

Looks like I'm at 0.2.1:

(branch:master*) » bundle show letters    
/Users/psobot/.rvm/gems/ruby-1.9.2-p290/gems/letters-0.2.1

I've tried requiring letters/patch/rails directly after this line in application.rb:

Bundler.require(*Rails.groups(:assets => %w(development test)))

I also tried sticking it at the end of boot.rb, to the same effect.

davejacobs commented 12 years ago

Okay, I made a small mistake in 0.2.1. Could you try upgrading to 0.2.2 and seeing if the same patch works? If it doesn't, would you mind creating a new Rails app and seeing if you can load it with Letters?

Thanks!

davejacobs commented 12 years ago

I haven't heard back in a while about this, please reopen the ticket if the problem persists.

codeablehq commented 12 years ago

It's still a problem, I have actionpack 3.2.8 and letters 0.2.3 and facing exactly the same issue, can't use time distance methods like distance_of_time_in_words

davejacobs commented 12 years ago

So you're requiring "letters/rails/patch" in application.rb and are getting a file not found error?

codeablehq commented 12 years ago

Ah, I missed the line about loading the patch, working fine - thanx!

psobot commented 12 years ago

Hey - sorry for the delay, but I've just been able to reproduce this issue in a fresh Rails 3.2.0 project. Added gem "letters" to my Gemfile, require "letters/patch/rails" in my application.rb and put the following in an otherwise blank view:

<%=distance_of_time_in_words(Time.now, Time.now + 5) %>

Which results in the following exception on that view:

letters (0.3.1) lib/letters/core_ext.rb:176:in `t'
actionpack (3.2.0) lib/action_view/helpers/date_helper.rb:77:in `block in distance_of_time_in_words'
activesupport (3.2.0) lib/active_support/core_ext/object/with_options.rb:41:in `with_options'
actionpack (3.2.0) lib/action_view/helpers/date_helper.rb:73:in `distance_of_time_in_words'
app/views/welcome/index.html.erb:1:in `_app_views_welcome_index_html_erb__612129154362926409_70147740061220'
davejacobs commented 12 years ago

Try adding :require => false to the line in your Gemfile that installs Letters.

psobot commented 12 years ago

D'oh - thanks, now that example works perfectly. Thanks for your help.