Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] ActiveRecord::Base class_name weirdness

#379

class_name should return the class name or be called something else.

## Rails IRB Session, Rails 2.1

class DummyOne < ActiveRecord::Base

end

>> DummyOne.class_name

=> "DummyOne"

class DummyTwo < ActiveRecord::Base

set_table_name "dt"

end

>> DummyTwo.class_name

=> "Dt" ##

Reported by Eadz · June 10th, 2008 @ 03:51 AM

State: committed
Milestone: none
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Cheah Chu Yeow
    Cheah Chu Yeow

    Hmm, you can actually simply use Module#name:

    DummyTwo.name
    

    AR::Base#class_name is actually documented like so:

    "Turns the +table_name+ back into a class name following the reverse rules of +table_name+."

    So while the method name may be misleading, I don't think it was intended to be used the way you described.

    June 23rd, 2008 @ 04:32 PM

  2. Eadz
    Eadz
    • Tag set to activerecord, tiny

    In the context of "Turns the +table_name+ back into a class name" what exactly is a "class name" if not the name of the class?

    June 27th, 2008 @ 01:09 AM

  3. Tarmo Tänav
    Tarmo Tänav
    • Tag changed from activerecord, tiny to activerecord, tiny

    Perhaps the method should be removed?, nothing in AR seems to depend on it (the only test that breaks when this method is broken is the test for this method) and as is clear from this ticket it is misleading and, at least to me, seems practically useless.

    September 18th, 2008 @ 08:02 AM

  4. Pratik
    Pratik
    • Assigned user set to Tarmo Tänav
    • State changed from new to incomplete

    Need to deprecate the method too. Patch/Commit please :)

    December 19th, 2008 @ 03:49 PM

  5. Eadz
    Eadz
    • Tag changed from activerecord, tiny to 2.3.0, activerecord, tiny

    Is this going to be fixed in 2.3?

    March 8th, 2009 @ 12:37 AM

  6. Eadz
    Eadz
    • Tag changed from 2.3.0, activerecord, tiny to 2.3.0, 3.0pre, activerecord, cleanup, tiny
    • Title changed from ActiveRecord::Base class_name weirdness to [PATCH] ActiveRecord::Base class_name weirdness

    This bug/feature is still in rails 3.0-pre.

    Loading development environment (Rails 3.0.0.beta)

    Site.class_name => "Site"

    class Site < ActiveRecord::Base set_table_name "my_site_table" end => nil Site.class_name => "MySiteTable"

    And it is still the case that when this method is removed completely from activerecord/base.rb, the only test that fails is the one for the method itself.

    Additionally, the method might not do as advertised, its description states :

    "Turns the +table_name+ back into a class name following the reverse rules of +table_name+."

    However, #table_name has been updated to check for parent's tables to do with STI. I'm not sure if this method would work with STI in its current state.

    I've attached a patch which deprecates class_name, and removes the test for it ( else you'll get deprecation warnings in your tests )

    February 8th, 2010 @ 01:57 AM

  7. David Trasbo
    David Trasbo
    • Assigned user changed from Tarmo Tänav to José Valim
    • Importance changed from to

    The ActiveSupport::Deprecation has native support for deprecating methods. I've attached a patch that makes use of that. Also, I propose not removing the test until the method itself is removed.

    June 29th, 2010 @ 01:13 PM

  8. David Trasbo
    David Trasbo

    The ActiveSupport::Deprecation has native support for deprecating methods. I've attached a patch that makes use of that. Also, I propose not removing the test until the method itself is removed.

    June 29th, 2010 @ 01:14 PM

  9. David Trasbo
    David Trasbo

    Ignore 2901.diff. It's a patch for another ticket. :)

    June 29th, 2010 @ 01:14 PM

  10. José Valim
    José Valim

    David, we can simply remove the method in Rails 3.0 and deprecate it only in 2-3-stable. Besides, we use only the ActiveSupport::Deprecation.warn pattern inside Rails. Could you please use it as well?

    June 29th, 2010 @ 01:42 PM

  11. David Trasbo
    David Trasbo

    Sure, I've made two patches: One that applies to master which simply removes the method and the corresponding test, and one that applies to 2-3-stable that issues a deprecation warning using the warn method and silences the deprecation warning in the test.

    June 29th, 2010 @ 03:56 PM

  12. David Trasbo
  13. Repository
  14. Repository