Skip to content

Commit aea7f43

Browse files
committed
[browsermedia#671] Fix namespaced tables
* Projects shouldn't have the namespace to generated migrations.
1 parent be46393 commit aea7f43

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

features/generators/content_blocks_for_projects.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Feature:
3434
"""
3535
class CreatePetstoreProducts < ActiveRecord::Migration
3636
def change
37-
create_content_table :petstore_products do |t|
37+
create_content_table :products do |t|
3838
t.string :name
3939
t.string :price
4040
@@ -123,7 +123,7 @@ Feature:
123123
"""
124124
class CreatePetstoreDogs < ActiveRecord::Migration
125125
def change
126-
create_content_table :petstore_dogs do |t|
126+
create_content_table :dogs do |t|
127127
128128
t.timestamps
129129
end

lib/generators/cms/content_block/content_block_generator.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,20 @@ def alter_the_model
3131
end
3232

3333
def alter_the_migration
34+
puts "Table name: #{table_name}"
35+
puts "Namespaced: #{namespaced?}"
36+
puts "Coreapplication: #{@in_core_application}"
37+
3438
migration = self.class.migration_exists?(File.absolute_path("db/migrate"), "create_#{table_name}")
39+
40+
if @in_core_application
41+
gsub_file migration, "create_table :#{table_name}", "create_table :#{unnamespaced_table_name}"
42+
puts "From #{table_name} to #{unnamespaced_table_name}"
43+
end
44+
3545
gsub_file migration, "create_table", "create_content_table"
3646

47+
3748
# Attachments do not require a FK from this model to attachments.
3849
self.attributes.select { |attr| attr.type == :attachment }.each do |attribute|
3950
gsub_file migration, "t.attachment :#{attribute.name}", ""
@@ -70,6 +81,14 @@ def create_routes
7081

7182
private
7283

84+
# @override NamedBase#table_name Copy&Paste of this method to make sure project table names are not actually namespaced in migrations.
85+
def unnamespaced_table_name
86+
@unnamespaced_table_name ||= begin
87+
base = pluralize_table_names? ? plural_name : singular_name
88+
(regular_class_path + [base]).join('_')
89+
end
90+
end
91+
7392
def model_has_attachment?
7493
!attachment_attributes().empty?
7594
end

0 commit comments

Comments
 (0)