This project is archived and is in readonly mode.
[PATCH] AR migration generator includes model's modules in table name.
-
phs
#4032 Module specific table_name_prefix looks like another issue stomping around in the same neighborhood.
-
phs
- Tag changed from activerecord module migration generator to activerecord, generator, migration, module, namespace
To choose the new table's name, the migration generator template in AR at
lib/generators/active_record/model/templates/migration.rbuses the (gasp)table_namemethod.table_nameis defined over in railties:# lib/rails/generators/named_base.rb def table_name @table_name ||= begin base = pluralize_table_names? ? plural_name : singular_name (class_path + [base]).join('_') end endfile_name, in the same file, looks like it would work.However, the other generator templates in AR also use
table_namewhere one might expect. Given that, and the fact the method is named table_name, it seems more natural to overridetable_nameinActiveRecord::Generators::Base. -
phs
- Tag changed from activerecord, generator, migration, module, namespace to activerecord, generator, migration, module, namespace, patch
This patch effectively renames #table_name to #table_path, and provides a new #table_name that omits any leading namespace components.
Most generators that use #table_name have been left alone, as they seem to work reasonably in either case. Migrations has been updated to keep the namespace components in their filenames (however, the names of the tables they manipulate have dropped their prefixes.) This keeps their tests happy, which seem to make a point of having the namespaced filenames in various situations.
-
phs
- Title changed from AR migration generator includes model's modules in table name. to [PATCH] AR migration generator includes model's modules in table name.
-
phs
Here's an alternate patch that makes the change to #table_name but doesn't bother introducing #table_path. Generated migrations from the model generator now make un-namespaced files.
-
phs
Here's a third option. This one leaves #table_name alone, and slips a
self.table_name_prefix = "my_module_"when generating namespaced models. -
Francesc Esplugas
Same problem here. At this moment I'm fixing this problem by using "set_table_name". Using the table name prefix doesn't seem a good option:
class Delayed::Job < ActiveRecord::Base self.table_name_prefix = 'delayed_' end rails_3b2 → rails console Loading development environment (Rails 3.0.0.beta2) >> Delayed::Job.table_name => "delayed_jobs" >> Post.table_name => "delayed_posts" rails_3b2 → rails console Loading development environment (Rails 3.0.0.beta2) >> Post.table_name => "posts" >> Delayed::Job.table_name => "delayed_jobs" >> Post.table_name => "posts"I would go for a set_table_name option ...
-
Francesc Esplugas
Another observation is that the usage of
self.table_name_prefixon a model would overwrite the system widetable_name_prefixoption. -
José Valim
- Milestone cleared.
- Assigned user set to José Valim
-
José Valim
Can someone please provide a patch that changes self.table_name_prefix to use class_attribute instead of cattr_accessor allowing it to work with inheritance?
-
phs
#4032 Module specific table_name_prefix might be another way out. If we could find a canonical place to put the module-specific table_name_prefix declaration, then the model generator could ensure it is present.
I don't have a justifiable opinion for preferring either #4032 Module specific table_name_prefix or class_attribute :table_name_prefix over the other, but the module-specific table_name_prefix is already in master.
-
phs
Here's the class_attribute patch.
-
José Valim
Awesome! I talked with Jeremy Kemper and he proposed us to follow the pattern in #4032 Module specific table_name_prefix. That said, when you run:
rails g model my_module/postIt should create a file in app/models/my_module.rb with the following contents:
module MyModule def self.table_name_prefix 'my_module_' end endCan you provide a patch for this? I will apply both this new patch and the class_attribute one once they are done. :)
Thanks!
-
Andrew White
José, should the patch try and load the parent constant and check for table_name_prefix or should it just check for the presence of the 'app/models/my_module.rb' file and don't create it if it already exists.
The attached patch is a naive implementation that doesn't have any kind of intelligence. Is this the kind of thing you're looking for or a more sophisticated patch that checks for the parent constant first and then adjusts the migration name and the table name appropriately.
The patch is just a proof of concept - if it's acceptable I'll add another later with docs and tests.
-
José Valim
Andrew, the patch looks great. We just need tests and there is no need to check if a file is already there, since Rails does it for us and delegates the decision to the user.
-
Andrew White
Test and docs added to USAGE
-
Andrew White
Forgot to squash commits - updated patch as single commit
-
Andrew White
Third time lucky - removed check for existing file.
-
Repository
- State changed from new to resolved
(from [788d9238931bb36ed2af78d34c84562dc3a4848c]) Generate module file for namespaced models [#4230 [PATCH] AR migration generator includes model's modules in table name. state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/788d9238931bb36ed2af78d34c8456... -
Repository
(from [bab1f910c7399fcfe9f031a1ce3a1f36bf5fd277]) table_name_prefix and table_name_suffix are class_attributes instead of cattr_accessors. [#4230 [PATCH] AR migration generator includes model's modules in table name. ]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/bab1f910c7399fcfe9f031a1ce3a1f... -
José Valim
Thanks guys, applied! Can someone please tell me if #2965 Advanced / foxy fixture features doesn't work well with models in modules is still an issue in Rails master?
-
Andrew White
Yes, it's still an issue. Is it something you'd like me to investigate?
-
José Valim
@Andrew, if you can, it would be awesome!
-
Andrew White
I'll get on it. One immediate consequence of getting it working is that the fixture file path will have to switch from matching the table name to matching the underscored class name - is this okay?
