Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6792 (closed defect: untested)

Opened 2 years ago

Last modified 3 months ago

[PATCH] models in modules need .demodulize fixes for polymorphic associations

Reported by: loobmedia Assigned to: bitsweat
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: 1.2.0rc1
Severity: major Keywords: sti polymorphic demodulize module
Cc: obrie, dkubb, wolpert, jonathan_viney, =mike

Description

Since 1.2 will offer better support for models placed in modules, it should be noted that most, if not all, association methods don't take this into consideration. Currently there are some places, most notably AR::Base#ensure_proper_type where the inheritance column value is set to a demodulized class name.

When it comes to polymorphic associations though, this isn't the case.

The where clause in these cases gives conditions like: taggable_type = 'Container::Folder' where it should be: taggable_type = 'Folder', according the the overall handling of (STI) types.

The attached patch adjusts this by appending a demodulize call. When applied, all unit test for RC 1.2 still pass.

see also: ticket 5077

Attachments

active_record.diff (8.8 kB) - added by loobmedia on 12/07/06 22:17:39.
namespaced_models.zip (14.2 kB) - added by loobmedia on 12/08/06 17:12:51.

Change History

12/07/06 22:17:39 changed by loobmedia

  • attachment active_record.diff added.

12/07/06 22:18:38 changed by loobmedia

  • summary changed from [PATCH] models in modules need .demodulize fixes for polymorfic associations to [PATCH] models in modules need .demodulize fixes for polymorphic associations.

12/08/06 17:12:51 changed by loobmedia

  • attachment namespaced_models.zip added.

12/08/06 17:15:08 changed by loobmedia

A plugin which fixes (monkeypatches) the issues has been added. There's another ticket on this here. My approach is similar, but should take care of all inconsistencies, association handling included.

12/08/06 17:24:47 changed by loobmedia

The original issue discription is a bit off now, since the solution I'm proposing works differently, it now should read:

"The where clause in these cases gives conditions like: taggable_type = 'Container::Folder' which is what you'd expect."

Module names are now used as table_name prefixes, so Content::Folder.table_name will be 'content_folders'. The (last) module name will be singular, the class name will be plural by convention. This actually does break backwards compatibility, but it's an inconsistency that should be fixed once and for all. Luckily namespaced models haven't been encouraged in the past, so it might not be such an issue.

12/08/06 20:21:04 changed by evan

So are we moving to using demodulize on inheritance types, or towards not using it? I would prefer the latter.

12/08/06 20:23:28 changed by loobmedia

We're not using demodulize on types from now on. That's how it should work and it truly allows namespaces, also at the db/table level.

12/08/06 22:44:16 changed by bitsweat

  • owner changed from core to bitsweat.

Is this really a regression?

If so, we need a separate 1.2 fix which omits the backward-incompatible changes in addition to the patch for trunk.

We also need a migration generator to move folks from the old to the new type values.

And all of these changes need test coverage. Exhausting, I know :) Thanks loob.

12/08/06 22:44:21 changed by bitsweat

  • status changed from new to closed.
  • resolution set to untested.

12/08/06 22:57:09 changed by loobmedia

I just wanted to point out that the changes only affect those who are already using namespaced models in their apps. Otherwise it won't affect anything. If any the improved table_name logic would be an issue, the fix is easy: just set the table_name manually. The only thing that's left is to update column values for the inheritance column; I can't see where a migration generator could help in this situation. You'd probably want to be selective on how and when you want to prefix the module name to those values.

I do understand the need for extra tests though, although the plugin already does some basic tests. There test/modules_test.rb file in AR is something I did before. Unfortunately by reusing the available tables/models Company, Client and so on makes things very hairy if you know what I mean. It leads to unnecessary complicated fixtures and associations. That's why implemented it as a plugin for now, to be able to test it seperatly.

01/03/07 18:16:55 changed by obrie

  • cc set to obrie.

02/19/07 20:02:57 changed by dkubb

  • cc changed from obrie to obrie, dkubb.

03/04/07 13:48:34 changed by bitsweat

  • keywords deleted.
  • milestone changed from 1.2 to 1.2 regressions.

03/12/07 16:00:56 changed by bitsweat

  • keywords set to sti polymorphic demodulize module.
  • milestone changed from 1.2 regressions to 1.x.

03/12/07 18:04:42 changed by wolpert

  • cc changed from obrie, dkubb to obrie, dkubb, wolpert.

03/15/07 21:31:30 changed by jonathan_viney

  • cc changed from obrie, dkubb, wolpert to obrie, dkubb, wolpert, jonathan_viney.

03/15/07 21:52:24 changed by jonathan_viney

I'd like to get a patch for this in if possible. What's the deal with providing backwards compatibility?

Is it fine to simply change the way AR works and provide example migrations or even a migration generator, or is backwards compatibility in this area a must for 1.3? This could be done with a flag set on AR::Base to toggle between the two behaviours.

It should be noted that the necessary changes will only affect people already storing the names of namespaced models in their applications. I think most people would be unaffected.

(follow-up: ↓ 21 ) 03/17/07 10:39:16 changed by jonathan_viney

I've whipped up an improved version of the namespaced_models plugin posted here.

http://svn.viney.net.nz/things/rails/plugins/namespaced_models

Merging the changes into Rails isn't a big job, but fixing all the test cases is the sort of task you wouldn't wish on anybody. It may be simpler to rewrite them with new fixtures, rather than try and rework the existing ones.

06/25/07 20:02:19 changed by dkubb

+1 for this patch

06/26/07 01:54:54 changed by caio

I had this idea while working on #8740 and figured it might be useful in this ticket too.

To ease the transition, how about you add some smarts to compute_type / type_name_with_module, so that they'll work with both modularized and demodularized type values. (while defaulting to modularized).

A migration is still welcome as having inconsistent records would not be wise in the long term.

09/28/07 16:26:25 changed by bitsweat

  • keywords changed from sti polymorphic demodulize module to sti polymorphic demodulize module rails2.

10/08/07 03:57:45 changed by bitsweat

  • keywords changed from sti polymorphic demodulize module rails2 to sti polymorphic demodulize module.
  • milestone changed from 1.x to 2.0.

Test me! :)

(in reply to: ↑ 16 ) 03/31/08 12:26:12 changed by =mike

  • cc changed from obrie, dkubb, wolpert, jonathan_viney to obrie, dkubb, wolpert, jonathan_viney, =mike.
  • milestone changed from 2.0 to 2.x.

Replying to jonathan_viney:

I've whipped up an improved version of the namespaced_models plugin posted here. http://svn.viney.net.nz/things/rails/plugins/namespaced_models

This plugin makes my namespaced STI models functional. Namespaced STI is unuseable in Rails 1.2.3 and 2.0.2 (and everything in between I'd guess) without the plugin because the model class name and the value stored in its inheritance column do not match.

+1 for incorporating this much needed fix

Like the commentors before me, I leave the ticket closed but the issue isn't closed at all. It's a valid, current major defect.

03/31/08 15:52:24 changed by jeremy

This ticket is *not closed* -- the patch has no tests, so the ticket is marked as untested. To incorporate this change, please add unit tests for it!