-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Auralcat/polymorphic-associations
Separate TextShouts from other kinds of content
- Loading branch information
Showing
8 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class TextShout < ApplicationRecord | ||
validates :body, presence: true, length: { in: 1..144 } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateTextShouts < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :text_shouts do |t| | ||
t.string :body, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class MakeShoutsPolymorphic < ActiveRecord::Migration[5.2] | ||
# Declare the involved models here so we don't have any validation | ||
# problems in the future. | ||
class Shout < ApplicationRecord | ||
belongs_to :content, polymorphic: true | ||
end | ||
class TextShout < ApplicationRecord; end | ||
|
||
def change | ||
change_table(:shouts) do |t| | ||
t.string :content_type | ||
t.integer :content_id | ||
t.index [:content_type, :content_id] | ||
end | ||
|
||
|
||
# With this we can reverse this migration, depending on direction | ||
reversible do |direction| | ||
Shout.reset_column_information | ||
Shout.find_each do |shout| | ||
# Copy the old Shouts to TextShouts. | ||
direction.up do | ||
text_shout = TextShout.create(body: shout.body) | ||
shout.update(content_id: text_shout.id, content_type: "TextShout") | ||
end | ||
|
||
direction.down do | ||
shout.update(body: shout.content.body) | ||
shout.content.destroy | ||
end | ||
end | ||
end | ||
|
||
remove_column :shouts, :body, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters