This project is archived and is in readonly mode.

#379 ✓committed
Eadz

[PATCH] ActiveRecord::Base class_name weirdness

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

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" ##

Comments and changes to this ticket

  • Cheah Chu Yeow

    Cheah Chu Yeow June 23rd, 2008 @ 04:32 PM

    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.

  • Eadz

    Eadz June 27th, 2008 @ 01:09 AM

    • 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?

  • Tarmo Tänav

    Tarmo Tänav September 18th, 2008 @ 08:02 AM

    • 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.

  • Pratik

    Pratik December 19th, 2008 @ 03:49 PM

    • Assigned user set to “Tarmo Tänav”
    • State changed from “new” to “incomplete”

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

  • Eadz

    Eadz March 8th, 2009 @ 12:37 AM

    • Tag changed from activerecord, tiny to 2.3.0, activerecord, tiny

    Is this going to be fixed in 2.3?

  • Eadz

    Eadz February 8th, 2010 @ 01:57 AM

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

  • David Trasbo

    David Trasbo June 29th, 2010 @ 01:13 PM

    • 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.

  • David Trasbo

    David Trasbo June 29th, 2010 @ 01:14 PM

    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.

  • David Trasbo

    David Trasbo June 29th, 2010 @ 01:14 PM

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

  • José Valim

    José Valim June 29th, 2010 @ 01:42 PM

    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?

  • David Trasbo

    David Trasbo June 29th, 2010 @ 03:56 PM

    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.

  • David Trasbo
  • Repository

    Repository June 29th, 2010 @ 04:55 PM

    • State changed from “incomplete” to “committed”

    (from [ac42e6951f0feb1a9890a3bbf5ebad87ebec08cc]) Deprecate ActiveRecord::Base#class_name [#379 state:committed]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/ac42e6951f0feb1a9890a3bbf5ebad...

  • Repository

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages