Skip to content

Instantly share code, notes, and snippets.

@cjheath
Created May 18, 2020 07:44
Show Gist options
  • Save cjheath/d5c3b076e1fdde4a2b71d9790c3e547f to your computer and use it in GitHub Desktop.
Save cjheath/d5c3b076e1fdde4a2b71d9790c3e547f to your computer and use it in GitHub Desktop.
module AlbumArtist
extend ActiveSupport::Concern
included do
validates :album_id, :presence => true
validates :artist_id, :presence => true
end
end
module FollowedPlaylist
extend ActiveSupport::Concern
included do
# Followed Playlist involves Playlist
belongs_to :playlist, :foreign_key => :playlist_id
validates :user_id, :presence => true
end
end
module Following
extend ActiveSupport::Concern
included do
validates :follower_user_id, :presence => true
validates :user_id, :presence => true
end
end
module Performance
extend ActiveSupport::Concern
included do
# Performance involves Song
belongs_to :song, :foreign_key => :song_id
validates :artist_id, :presence => true
end
end
module Playlist
extend ActiveSupport::Concern
included do
self.primary_key = 'playlist_id'
# Playlist Sharing involves Secret Playlist
has_many :playlist_sharings, :class_name => 'PlaylistSharing', :foreign_key => :secret_playlist_id, :dependent => :destroy
# Playlist Member involves Playlist
has_many :playlist_members, :class_name => 'PlaylistMember', :foreign_key => :playlist_id, :dependent => :destroy
has_many :songs, :through => :playlist_members
# Playlist Collaboration involves Collaborative Playlist
has_many :playlist_collaborations, :class_name => 'PlaylistCollaboration', :foreign_key => :collaborative_playlist_id, :dependent => :destroy
# Followed Playlist involves Playlist
has_many :followed_playlists, :class_name => 'FollowedPlaylist', :foreign_key => :playlist_id, :dependent => :destroy
validates :playlist_id, :presence => true
end
end
module PlaylistCollaboration
extend ActiveSupport::Concern
included do
# Playlist Collaboration involves Collaborative Playlist
belongs_to :collaborative_playlist, :class_name => 'Playlist', :foreign_key => :collaborative_playlist_id
validates :user_id, :presence => true
end
end
module PlaylistMember
extend ActiveSupport::Concern
included do
# Playlist Member involves Playlist
belongs_to :playlist, :foreign_key => :playlist_id
# Playlist Member involves Song
belongs_to :song, :foreign_key => :song_id
end
end
module PlaylistSharing
extend ActiveSupport::Concern
included do
# Playlist Sharing involves Secret Playlist
belongs_to :secret_playlist, :class_name => 'Playlist', :foreign_key => :secret_playlist_id
validates :shared_with_user_id, :presence => true
end
end
module Song
extend ActiveSupport::Concern
included do
self.primary_key = 'song_id'
# Performance involves Song
has_many :performances, :class_name => 'Performance', :foreign_key => :song_id, :dependent => :destroy
# Playlist Member involves Song
has_many :playlist_members, :class_name => 'PlaylistMember', :foreign_key => :song_id, :dependent => :destroy
has_many :playlists, :through => :playlist_members
validates :song_id, :presence => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment