like/follow refactor

This commit is contained in:
Brian Smith 2014-02-15 18:23:00 -05:00
parent 5a03ec87ae
commit c03404d520
9 changed files with 51 additions and 52 deletions

View File

@ -4,56 +4,29 @@ add constraint music_sessions_history_pkey PRIMARY KEY (id);
CREATE TABLE likes CREATE TABLE likes
( (
id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), id character varying(64) NOT NULL DEFAULT uuid_generate_v4(),
liker_id character varying(64) NOT NULL, user_id character varying(64) NOT NULL,
user_id character varying(64), likable_id character varying(64) NOT NULL,
band_id character varying(64), likable_type character varying(25) NOT NULL,
session_id character varying(64),
recording_id character varying(64),
likable_type character varying(25),
created_at timestamp without time zone NOT NULL DEFAULT now(), created_at timestamp without time zone NOT NULL DEFAULT now(),
updated_at timestamp without time zone NOT NULL DEFAULT now(), updated_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT likes_pkey PRIMARY KEY (id), CONSTRAINT likes_pkey PRIMARY KEY (id),
CONSTRAINT liker_fkey FOREIGN KEY (liker_id) CONSTRAINT likes_user_fkey FOREIGN KEY (user_id)
REFERENCES users (id) MATCH SIMPLE REFERENCES users (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE, ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT user_likes_fkey FOREIGN KEY (user_id) CONSTRAINT likes_user_uniqkey UNIQUE (user_id, likable_id)
REFERENCES users (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT bands_likes_fkey FOREIGN KEY (band_id)
REFERENCES bands (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT session_likes_fkey FOREIGN KEY (session_id)
REFERENCES music_sessions_history (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT recording_likes_fkey FOREIGN KEY (recording_id)
REFERENCES recordings (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT user_likes_uniqkey UNIQUE (liker_id, user_id),
CONSTRAINT band_likes_uniqkey UNIQUE (liker_id, band_id),
CONSTRAINT session_likes_uniqkey UNIQUE (liker_id, session_id),
CONSTRAINT recording_likes_uniqkey UNIQUE (liker_id, recording_id)
); );
CREATE TABLE followings CREATE TABLE follows
( (
id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), id character varying(64) NOT NULL DEFAULT uuid_generate_v4(),
follower_id character varying(64) NOT NULL, user_id character varying(64) NOT NULL,
user_id character varying(64), followable_id character varying(64) NOT NULL,
band_id character varying(64), followable_type character varying(25) NOT NULL,
followable_type character varying(25),
created_at timestamp without time zone NOT NULL DEFAULT now(), created_at timestamp without time zone NOT NULL DEFAULT now(),
updated_at timestamp without time zone NOT NULL DEFAULT now(), updated_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT followings_pkey PRIMARY KEY (id), CONSTRAINT follows_pkey PRIMARY KEY (id),
CONSTRAINT follower_fkey FOREIGN KEY (follower_id) CONSTRAINT follows_user_fkey FOREIGN KEY (user_id)
REFERENCES users (id) MATCH SIMPLE REFERENCES users (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE, ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT user_followings_fkey FOREIGN KEY (user_id) CONSTRAINT follows_user_uniqkey UNIQUE (user_id, followable_id)
REFERENCES users (id) MATCH SIMPLE );
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT bands_followings_fkey FOREIGN KEY (band_id)
REFERENCES bands (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT user_followings_uniqkey UNIQUE (follower_id, user_id),
CONSTRAINT band_followings_uniqkey UNIQUE (follower_id, band_id)
);

View File

@ -84,6 +84,7 @@ require "jam_ruby/models/fan_invitation"
require "jam_ruby/models/friend_request" require "jam_ruby/models/friend_request"
require "jam_ruby/models/instrument" require "jam_ruby/models/instrument"
require "jam_ruby/models/like" require "jam_ruby/models/like"
require "jam_ruby/models/follow"
require "jam_ruby/models/musician_instrument" require "jam_ruby/models/musician_instrument"
require "jam_ruby/models/notification" require "jam_ruby/models/notification"
require "jam_ruby/models/track" require "jam_ruby/models/track"

View File

@ -26,15 +26,20 @@ module JamRuby
# recordings # recordings
has_many :recordings, :class_name => "JamRuby::Recording", :foreign_key => "band_id" has_many :recordings, :class_name => "JamRuby::Recording", :foreign_key => "band_id"
# likers # self.id = likable_id in likes table
has_many :likers, :class_name => "JamRuby::BandLiker", :foreign_key => "band_id", :inverse_of => :band has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy
has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id"
# self.id = followable_id in follows table
has_many :follows, :as => :followable, :class_name => "JamRuby::Follow", :dependent => :destroy
# has_many :likers, :class_name => "JamRuby::BandLiker", :foreign_key => "band_id", :inverse_of => :band
# has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id"
# followers # followers
has_many :band_followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id" # has_many :band_followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id"
has_many :followers, :through => :band_followers, :class_name => "JamRuby::User" # has_many :followers, :through => :band_followers, :class_name => "JamRuby::User"
has_many :inverse_band_followers, :through => :followers, :class_name => "JamRuby::BandFollower", :foreign_key => "follower_id" # has_many :inverse_band_followers, :through => :followers, :class_name => "JamRuby::BandFollower", :foreign_key => "follower_id"
has_many :inverse_followers, :through => :inverse_band_followers, :source => :band, :class_name => "JamRuby::Band" # has_many :inverse_followers, :through => :inverse_band_followers, :source => :band, :class_name => "JamRuby::Band"
# invitations # invitations
has_many :invitations, :inverse_of => :band, :class_name => "JamRuby::BandInvitation", :foreign_key => "band_id" has_many :invitations, :inverse_of => :band, :class_name => "JamRuby::BandInvitation", :foreign_key => "band_id"

View File

@ -0,0 +1,8 @@
module JamRuby
class Follow < ActiveRecord::Base
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
belongs_to :likable, :polymorphic => true
end
end

View File

@ -1,7 +1,7 @@
module JamRuby module JamRuby
class Like < ActiveRecord::Base class Like < ActiveRecord::Base
belongs_to :liker, :class_name => "JamRuby::User", :foreign_key => "liker_id" belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
belongs_to :likable, :polymorphic => true belongs_to :likable, :polymorphic => true
end end

View File

@ -21,7 +21,8 @@ module JamRuby
has_many :music_session_user_histories, :class_name => "JamRuby::MusicSessionUserHistory", :foreign_key => "music_session_id" has_many :music_session_user_histories, :class_name => "JamRuby::MusicSessionUserHistory", :foreign_key => "music_session_id"
has_many :comments, :class_name => "JamRuby::MusicSessionComment", :foreign_key => "music_session_id" has_many :comments, :class_name => "JamRuby::MusicSessionComment", :foreign_key => "music_session_id"
has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "music_session_id" # has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "music_session_id"
has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy
has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id' has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id'
before_create :generate_share_token before_create :generate_share_token

View File

@ -10,7 +10,8 @@ module JamRuby
has_many :mixes, :class_name => "JamRuby::Mix", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy has_many :mixes, :class_name => "JamRuby::Mix", :inverse_of => :recording, :foreign_key => 'recording_id', :dependent => :destroy
has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :foreign_key => :recording_id, :dependent => :destroy has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :foreign_key => :recording_id, :dependent => :destroy
has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id" has_many :comments, :class_name => "JamRuby::RecordingComment", :foreign_key => "recording_id"
has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "recording_id" # has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "recording_id"
has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy
has_many :plays, :class_name => "JamRuby::RecordingPlay", :foreign_key => "recording_id" has_many :plays, :class_name => "JamRuby::RecordingPlay", :foreign_key => "recording_id"
belongs_to :owner, :class_name => "JamRuby::User", :inverse_of => :owned_recordings, :foreign_key => 'owner_id' belongs_to :owner, :class_name => "JamRuby::User", :inverse_of => :owned_recordings, :foreign_key => 'owner_id'

View File

@ -38,12 +38,22 @@ module JamRuby
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band" has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
# recordings # recordings
has_many :owned_recordings, :class_name => "JamRuby::Recording", :foreign_key => 'owner_id' has_many :owned_recordings, :class_name => "JamRuby::Recording", :foreign_key => "owner_id"
has_many :recordings, :through => :claimed_recordings, :class_name => "JamRuby::Recording" has_many :recordings, :through => :claimed_recordings, :class_name => "JamRuby::Recording"
has_many :claimed_recordings, :class_name => "JamRuby::ClaimedRecording", :inverse_of => :user has_many :claimed_recordings, :class_name => "JamRuby::ClaimedRecording", :inverse_of => :user
has_many :playing_claimed_recordings, :class_name => "JamRuby::MusicSession", :inverse_of => :claimed_recording_initiator has_many :playing_claimed_recordings, :class_name => "JamRuby::MusicSession", :inverse_of => :claimed_recording_initiator
has_many :likes, :as => :likable # self.id = user_id in likes table
has_many :likings, :class_name => "JamRuby::Like", :inverse_of => :user, :dependent => :destroy
# self.id = likable_id in likes table
has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy
# self.id = user_id in follows table
has_many :followings, :class_name => "JamRuby::Follow", :inverse_of => :user, :dependent => :destroy
# self.id = followable_id in follows table
has_many :follows, :as => :followable, :class_name => "JamRuby::Follow", :dependent => :destroy
# # user likers (users who like current_user) # # user likers (users who like current_user)
# has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user # has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user