Repository navigation
feat: give contents ownership of module membership and order #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| module Contentable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| has_one :content, as: :contentable | ||
| # Whether the content record owns this contentable, so the contentable is destroyed together with it. | ||
| # | ||
| # @return [Boolean] | ||
| def owned_by_content? | ||
| false | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,24 @@ | ||
| # Orders records by an integer +position+ column. New records go to the end of their siblings. | ||
| module Ordered | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| scope :ordered, -> { order(:position) } | ||
|
|
||
| before_create :append_to_siblings | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # This method is required to override. It is the relation of records this record is | ||
| # ordered among (for example, the contents of one module). | ||
| # | ||
| # @return [ActiveRecord::Relation] | ||
| def position_siblings | ||
|
sean-dickinson marked this conversation as resolved.
Resolved
|
||
| raise NotImplementedError, "#{self.class} must define #position_siblings to be ordered" | ||
| end | ||
|
|
||
| def append_to_siblings | ||
| self.position = (position_siblings.maximum(:position) || 0) + 1 | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,26 @@ | ||
| class Content < ApplicationRecord | ||
| include Ordered | ||
|
|
||
| belongs_to :content_module | ||
|
|
||
| delegated_type :contentable, types: %w[Link Game] | ||
|
|
||
| validates :contentable_id, uniqueness: { | ||
| scope: [ :content_module_id, :contentable_type ], | ||
| message: "is already in this module" | ||
| } | ||
|
|
||
| # Ensure the associated contentable record is destroyed if allowed before destroying this content. | ||
| # This allows us to have dependent: :destroy behavior for contentable records. | ||
| before_destroy :destroy_owned_record | ||
|
|
||
| private | ||
|
|
||
| def position_siblings | ||
| content_module.contents | ||
| end | ||
|
|
||
| def destroy_owned_record | ||
| contentable.destroy if contentable.destroy_with_attachable? | ||
| contentable.destroy if contentable.owned_by_content? | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,12 @@ | ||
| class Game < ApplicationRecord | ||
| include Contentable | ||
|
|
||
| belongs_to :content_module, optional: true | ||
| has_many :game_attempts, dependent: :destroy | ||
|
|
||
| has_many :contents, as: :contentable, dependent: :destroy | ||
| has_many :content_modules, through: :contents | ||
| has_many :game_attempts, dependent: :destroy | ||
|
|
||
| validates :title, :slug, presence: true | ||
| validates :title, :slug, uniqueness: true | ||
|
|
||
| def destroy_with_attachable? | ||
| false | ||
| end | ||
| scope :available_for, ->(content_module) { where.not(id: content_module.games).order(:title) } | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,16 @@ | ||
| class Link < ApplicationRecord | ||
| include Ordered | ||
| include Contentable | ||
|
|
||
| belongs_to :content_module | ||
| # :delete skips Content callbacks to avoid a destroy loop | ||
| has_one :content, as: :contentable, dependent: :delete | ||
| has_one :content_module, through: :content | ||
|
|
||
| enum :link_type, { survey: "survey" }, validate: true | ||
|
|
||
| validates :title, :url, :link_type, presence: true | ||
| validates :url, format: { with: /\Ahttps?:\/\/.+\z/i, message: "must start with http:// or https://" }, allow_blank: true | ||
|
|
||
| def destroy_with_attachable? | ||
| def owned_by_content? | ||
| true | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| class AddPositionToContents < ActiveRecord::Migration[8.1] | ||
| def change | ||
| add_column :contents, :position, :integer | ||
| add_index :contents, [ :content_module_id, :position ] | ||
| add_index :contents, [ :content_module_id, :contentable_type, :contentable_id ], unique: true, name: "index_contents_on_module_and_contentable" | ||
| end | ||
| end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20261031181544_remove_content_module_from_links_and_games.rb
This file contains hidden or 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,11 @@ | ||
| class RemoveContentModuleFromLinksAndGames < ActiveRecord::Migration[8.1] | ||
| def up | ||
| remove_reference :links, :content_module, foreign_key: true, index: true | ||
| remove_column :links, :position | ||
| remove_reference :games, :content_module, foreign_key: true, index: true | ||
| end | ||
|
|
||
| def down | ||
| raise ActiveRecord::IrreversibleMigration | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.