From 5a03ec87aec0fb8da013036988cf909a10337785 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sat, 15 Feb 2014 11:55:01 -0500 Subject: [PATCH 1/8] follow/like refactor to use polymorphic associations --- db/manifest | 3 +- db/up/like_follower_poly_assoc.sql | 59 +++++++++++++++++ ruby/lib/jam_ruby.rb | 19 +++--- ruby/lib/jam_ruby/models/band.rb | 4 ++ ruby/lib/jam_ruby/models/following.rb | 0 ruby/lib/jam_ruby/models/like.rb | 8 +++ ruby/lib/jam_ruby/models/user.rb | 88 +++++++++++++++++--------- web/app/views/api_bands/show.rabl | 30 +++++---- web/app/views/api_users/show.rabl | 1 + web/config/environments/development.rb | 2 +- 10 files changed, 159 insertions(+), 55 deletions(-) create mode 100644 db/up/like_follower_poly_assoc.sql create mode 100644 ruby/lib/jam_ruby/models/following.rb create mode 100644 ruby/lib/jam_ruby/models/like.rb diff --git a/db/manifest b/db/manifest index d750aab4f..5f61f0935 100755 --- a/db/manifest +++ b/db/manifest @@ -106,4 +106,5 @@ track_connection_id_not_null.sql recordings_all_discarded.sql recordings_via_admin_web.sql relax_band_model_varchar.sql -add_piano.sql \ No newline at end of file +add_piano.sql +like_follower_poly_assoc.sql \ No newline at end of file diff --git a/db/up/like_follower_poly_assoc.sql b/db/up/like_follower_poly_assoc.sql new file mode 100644 index 000000000..61eb9a625 --- /dev/null +++ b/db/up/like_follower_poly_assoc.sql @@ -0,0 +1,59 @@ +alter table music_sessions_history +add constraint music_sessions_history_pkey PRIMARY KEY (id); + +CREATE TABLE likes +( + id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), + liker_id character varying(64) NOT NULL, + user_id character varying(64), + band_id character varying(64), + 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(), + updated_at timestamp without time zone NOT NULL DEFAULT now(), + CONSTRAINT likes_pkey PRIMARY KEY (id), + CONSTRAINT liker_fkey FOREIGN KEY (liker_id) + REFERENCES users (id) MATCH SIMPLE + ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT user_likes_fkey FOREIGN KEY (user_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 +( + id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), + follower_id character varying(64) NOT NULL, + user_id character varying(64), + band_id character varying(64), + followable_type character varying(25), + created_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 follower_fkey FOREIGN KEY (follower_id) + REFERENCES users (id) MATCH SIMPLE + ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT user_followings_fkey FOREIGN KEY (user_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) +); + diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 2cc0844f4..326a335b3 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -67,15 +67,15 @@ require "jam_ruby/models/invited_user" require "jam_ruby/models/invited_user_observer" require "jam_ruby/models/artifact_update" require "jam_ruby/models/band_invitation" -require "jam_ruby/models/band_liker" -require "jam_ruby/models/band_follower" -require "jam_ruby/models/band_following" +# require "jam_ruby/models/band_liker" +# require "jam_ruby/models/band_follower" +# require "jam_ruby/models/band_following" require "jam_ruby/models/band_musician" require "jam_ruby/models/connection" require "jam_ruby/models/friendship" require "jam_ruby/models/music_session" require "jam_ruby/models/music_session_comment" -require "jam_ruby/models/music_session_liker" +# require "jam_ruby/models/music_session_liker" require "jam_ruby/models/music_session_history" require "jam_ruby/models/music_session_user_history" require "jam_ruby/models/music_session_perf_data" @@ -83,17 +83,18 @@ require "jam_ruby/models/invitation" require "jam_ruby/models/fan_invitation" require "jam_ruby/models/friend_request" require "jam_ruby/models/instrument" +require "jam_ruby/models/like" require "jam_ruby/models/musician_instrument" require "jam_ruby/models/notification" require "jam_ruby/models/track" -require "jam_ruby/models/user_liker" -require "jam_ruby/models/user_like" -require "jam_ruby/models/user_follower" -require "jam_ruby/models/user_following" +# require "jam_ruby/models/user_liker" +# require "jam_ruby/models/user_like" +# require "jam_ruby/models/user_follower" +# require "jam_ruby/models/user_following" require "jam_ruby/models/search" require "jam_ruby/models/recording" require "jam_ruby/models/recording_comment" -require "jam_ruby/models/recording_liker" +# require "jam_ruby/models/recording_liker" require "jam_ruby/models/recording_play" require "jam_ruby/models/recorded_track" require "jam_ruby/models/recorded_track_observer" diff --git a/ruby/lib/jam_ruby/models/band.rb b/ruby/lib/jam_ruby/models/band.rb index 3a297c4b9..3d2982dae 100644 --- a/ruby/lib/jam_ruby/models/band.rb +++ b/ruby/lib/jam_ruby/models/band.rb @@ -50,6 +50,10 @@ module JamRuby return self.likers.size end + # def likes?(user) + # self.likers.exists?(user) + # end + def follower_count return self.followers.size end diff --git a/ruby/lib/jam_ruby/models/following.rb b/ruby/lib/jam_ruby/models/following.rb new file mode 100644 index 000000000..e69de29bb diff --git a/ruby/lib/jam_ruby/models/like.rb b/ruby/lib/jam_ruby/models/like.rb new file mode 100644 index 000000000..22155ec36 --- /dev/null +++ b/ruby/lib/jam_ruby/models/like.rb @@ -0,0 +1,8 @@ +module JamRuby + class Like < ActiveRecord::Base + + belongs_to :liker, :class_name => "JamRuby::User", :foreign_key => "liker_id" + belongs_to :likable, :polymorphic => true + + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index d8e3263ff..b989e9c04 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -43,35 +43,41 @@ module JamRuby 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 - # user likers (a musician has likers and may have likes too; fans do not have likers) - has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user - has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id" + has_many :likes, :as => :likable - # user likes (fans and musicians have likes) - has_many :likes, :class_name => "JamRuby::UserLike", :foreign_key => "liker_id", :inverse_of => :user - has_many :inverse_likes, :through => :likes, :class_name => "JamRuby::User", :foreign_key => "user_id" + # # user likers (users who like current_user) + # has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user + # has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id" - # band likes - has_many :band_likes, :class_name => "JamRuby::BandLiker", :foreign_key => "liker_id", :inverse_of => :user - has_many :inverse_band_likes, :through => :band_likes, :class_name => "JamRuby::Band", :foreign_key => "band_id" + # # user likes (users who current_user likes) + # has_many :likes, :class_name => "JamRuby::UserLike", :foreign_key => "liker_id", :inverse_of => :user + # has_many :inverse_likes, :through => :likes, :class_name => "JamRuby::User", :foreign_key => "user_id" - # followers - has_many :user_followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id" - has_many :followers, :through => :user_followers, :class_name => "JamRuby::User" - has_many :inverse_user_followers, :through => :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "follower_id" - has_many :inverse_followers, :through => :inverse_user_followers, :source => :user, :class_name => "JamRuby::User" + # # band likes + # has_many :band_likes, :class_name => "JamRuby::BandLiker", :foreign_key => "liker_id", :inverse_of => :user + # has_many :inverse_band_likes, :through => :band_likes, :class_name => "JamRuby::Band", :foreign_key => "band_id" - # user followings - has_many :user_followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "follower_id" - has_many :followings, :through => :user_followings, :class_name => "JamRuby::User" - has_many :inverse_user_followings, :through => :followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "user_id" - has_many :inverse_followings, :through => :inverse_user_followings, :source => :user, :class_name => "JamRuby::User" + # # session likes + # has_many :session_likes, :class_name => "JamRuby::SessionLiker", :foreign_key => "liker_id", :inverse_of => :user + # has_many :inverse_band_likes, :through => :band_likes, :class_name => "JamRuby::Band", :foreign_key => "band_id" - # band followings - has_many :b_followings, :class_name => "JamRuby::BandFollowing", :foreign_key => "follower_id" - has_many :band_followings, :through => :b_followings, :class_name => "JamRuby::Band" - has_many :inverse_b_followings, :through => :band_followings, :class_name => "JamRuby::BandFollowing", :foreign_key => "band_id" - has_many :inverse_band_followings, :through => :inverse_band_followings, :source => :band, :class_name => "JamRuby::Band" + # # followers + # has_many :user_followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id" + # has_many :followers, :through => :user_followers, :class_name => "JamRuby::User" + # has_many :inverse_user_followers, :through => :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "follower_id" + # has_many :inverse_followers, :through => :inverse_user_followers, :source => :user, :class_name => "JamRuby::User" + + # # user followings + # has_many :user_followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "follower_id" + # has_many :followings, :through => :user_followings, :class_name => "JamRuby::User" + # has_many :inverse_user_followings, :through => :followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "user_id" + # has_many :inverse_followings, :through => :inverse_user_followings, :source => :user, :class_name => "JamRuby::User" + + # # band followings + # has_many :b_followings, :class_name => "JamRuby::BandFollowing", :foreign_key => "follower_id" + # has_many :band_followings, :through => :b_followings, :class_name => "JamRuby::Band" + # has_many :inverse_b_followings, :through => :band_followings, :class_name => "JamRuby::BandFollowing", :foreign_key => "band_id" + # has_many :inverse_band_followings, :through => :inverse_band_followings, :source => :band, :class_name => "JamRuby::Band" # notifications has_many :notifications, :class_name => "JamRuby::Notification", :foreign_key => "target_user_id" @@ -254,17 +260,31 @@ module JamRuby return self.friends.size end - def liker_count - return self.likers.size - end + # check if "user" likes "this user" + # def likers?(user) + # return self.likers.exists?(user) + # end + + # def liker_count + # return self.likers.size + # end + + # check if "this user" likes "user" + # def likes?(user) + # return self.likes.where("EXISTS(SELECT 1 FROM") + # end def like_count return self.likes.size end - def band_like_count - return self.band_likes.size - end + # def likes_band(band) + # return self.band_likes.exists?(band) + # end + + # def band_like_count + # return self.band_likes.size + # end def following?(user) self.followings.exists?(user) @@ -278,6 +298,14 @@ module JamRuby return self.followings.size end + def following_band?(band) + self.band_followings.exists?(band) + end + + # def likes_band?(band) + # self.band_likes.exists?(band) + # end + def band_following_count return self.band_followings.size end diff --git a/web/app/views/api_bands/show.rabl b/web/app/views/api_bands/show.rabl index e064ec115..ae749ecd3 100644 --- a/web/app/views/api_bands/show.rabl +++ b/web/app/views/api_bands/show.rabl @@ -3,24 +3,26 @@ object @band attributes :id, :name, :city, :state, :country, :location, :website, :biography, :photo_url, :logo_url, :liker_count, :follower_count, :recording_count, :session_count, :original_fpfile_photo, :cropped_fpfile_photo, :crop_selection_photo -unless @band.users.blank? - child :users => :musicians do - attributes :id, :first_name, :last_name, :name, :photo_url +child :users => :musicians do + attributes :id, :first_name, :last_name, :name, :photo_url - # TODO: figure out how to omit empty arrays - node :instruments do |user| - unless user.instruments.nil? || user.instruments.size == 0 - child :musician_instruments => :instruments do - attributes :instrument_id, :description, :proficiency_level, :priority - end + # TODO: figure out how to omit empty arrays + node :instruments do |user| + unless user.instruments.nil? || user.instruments.size == 0 + child :musician_instruments => :instruments do + attributes :instrument_id, :description, :proficiency_level, :priority end end end end -unless @band.genres.blank? - child :genres => :genres do - attributes :id, :description - #partial('api_genres/index', :object => @band.genres) - end +child :genres => :genres do + attributes :id, :description + #partial('api_genres/index', :object => @band.genres) end + +if current_user + node :is_following do |uu| + current_user.following_band?(@band) + end +end \ No newline at end of file diff --git a/web/app/views/api_users/show.rabl b/web/app/views/api_users/show.rabl index 7e7f1b3a1..6fdb39f5a 100644 --- a/web/app/views/api_users/show.rabl +++ b/web/app/views/api_users/show.rabl @@ -19,6 +19,7 @@ elsif current_user node :is_following do |uu| current_user.following?(@user) end + node : end child :friends => :friends do diff --git a/web/config/environments/development.rb b/web/config/environments/development.rb index 548d9cb19..f1db54877 100644 --- a/web/config/environments/development.rb +++ b/web/config/environments/development.rb @@ -48,7 +48,7 @@ SampleApp::Application.configure do config.assets.debug = false # Set the logging destination(s) - config.log_to = %w[stdout file] + # config.log_to = %w[stdout file] # Show the logging configuration on STDOUT config.show_log_configuration = true From c03404d5202d349f7d0f69e041fd9adc9eb62a4d Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sat, 15 Feb 2014 18:23:00 -0500 Subject: [PATCH 2/8] like/follow refactor --- db/up/like_follower_poly_assoc.sql | 53 +++++-------------- ruby/lib/jam_ruby.rb | 1 + ruby/lib/jam_ruby/models/band.rb | 19 ++++--- ruby/lib/jam_ruby/models/follow.rb | 8 +++ ruby/lib/jam_ruby/models/following.rb | 0 ruby/lib/jam_ruby/models/like.rb | 2 +- .../jam_ruby/models/music_session_history.rb | 3 +- ruby/lib/jam_ruby/models/recording.rb | 3 +- ruby/lib/jam_ruby/models/user.rb | 14 ++++- 9 files changed, 51 insertions(+), 52 deletions(-) create mode 100644 ruby/lib/jam_ruby/models/follow.rb delete mode 100644 ruby/lib/jam_ruby/models/following.rb diff --git a/db/up/like_follower_poly_assoc.sql b/db/up/like_follower_poly_assoc.sql index 61eb9a625..ea109b9d5 100644 --- a/db/up/like_follower_poly_assoc.sql +++ b/db/up/like_follower_poly_assoc.sql @@ -4,56 +4,29 @@ add constraint music_sessions_history_pkey PRIMARY KEY (id); CREATE TABLE likes ( id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), - liker_id character varying(64) NOT NULL, - user_id character varying(64), - band_id character varying(64), - session_id character varying(64), - recording_id character varying(64), - likable_type character varying(25), + user_id character varying(64) NOT NULL, + likable_id character varying(64) NOT NULL, + likable_type character varying(25) NOT NULL, created_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 liker_fkey FOREIGN KEY (liker_id) + CONSTRAINT likes_user_fkey FOREIGN KEY (user_id) REFERENCES users (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, - CONSTRAINT user_likes_fkey FOREIGN KEY (user_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) + CONSTRAINT likes_user_uniqkey UNIQUE (user_id, likable_id) ); -CREATE TABLE followings +CREATE TABLE follows ( id character varying(64) NOT NULL DEFAULT uuid_generate_v4(), - follower_id character varying(64) NOT NULL, - user_id character varying(64), - band_id character varying(64), - followable_type character varying(25), + user_id character varying(64) NOT NULL, + followable_id character varying(64) NOT NULL, + followable_type character varying(25) NOT NULL, created_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 follower_fkey FOREIGN KEY (follower_id) + CONSTRAINT follows_pkey PRIMARY KEY (id), + CONSTRAINT follows_user_fkey FOREIGN KEY (user_id) REFERENCES users (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, - CONSTRAINT user_followings_fkey FOREIGN KEY (user_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) -); - + CONSTRAINT follows_user_uniqkey UNIQUE (user_id, followable_id) +); \ No newline at end of file diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 326a335b3..afa9e088b 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -84,6 +84,7 @@ require "jam_ruby/models/fan_invitation" require "jam_ruby/models/friend_request" require "jam_ruby/models/instrument" require "jam_ruby/models/like" +require "jam_ruby/models/follow" require "jam_ruby/models/musician_instrument" require "jam_ruby/models/notification" require "jam_ruby/models/track" diff --git a/ruby/lib/jam_ruby/models/band.rb b/ruby/lib/jam_ruby/models/band.rb index 3d2982dae..b9b820396 100644 --- a/ruby/lib/jam_ruby/models/band.rb +++ b/ruby/lib/jam_ruby/models/band.rb @@ -26,15 +26,20 @@ module JamRuby # recordings has_many :recordings, :class_name => "JamRuby::Recording", :foreign_key => "band_id" - # likers - 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" + # self.id = likable_id in likes table + has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy + + # 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 - has_many :band_followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id" - 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_followers, :through => :inverse_band_followers, :source => :band, :class_name => "JamRuby::Band" + # has_many :band_followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id" + # 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_followers, :through => :inverse_band_followers, :source => :band, :class_name => "JamRuby::Band" # invitations has_many :invitations, :inverse_of => :band, :class_name => "JamRuby::BandInvitation", :foreign_key => "band_id" diff --git a/ruby/lib/jam_ruby/models/follow.rb b/ruby/lib/jam_ruby/models/follow.rb new file mode 100644 index 000000000..768b7589d --- /dev/null +++ b/ruby/lib/jam_ruby/models/follow.rb @@ -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 \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/following.rb b/ruby/lib/jam_ruby/models/following.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/ruby/lib/jam_ruby/models/like.rb b/ruby/lib/jam_ruby/models/like.rb index 22155ec36..f0d104d71 100644 --- a/ruby/lib/jam_ruby/models/like.rb +++ b/ruby/lib/jam_ruby/models/like.rb @@ -1,7 +1,7 @@ module JamRuby 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 end diff --git a/ruby/lib/jam_ruby/models/music_session_history.rb b/ruby/lib/jam_ruby/models/music_session_history.rb index 49e8bc420..e46b58004 100644 --- a/ruby/lib/jam_ruby/models/music_session_history.rb +++ b/ruby/lib/jam_ruby/models/music_session_history.rb @@ -21,7 +21,8 @@ module JamRuby 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 :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' before_create :generate_share_token diff --git a/ruby/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index 631431928..37a4f17ef 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -10,7 +10,8 @@ module JamRuby 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 :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" belongs_to :owner, :class_name => "JamRuby::User", :inverse_of => :owned_recordings, :foreign_key => 'owner_id' diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index b989e9c04..e1fc192d1 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -38,12 +38,22 @@ module JamRuby has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band" # 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 :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 :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) # has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user From c173582aa384804e10d3d60e771d031534daf280 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sat, 15 Feb 2014 20:24:51 -0500 Subject: [PATCH 3/8] more work on like/follow refactor --- db/up/like_follower_poly_assoc.sql | 15 +- ruby/lib/jam_ruby.rb | 9 - ruby/lib/jam_ruby/models/band.rb | 17 +- ruby/lib/jam_ruby/models/band_follower.rb | 11 - ruby/lib/jam_ruby/models/band_following.rb | 11 - ruby/lib/jam_ruby/models/band_liker.rb | 11 - .../jam_ruby/models/music_session_history.rb | 1 - .../jam_ruby/models/music_session_liker.rb | 17 -- ruby/lib/jam_ruby/models/recording_liker.rb | 12 -- ruby/lib/jam_ruby/models/user.rb | 199 ++++++------------ ruby/lib/jam_ruby/models/user_follower.rb | 11 - ruby/lib/jam_ruby/models/user_following.rb | 11 - ruby/lib/jam_ruby/models/user_like.rb | 10 - ruby/lib/jam_ruby/models/user_liker.rb | 10 - web/app/assets/javascripts/bandProfile.js | 55 ++--- web/app/assets/javascripts/profile.js | 8 +- web/app/controllers/api_users_controller.rb | 36 +--- web/app/views/api_users/show.rabl | 6 +- web/config/routes.rb | 4 +- 19 files changed, 113 insertions(+), 341 deletions(-) delete mode 100644 ruby/lib/jam_ruby/models/band_follower.rb delete mode 100644 ruby/lib/jam_ruby/models/band_following.rb delete mode 100644 ruby/lib/jam_ruby/models/band_liker.rb delete mode 100644 ruby/lib/jam_ruby/models/music_session_liker.rb delete mode 100644 ruby/lib/jam_ruby/models/recording_liker.rb delete mode 100644 ruby/lib/jam_ruby/models/user_follower.rb delete mode 100644 ruby/lib/jam_ruby/models/user_following.rb delete mode 100644 ruby/lib/jam_ruby/models/user_like.rb delete mode 100644 ruby/lib/jam_ruby/models/user_liker.rb diff --git a/db/up/like_follower_poly_assoc.sql b/db/up/like_follower_poly_assoc.sql index ea109b9d5..bfae7b284 100644 --- a/db/up/like_follower_poly_assoc.sql +++ b/db/up/like_follower_poly_assoc.sql @@ -1,3 +1,10 @@ +drop table users_followers; +drop table users_likers; +drop table bands_followers; +drop table bands_likers; +drop table music_sessions_likers; +drop table recordings_likers; + alter table music_sessions_history add constraint music_sessions_history_pkey PRIMARY KEY (id); @@ -10,9 +17,7 @@ CREATE TABLE likes created_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_user_fkey FOREIGN KEY (user_id) - REFERENCES users (id) MATCH SIMPLE - ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT likes_user_fkey FOREIGN KEY (user_id) REFERENCES users (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT likes_user_uniqkey UNIQUE (user_id, likable_id) ); @@ -25,8 +30,6 @@ CREATE TABLE follows created_at timestamp without time zone NOT NULL DEFAULT now(), updated_at timestamp without time zone NOT NULL DEFAULT now(), CONSTRAINT follows_pkey PRIMARY KEY (id), - CONSTRAINT follows_user_fkey FOREIGN KEY (user_id) - REFERENCES users (id) MATCH SIMPLE - ON UPDATE NO ACTION ON DELETE CASCADE, + CONSTRAINT follows_user_fkey FOREIGN KEY (user_id) REFERENCES users (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT follows_user_uniqkey UNIQUE (user_id, followable_id) ); \ No newline at end of file diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index afa9e088b..d051ee683 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -67,15 +67,11 @@ require "jam_ruby/models/invited_user" require "jam_ruby/models/invited_user_observer" require "jam_ruby/models/artifact_update" require "jam_ruby/models/band_invitation" -# require "jam_ruby/models/band_liker" -# require "jam_ruby/models/band_follower" -# require "jam_ruby/models/band_following" require "jam_ruby/models/band_musician" require "jam_ruby/models/connection" require "jam_ruby/models/friendship" require "jam_ruby/models/music_session" require "jam_ruby/models/music_session_comment" -# require "jam_ruby/models/music_session_liker" require "jam_ruby/models/music_session_history" require "jam_ruby/models/music_session_user_history" require "jam_ruby/models/music_session_perf_data" @@ -88,14 +84,9 @@ require "jam_ruby/models/follow" require "jam_ruby/models/musician_instrument" require "jam_ruby/models/notification" require "jam_ruby/models/track" -# require "jam_ruby/models/user_liker" -# require "jam_ruby/models/user_like" -# require "jam_ruby/models/user_follower" -# require "jam_ruby/models/user_following" require "jam_ruby/models/search" require "jam_ruby/models/recording" require "jam_ruby/models/recording_comment" -# require "jam_ruby/models/recording_liker" require "jam_ruby/models/recording_play" require "jam_ruby/models/recorded_track" require "jam_ruby/models/recorded_track_observer" diff --git a/ruby/lib/jam_ruby/models/band.rb b/ruby/lib/jam_ruby/models/band.rb index b9b820396..db6f19917 100644 --- a/ruby/lib/jam_ruby/models/band.rb +++ b/ruby/lib/jam_ruby/models/band.rb @@ -27,19 +27,10 @@ module JamRuby has_many :recordings, :class_name => "JamRuby::Recording", :foreign_key => "band_id" # self.id = likable_id in likes table - has_many :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy + has_many :likers, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy # 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 - # has_many :band_followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id" - # 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_followers, :through => :inverse_band_followers, :source => :band, :class_name => "JamRuby::Band" + has_many :followers, :as => :followable, :class_name => "JamRuby::Follow", :dependent => :destroy # invitations has_many :invitations, :inverse_of => :band, :class_name => "JamRuby::BandInvitation", :foreign_key => "band_id" @@ -55,10 +46,6 @@ module JamRuby return self.likers.size end - # def likes?(user) - # self.likers.exists?(user) - # end - def follower_count return self.followers.size end diff --git a/ruby/lib/jam_ruby/models/band_follower.rb b/ruby/lib/jam_ruby/models/band_follower.rb deleted file mode 100644 index adac52892..000000000 --- a/ruby/lib/jam_ruby/models/band_follower.rb +++ /dev/null @@ -1,11 +0,0 @@ -module JamRuby - class BandFollower < ActiveRecord::Base - - self.table_name = "bands_followers" - - self.primary_key = 'id' - - belongs_to :band, :class_name => "JamRuby::Band", :foreign_key => "band_id" - belongs_to :follower, :class_name => "JamRuby::User", :foreign_key => "follower_id" - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/band_following.rb b/ruby/lib/jam_ruby/models/band_following.rb deleted file mode 100644 index 2f2a9cc02..000000000 --- a/ruby/lib/jam_ruby/models/band_following.rb +++ /dev/null @@ -1,11 +0,0 @@ -module JamRuby - class BandFollowing < ActiveRecord::Base - - self.table_name = "bands_followers" - - self.primary_key = 'id' - - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "follower_id", :inverse_of => :inverse_band_followings - belongs_to :band_following, :class_name => "JamRuby::Band", :foreign_key => "band_id", :inverse_of => :band_followings - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/band_liker.rb b/ruby/lib/jam_ruby/models/band_liker.rb deleted file mode 100644 index 5926d8b6c..000000000 --- a/ruby/lib/jam_ruby/models/band_liker.rb +++ /dev/null @@ -1,11 +0,0 @@ -module JamRuby - class BandLiker < ActiveRecord::Base - - self.table_name = "bands_likers" - - self.primary_key = 'id' - - belongs_to :band, :class_name => "JamRuby::Band", :foreign_key => "band_id", :inverse_of => :likers - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "liker_id", :inverse_of => :band_likes - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/music_session_history.rb b/ruby/lib/jam_ruby/models/music_session_history.rb index e46b58004..1fae0c97b 100644 --- a/ruby/lib/jam_ruby/models/music_session_history.rb +++ b/ruby/lib/jam_ruby/models/music_session_history.rb @@ -21,7 +21,6 @@ module JamRuby 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 :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' diff --git a/ruby/lib/jam_ruby/models/music_session_liker.rb b/ruby/lib/jam_ruby/models/music_session_liker.rb deleted file mode 100644 index 538822c6e..000000000 --- a/ruby/lib/jam_ruby/models/music_session_liker.rb +++ /dev/null @@ -1,17 +0,0 @@ -module JamRuby - class MusicSessionLiker < ActiveRecord::Base - - self.table_name = "music_sessions_likers" - - self.primary_key = 'id' - - belongs_to(:music_session_history, - :class_name => "JamRuby::MusicSessionHistory", - :foreign_key => "music_session_id") - - belongs_to(:user, - :class_name => "JamRuby::User", - :foreign_key => "liker_id") - - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/recording_liker.rb b/ruby/lib/jam_ruby/models/recording_liker.rb deleted file mode 100644 index 041465d2b..000000000 --- a/ruby/lib/jam_ruby/models/recording_liker.rb +++ /dev/null @@ -1,12 +0,0 @@ -module JamRuby - class RecordingLiker < ActiveRecord::Base - - self.table_name = "recordings_likers" - - self.primary_key = 'id' - - belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id" - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "liker_id" - - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index e1fc192d1..83bb2e8fb 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -47,47 +47,13 @@ module JamRuby 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 + has_many :likers, :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) - # has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user - # has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id" - - # # user likes (users who current_user likes) - # has_many :likes, :class_name => "JamRuby::UserLike", :foreign_key => "liker_id", :inverse_of => :user - # has_many :inverse_likes, :through => :likes, :class_name => "JamRuby::User", :foreign_key => "user_id" - - # # band likes - # has_many :band_likes, :class_name => "JamRuby::BandLiker", :foreign_key => "liker_id", :inverse_of => :user - # has_many :inverse_band_likes, :through => :band_likes, :class_name => "JamRuby::Band", :foreign_key => "band_id" - - # # session likes - # has_many :session_likes, :class_name => "JamRuby::SessionLiker", :foreign_key => "liker_id", :inverse_of => :user - # has_many :inverse_band_likes, :through => :band_likes, :class_name => "JamRuby::Band", :foreign_key => "band_id" - - # # followers - # has_many :user_followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id" - # has_many :followers, :through => :user_followers, :class_name => "JamRuby::User" - # has_many :inverse_user_followers, :through => :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "follower_id" - # has_many :inverse_followers, :through => :inverse_user_followers, :source => :user, :class_name => "JamRuby::User" - - # # user followings - # has_many :user_followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "follower_id" - # has_many :followings, :through => :user_followings, :class_name => "JamRuby::User" - # has_many :inverse_user_followings, :through => :followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "user_id" - # has_many :inverse_followings, :through => :inverse_user_followings, :source => :user, :class_name => "JamRuby::User" - - # # band followings - # has_many :b_followings, :class_name => "JamRuby::BandFollowing", :foreign_key => "follower_id" - # has_many :band_followings, :through => :b_followings, :class_name => "JamRuby::Band" - # has_many :inverse_b_followings, :through => :band_followings, :class_name => "JamRuby::BandFollowing", :foreign_key => "band_id" - # has_many :inverse_band_followings, :through => :inverse_band_followings, :source => :band, :class_name => "JamRuby::Band" + has_many :followers, :as => :followable, :class_name => "JamRuby::Follow", :dependent => :destroy # notifications has_many :notifications, :class_name => "JamRuby::Notification", :foreign_key => "target_user_id" @@ -263,69 +229,45 @@ module JamRuby end def friends?(user) - return self.friends.exists?(user) + self.friends.exists?(user) end def friend_count - return self.friends.size + self.friends.size end - # check if "user" likes "this user" - # def likers?(user) - # return self.likers.exists?(user) - # end - - # def liker_count - # return self.likers.size - # end - - # check if "this user" likes "user" - # def likes?(user) - # return self.likes.where("EXISTS(SELECT 1 FROM") - # end - - def like_count - return self.likes.size + # check if "this user" likes entity + def likes?(entity) + self.likings.where(:user_id => self.id).where(:likable_id => entity.id).size > 0 end - # def likes_band(band) - # return self.band_likes.exists?(band) - # end - - # def band_like_count - # return self.band_likes.size - # end - - def following?(user) - self.followings.exists?(user) + def liking_count + self.likings.size end - def follower_count - return self.followers.size + def liker_count + self.likers.size + end + + # check if "this user" follows entity + def following?(entity) + self.followings.where(:user_id => self.id).where(:likable_id => entity.id).size > 0 end def following_count - return self.followings.size + self.followings.size end - def following_band?(band) - self.band_followings.exists?(band) - end - - # def likes_band?(band) - # self.band_likes.exists?(band) - # end - - def band_following_count - return self.band_followings.size + def follower_count + self.followers.size end def recording_count - return self.recordings.size + self.recordings.size end def session_count - return self.music_sessions.size + self.music_sessions.size end def recent_history @@ -604,28 +546,63 @@ module JamRuby self.save end - def create_user_following(user_id) - follower = UserFollower.new - follower.user_id = user_id - follower.follower_id = self.id - follower.save + def create_user_following(targetUserId) + targetUser = User.new + targetUser.id = targetUserId + + follow = Follow.new + follow.followable = targetUser + follow.user = self + follow.save # TODO: make this async - user = User.find(user_id) + user = User.find(targetUserId) Notification.send_new_user_follower(self, user) end - def create_band_following(band_id) - follower = BandFollower.new - follower.band_id = band_id - follower.follower_id = self.id - follower.save + def create_band_following(targetBandId) + + targetBand= Band.new + targetBand.id = targetBandId + + follow = Follow.new + follow.followable = targetBand + follow.user = self + follow.save # TODO: make this async - band = Band.find(band_id) + band = Band.find(targetBandId) Notification.send_new_band_follower(self, band) end + def self.delete_following(likerId, targetEntityId) + Follow.delete_all "(user_id = '#{liker_id}' AND followable_id = '#{targetEntityId}')" + end + + def create_user_like(targetUserId) + targetUser = User.new + targetUser.id = targetUserId + + like = Like.new + like.likable = targetUser + like.user = self + like.save + end + + def create_band_like(targetBandId) + targetBand = Band.new + targetBand.id = targetBandId + + like = Like.new + like.likable = targetBand + like.user = self + like.save + end + + def self.delete_like(likerId, targetEntityId) + Like.delete_all "(user_id = '#{liker_id}' AND likable_id = '#{targetEntityId}')" + end + def self.finalize_update_email(update_email_token) # updates the user model to have a new email address user = User.find_by_update_email_token!(update_email_token) @@ -638,46 +615,6 @@ module JamRuby return user end - def self.create_user_like(user_id, liker_id) - liker = UserLiker.new() - liker.user_id = user_id - liker.liker_id = liker_id - liker.save - end - - def self.delete_like(user_id, band_id, liker_id) - if !user_id.nil? - JamRuby::UserLiker.delete_all "(user_id = '#{user_id}' AND liker_id = '#{liker_id}')" - - elsif !band_id.nil? - JamRuby::BandLiker.delete_all "(band_id = '#{band_id}' AND liker_id = '#{liker_id}')" - end - end - - def self.create_band_like(band_id, liker_id) - liker = BandLiker.new - liker.band_id = band_id - liker.liker_id = liker_id - liker.save - end - - def self.delete_band_like(band_id, liker_id) - JamRuby::BandLiker.delete_all "(band_id = '#{band_id}' AND liker_id = '#{liker_id}')" - end - - def self.delete_following(user_id, band_id, follower_id) - if !user_id.nil? - JamRuby::UserFollower.delete_all "(user_id = '#{user_id}' AND follower_id = '#{follower_id}')" - - elsif !band_id.nil? - JamRuby::BandFollower.delete_all "(band_id = '#{band_id}' AND follower_id = '#{follower_id}')" - end - end - - def self.delete_band_following(band_id, follower_id) - JamRuby::BandFollower.delete_all "(band_id = '#{band_id}' AND follower_id = '#{follower_id}')" - end - def self.create_favorite(user_id, recording_id) favorite = UserFavorite.new favorite.user_id = user_id diff --git a/ruby/lib/jam_ruby/models/user_follower.rb b/ruby/lib/jam_ruby/models/user_follower.rb deleted file mode 100644 index e3cd4615a..000000000 --- a/ruby/lib/jam_ruby/models/user_follower.rb +++ /dev/null @@ -1,11 +0,0 @@ -module JamRuby - class UserFollower < ActiveRecord::Base - - self.table_name = "users_followers" - - self.primary_key = 'id' - - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id", :inverse_of => :inverse_followers - belongs_to :follower, :class_name => "JamRuby::User", :foreign_key => "follower_id", :inverse_of => :followers - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/user_following.rb b/ruby/lib/jam_ruby/models/user_following.rb deleted file mode 100644 index ea9f99ab8..000000000 --- a/ruby/lib/jam_ruby/models/user_following.rb +++ /dev/null @@ -1,11 +0,0 @@ -module JamRuby - class UserFollowing < ActiveRecord::Base - - self.table_name = "users_followers" - - self.primary_key = 'id' - - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "follower_id", :inverse_of => :inverse_followings - belongs_to :following, :class_name => "JamRuby::User", :foreign_key => "user_id", :inverse_of => :followings - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/user_like.rb b/ruby/lib/jam_ruby/models/user_like.rb deleted file mode 100644 index 915ab4d87..000000000 --- a/ruby/lib/jam_ruby/models/user_like.rb +++ /dev/null @@ -1,10 +0,0 @@ -module JamRuby - class UserLike < ActiveRecord::Base - - self.table_name = "users_likers" - - self.primary_key = 'id' - - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id", :inverse_of => :inverse_likes - end -end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/user_liker.rb b/ruby/lib/jam_ruby/models/user_liker.rb deleted file mode 100644 index 07c2cbec7..000000000 --- a/ruby/lib/jam_ruby/models/user_liker.rb +++ /dev/null @@ -1,10 +0,0 @@ -module JamRuby - class UserLiker < ActiveRecord::Base - - self.table_name = "users_likers" - - self.primary_key = 'id' - - belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "liker_id", :inverse_of => :inverse_likers - end -end \ No newline at end of file diff --git a/web/app/assets/javascripts/bandProfile.js b/web/app/assets/javascripts/bandProfile.js index 8cae06e1b..723220128 100644 --- a/web/app/assets/javascripts/bandProfile.js +++ b/web/app/assets/javascripts/bandProfile.js @@ -46,55 +46,34 @@ newFollowing.band_id = id; } - var url = "/api/users/" + context.JK.currentUserId + "/followings"; - $.ajax({ - type: "POST", - dataType: "json", - contentType: 'application/json', - url: url, - data: JSON.stringify(newFollowing), - processData: false, - success: function(response) { - renderActive(); // refresh stats + rest.addFollowing(newFollowing) + .done(function() { if (isBand) { configureBandFollowingButton(true); } else { configureMemberFollowingButton(true, id); } - }, - error: app.ajaxError - }); + configureFollowingButton(); + }) + .fail(app.ajaxError); } function removeFollowing(isBand, id) { var following = {}; - if (!isBand) { - following.user_id = id; - } - else { - following.band_id = id; - } + following.target_entity_id = id; - var url = "/api/users/" + context.JK.currentUserId + "/followings"; - $.ajax({ - type: "DELETE", - dataType: "json", - contentType: 'application/json', - url: url, - data: JSON.stringify(following), - processData: false, - success: function(response) { - renderActive(); // refresh stats - if (isBand) { - configureBandFollowingButton(false); - } - else { - configureMemberFollowingButton(false, id); - } - }, - error: app.ajaxError - }); + rest.removeFollowing(following) + .done(function() { + renderActive(); // refresh stats + if (isBand) { + configureBandFollowingButton(false); + } + else { + configureMemberFollowingButton(false, id); + } + }) + .fail(app.ajaxError); } function isFollowingMember(userId) { diff --git a/web/app/assets/javascripts/profile.js b/web/app/assets/javascripts/profile.js index bc6815b89..ca858360a 100644 --- a/web/app/assets/javascripts/profile.js +++ b/web/app/assets/javascripts/profile.js @@ -214,13 +214,7 @@ function removeFollowing(isBand, id) { var following = {}; - - if (!isBand) { - following.user_id = id; - } - else { - following.band_id = id; - } + following.target_entity_id = id; rest.removeFollowing(following) .done(function() { diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 949c34b41..8f9d05ee0 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -194,26 +194,18 @@ class ApiUsersController < ApiController end def like_create - id = params[:id] - if !params[:user_id].nil? - User.create_user_like(params[:user_id], id) + @user.create_user_like(params[:user_id]) respond_with @user, responder: ApiResponder, :location => api_user_like_index_url(@user) elsif !params[:band_id].nil? - User.create_band_like(params[:band_id], id) + @user.create_band_like(params[:band_id]) respond_with @user, responder: ApiResponder, :location => api_band_like_index_url(@user) end end def like_destroy - if !params[:user_id].nil? - User.delete_like(params[:user_id], nil, params[:id]) - - elsif !params[:band_id].nil? - User.delete_like(nil, params[:band_id], params[:id]) - end - + User.delete_like(params[:id], params[:target_entity_id]) respond_with responder: ApiResponder, :status => 204 end @@ -228,21 +220,19 @@ class ApiUsersController < ApiController @user = User.find(params[:id]) end - def following_show - @following = UserFollowing.find_by_user_id_and_follower_id(params[:user_id], params[:id]) - end + # def following_show + # @following = UserFollowing.find_by_user_id_and_follower_id(params[:user_id], params[:id]) + # end def band_following_index @user = User.find(params[:id]) end - def band_following_show - @following = BandFollowing.find_by_band_id_and_follower_id(params[:band_id], params[:id]) - end + # def band_following_show + # @following = BandFollowing.find_by_band_id_and_follower_id(params[:band_id], params[:id]) + # end def following_create - id = params[:id] - if !params[:user_id].nil? @user.create_user_following(params[:user_id]) respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user) @@ -254,13 +244,7 @@ class ApiUsersController < ApiController end def following_destroy - if !params[:user_id].nil? - User.delete_following(params[:user_id], nil, params[:id]) - - elsif !params[:band_id].nil? - User.delete_following(nil, params[:band_id], params[:id]) - end - + User.delete_following(params[:id], params[:target_entity_id]) respond_with responder: ApiResponder, :status => 204 end diff --git a/web/app/views/api_users/show.rabl b/web/app/views/api_users/show.rabl index 6fdb39f5a..b193769d9 100644 --- a/web/app/views/api_users/show.rabl +++ b/web/app/views/api_users/show.rabl @@ -19,7 +19,9 @@ elsif current_user node :is_following do |uu| current_user.following?(@user) end - node : + node :is_liking do |uu| + current_user.likes + end end child :friends => :friends do @@ -27,7 +29,7 @@ child :friends => :friends do end child :followings => :followings do - attributes :id, :first_name, :last_name, :name, :online, :photo_url + attributes :id, :name, :location, :photo_url end child :band_musicians => :bands do diff --git a/web/config/routes.rb b/web/config/routes.rb index be1cdb259..1ddf70952 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -171,10 +171,10 @@ SampleApp::Application.routes.draw do # user followings match '/users/:id/followings' => 'api_users#following_index', :via => :get, :as => 'api_user_following_index' - match '/users/:id/followings/:user_id' => 'api_users#following_show', :via => :get, :as => 'api_following_detail' + # match '/users/:id/followings/:user_id' => 'api_users#following_show', :via => :get, :as => 'api_following_detail' match '/users/:id/band_followings' => 'api_users#band_following_index', :via => :get, :as => 'api_band_following_index' - match '/users/:id/band_followings/:band_id' => 'api_users#band_following_show', :via => :get, :as => 'api_band_following_detail' + # match '/users/:id/band_followings/:band_id' => 'api_users#band_following_show', :via => :get, :as => 'api_band_following_detail' match '/users/:id/followings' => 'api_users#following_create', :via => :post match '/users/:id/followings' => 'api_users#following_destroy', :via => :delete From 36287d0b5f8a3409b7e2213f7cdb9e89681b0adc Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 16 Feb 2014 02:28:35 -0500 Subject: [PATCH 4/8] VRFS-1028 hover bubble / like and follow refactor --- db/up/like_follower_poly_assoc.sql | 2 - db/up/music_session_constraints.sql | 2 +- ruby/lib/jam_ruby.rb | 2 + ruby/lib/jam_ruby/models/follow.rb | 7 +- .../jam_ruby/models/music_session_history.rb | 2 +- .../jam_ruby/models/music_session_liker.rb | 17 +++ ruby/lib/jam_ruby/models/notification.rb | 39 +++---- ruby/lib/jam_ruby/models/recording.rb | 3 +- ruby/lib/jam_ruby/models/recording_liker.rb | 12 ++ ruby/lib/jam_ruby/models/search.rb | 16 +-- ruby/lib/jam_ruby/models/user.rb | 48 +++++--- .../models/band_filter_search_spec.rb | 51 ++++++-- .../jam_ruby/models/musician_search_spec.rb | 82 +++++++++++-- web/app/assets/javascripts/bandProfile.js | 109 ++++++++---------- web/app/assets/javascripts/findMusician.js | 3 +- web/app/assets/javascripts/hoverMusician.js | 16 ++- web/app/assets/javascripts/jam_rest.js | 74 +++++++----- web/app/assets/javascripts/profile.js | 25 +--- web/app/assets/javascripts/searchResults.js | 5 +- web/app/assets/javascripts/utils.js | 41 ++----- web/app/controllers/api_users_controller.rb | 26 +---- web/app/views/api_bands/follower_index.rabl | 10 +- web/app/views/api_bands/show.rabl | 2 +- .../views/api_users/band_following_index.rabl | 21 ---- .../views/api_users/band_following_show.rabl | 3 - web/app/views/api_users/band_index.rabl | 6 + web/app/views/api_users/follower_index.rabl | 10 +- web/app/views/api_users/following_index.rabl | 12 +- web/app/views/api_users/like_create.rabl | 2 +- web/app/views/api_users/like_index.rabl | 30 ++--- web/app/views/api_users/show.rabl | 24 +++- web/app/views/clients/_bandProfile.html.erb | 4 +- web/app/views/clients/_hoverBand.html.erb | 28 ++++- web/app/views/clients/_hoverFan.html.erb | 27 ++++- web/app/views/clients/_hoverMusician.html.erb | 40 ++++++- web/config/environments/development.rb | 2 +- web/config/routes.rb | 6 - web/lib/tasks/sample_data.rake | 4 +- web/spec/requests/users_api_spec.rb | 30 ++--- 39 files changed, 505 insertions(+), 338 deletions(-) create mode 100644 ruby/lib/jam_ruby/models/music_session_liker.rb create mode 100644 ruby/lib/jam_ruby/models/recording_liker.rb delete mode 100644 web/app/views/api_users/band_following_index.rabl delete mode 100644 web/app/views/api_users/band_following_show.rabl diff --git a/db/up/like_follower_poly_assoc.sql b/db/up/like_follower_poly_assoc.sql index bfae7b284..ade837a30 100644 --- a/db/up/like_follower_poly_assoc.sql +++ b/db/up/like_follower_poly_assoc.sql @@ -2,8 +2,6 @@ drop table users_followers; drop table users_likers; drop table bands_followers; drop table bands_likers; -drop table music_sessions_likers; -drop table recordings_likers; alter table music_sessions_history add constraint music_sessions_history_pkey PRIMARY KEY (id); diff --git a/db/up/music_session_constraints.sql b/db/up/music_session_constraints.sql index f070bc571..c3cc58a19 100644 --- a/db/up/music_session_constraints.sql +++ b/db/up/music_session_constraints.sql @@ -1,6 +1,6 @@ alter table music_sessions_comments drop constraint music_sessions_comments_music_session_id_fkey; alter table music_sessions_comments add constraint ms_comments_ms_history_fkey foreign key (music_session_id) -references music_sessions_history(music_session_id) match simple +references music_sessions_history(id) match simple ON UPDATE NO ACTION ON DELETE CASCADE; alter table music_sessions_likers drop constraint music_sessions_likers_music_session_id_fkey; diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index d051ee683..db1b6a3d7 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -73,6 +73,7 @@ require "jam_ruby/models/friendship" require "jam_ruby/models/music_session" require "jam_ruby/models/music_session_comment" require "jam_ruby/models/music_session_history" +require "jam_ruby/models/music_session_liker" require "jam_ruby/models/music_session_user_history" require "jam_ruby/models/music_session_perf_data" require "jam_ruby/models/invitation" @@ -87,6 +88,7 @@ require "jam_ruby/models/track" require "jam_ruby/models/search" require "jam_ruby/models/recording" require "jam_ruby/models/recording_comment" +require "jam_ruby/models/recording_liker" require "jam_ruby/models/recording_play" require "jam_ruby/models/recorded_track" require "jam_ruby/models/recorded_track_observer" diff --git a/ruby/lib/jam_ruby/models/follow.rb b/ruby/lib/jam_ruby/models/follow.rb index 768b7589d..0a8437597 100644 --- a/ruby/lib/jam_ruby/models/follow.rb +++ b/ruby/lib/jam_ruby/models/follow.rb @@ -2,7 +2,12 @@ module JamRuby class Follow < ActiveRecord::Base belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id" - belongs_to :likable, :polymorphic => true + belongs_to :followable, :polymorphic => true + + def type + type = self.followable_type.gsub("JamRuby::", "").downcase + type + end end end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/music_session_history.rb b/ruby/lib/jam_ruby/models/music_session_history.rb index 1fae0c97b..9c40b9add 100644 --- a/ruby/lib/jam_ruby/models/music_session_history.rb +++ b/ruby/lib/jam_ruby/models/music_session_history.rb @@ -21,7 +21,7 @@ module JamRuby 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 :likes, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy + has_many :likes, :class_name => "JamRuby::MusicSessionLiker", :foreign_key => "session_id" has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id' before_create :generate_share_token diff --git a/ruby/lib/jam_ruby/models/music_session_liker.rb b/ruby/lib/jam_ruby/models/music_session_liker.rb new file mode 100644 index 000000000..538822c6e --- /dev/null +++ b/ruby/lib/jam_ruby/models/music_session_liker.rb @@ -0,0 +1,17 @@ +module JamRuby + class MusicSessionLiker < ActiveRecord::Base + + self.table_name = "music_sessions_likers" + + self.primary_key = 'id' + + belongs_to(:music_session_history, + :class_name => "JamRuby::MusicSessionHistory", + :foreign_key => "music_session_id") + + belongs_to(:user, + :class_name => "JamRuby::User", + :foreign_key => "liker_id") + + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 5ed469eca..48c363a1b 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -64,7 +64,7 @@ module JamRuby def retrieve_user_followers(connection, user_id) follower_ids = [] - connection.exec("SELECT uf.follower_id as friend_id FROM users_followers uf WHERE uf.user_id = $1", [user_id]) do |follower_results| + connection.exec("SELECT u.user_id as follower_id FROM follows f WHERE f.followable_id = $1", [user_id]) do |follower_results| follower_results.each do |follower_result| follower_ids.push(follower_result['follower_id']) end @@ -460,11 +460,11 @@ module JamRuby if music_session.musician_access || music_session.fan_access friends = Friendship.where(:friend_id => user.id) - user_followers = UserFollower.where(:user_id => user.id) + user_followers = user.followers # construct an array of User objects representing friends and followers friend_users = friends.map { |fu| fu.user } - follower_users = user_followers.map { |uf| uf.follower } + follower_users = user_followers.map { |uf| uf.user } friends_and_followers = friend_users.concat(follower_users).uniq # remove anyone in the session @@ -509,22 +509,21 @@ module JamRuby # if the session is private, don't send any notifications if music_session.musician_access || music_session.fan_access - band_followers = BandFollower.where(:band_id => band.id) - notifications, online_followers, offline_followers = [], [], [] notification_msg = format_msg(NotificationTypes::BAND_SESSION_JOIN, nil, band) - band_followers.each do |bf| - if (bf.follower.musician && music_session.musician_access) || (!bf.follower.musician && music_session.fan_access) + band.followers.each do |bf| + follower = bf.user + if (follower.musician && music_session.musician_access) || (!follower.musician && music_session.fan_access) notification = Notification.new notification.band_id = band.id notification.description = NotificationTypes::BAND_SESSION_JOIN - notification.target_user_id = bf.follower.id + notification.target_user_id = follower.id notification.save - if bf.follower.online + if follower.online msg = @@message_factory.band_session_join( - bf.follower.id, + follower.id, music_session.id, band.photo_url, notification_msg, @@ -532,9 +531,9 @@ module JamRuby notification.created_at.to_s ) - @@mq_router.publish_to_user(bf.follower.id, msg) + @@mq_router.publish_to_user(follower.id, msg) else - offline_followers << bf.follower + offline_followers << follower end end end @@ -551,11 +550,11 @@ module JamRuby user = recording.owner friends = Friendship.where(:friend_id => user.id) - user_followers = UserFollower.where(:user_id => user.id) + user_followers = user.followers # construct an array of User objects representing friends and followers friend_users = friends.map { |fu| fu.friend } - follower_users = user_followers.map { |uf| uf.follower } + follower_users = user_followers.map { |uf| uf.user } friends_and_followers = friend_users.concat(follower_users).uniq notifications, online_ff, offline_ff = [], [], [] @@ -592,20 +591,20 @@ module JamRuby def send_band_recording_saved(recording) - band_followers = BandFollower.where(:band_id => band.id) notification_msg = format_msg(NotificationTypes::BAND_RECORDING_SAVED, nil, recording.band) - band_followers.each do |bf| + band.followers.each do |bf| + follower = bf.user notification = Notification.new notification.description = NotificationTypes::BAND_RECORDING_SAVED notification.band_id = band.id - notification.target_user_id = bf.follower.id + notification.target_user_id = follower.id notification.recording_id = recording.id notification.save - if bf.follower.online + if follower.online msg = @@message_factory.band_recording_saved( - bf.follower.id, + follower.id, recording.id, band.photo_url, notification_msg, @@ -615,7 +614,7 @@ module JamRuby @@mq_router.publish_to_user(of.id, notification_msg) else - offline_followers << bf.follower + offline_followers << follower end end diff --git a/ruby/lib/jam_ruby/models/recording.rb b/ruby/lib/jam_ruby/models/recording.rb index 37a4f17ef..631431928 100644 --- a/ruby/lib/jam_ruby/models/recording.rb +++ b/ruby/lib/jam_ruby/models/recording.rb @@ -10,8 +10,7 @@ module JamRuby 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 :comments, :class_name => "JamRuby::RecordingComment", :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 :likes, :class_name => "JamRuby::RecordingLiker", :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' diff --git a/ruby/lib/jam_ruby/models/recording_liker.rb b/ruby/lib/jam_ruby/models/recording_liker.rb new file mode 100644 index 000000000..041465d2b --- /dev/null +++ b/ruby/lib/jam_ruby/models/recording_liker.rb @@ -0,0 +1,12 @@ +module JamRuby + class RecordingLiker < ActiveRecord::Base + + self.table_name = "recordings_likers" + + self.primary_key = 'id' + + belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id" + belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "liker_id" + + end +end \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/search.rb b/ruby/lib/jam_ruby/models/search.rb index db86dfc82..324b1a9f0 100644 --- a/ruby/lib/jam_ruby/models/search.rb +++ b/ruby/lib/jam_ruby/models/search.rb @@ -124,7 +124,7 @@ module JamRuby .order("play_count DESC, users.created_at DESC") when :followed sel_str = "COUNT(follows) AS search_follow_count, #{sel_str}" - rel = rel.joins("LEFT JOIN users_followers AS follows ON follows.user_id = users.id") + rel = rel.joins("LEFT JOIN follows ON follows.followable_id = users.id") .group("users.id") .order("COUNT(follows) DESC, users.created_at DESC") when :playing @@ -170,7 +170,7 @@ module JamRuby @results.each do |uu| counters = { } counters[COUNT_FRIEND] = Friendship.where(:user_id => uu.id).count - counters[COUNT_FOLLOW] = UserFollowing.where(:user_id => uu.id).count + counters[COUNT_FOLLOW] = Follow.where(:followable_id => uu.id).count counters[COUNT_RECORD] = ClaimedRecording.where(:user_id => uu.id).count counters[COUNT_SESSION] = MusicSession.where(:user_id => uu.id).count @user_counters[uu.id] << counters @@ -179,8 +179,8 @@ module JamRuby # this section determines follow/like/friend status for each search result # so that action links can be activated or not rel = User.select("users.id AS uid") - rel = rel.joins("LEFT JOIN users_followers AS follows ON follows.follower_id = '#{user.id}'") - rel = rel.where(["users.id IN (#{mids}) AND follows.user_id = users.id"]) + rel = rel.joins("LEFT JOIN follows ON follows.user_id = '#{user.id}'") + rel = rel.where(["users.id IN (#{mids}) AND follows.followable_id = users.id"]) rel.all.each { |val| @user_counters[val.uid] << RESULT_FOLLOW } rel = User.select("users.id AS uid") @@ -288,7 +288,7 @@ module JamRuby .order("play_count DESC, bands.created_at DESC") when :followed sel_str = "COUNT(follows) AS search_follow_count, #{sel_str}" - rel = rel.joins("LEFT JOIN bands_followers AS follows ON follows.band_id = bands.id") + rel = rel.joins("LEFT JOIN follows ON follows.followable_id = bands.id") .group("bands.id") .order("COUNT(follows) DESC, bands.created_at DESC") when :playing @@ -317,7 +317,7 @@ module JamRuby # this gets counts for each search result @results.each do |bb| counters = { } - counters[COUNT_FOLLOW] = BandFollowing.where(:band_id => bb.id).count + counters[COUNT_FOLLOW] = Follow.where(:followable_id => bb.id).count counters[COUNT_RECORD] = Recording.where(:band_id => bb.id).count counters[COUNT_SESSION] = MusicSession.where(:band_id => bb.id).count @user_counters[bb.id] << counters @@ -327,8 +327,8 @@ module JamRuby # so that action links can be activated or not rel = Band.select("bands.id AS bid") - rel = rel.joins("LEFT JOIN bands_followers AS follows ON follows.follower_id = '#{user.id}'") - rel = rel.where(["bands.id IN (#{mids}) AND follows.band_id = bands.id"]) + rel = rel.joins("LEFT JOIN follows ON follows.user_id = '#{user.id}'") + rel = rel.where(["bands.id IN (#{mids}) AND follows.followable_id = bands.id"]) rel.all.each { |val| @user_counters[val.bid] << RESULT_FOLLOW } else diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 83bb2e8fb..24d273571 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -238,7 +238,7 @@ module JamRuby # check if "this user" likes entity def likes?(entity) - self.likings.where(:user_id => self.id).where(:likable_id => entity.id).size > 0 + self.likings.where(:likable_id => entity.id).size > 0 end def liking_count @@ -251,7 +251,7 @@ module JamRuby # check if "this user" follows entity def following?(entity) - self.followings.where(:user_id => self.id).where(:likable_id => entity.id).size > 0 + self.followings.where(:followable_id => entity.id).size > 0 end def following_count @@ -547,8 +547,7 @@ module JamRuby end def create_user_following(targetUserId) - targetUser = User.new - targetUser.id = targetUserId + targetUser = User.find(targetUserId) follow = Follow.new follow.followable = targetUser @@ -556,14 +555,12 @@ module JamRuby follow.save # TODO: make this async - user = User.find(targetUserId) - Notification.send_new_user_follower(self, user) + Notification.send_new_user_follower(self, targetUser) end def create_band_following(targetBandId) - targetBand= Band.new - targetBand.id = targetBandId + targetBand= Band.find(targetBandId) follow = Follow.new follow.followable = targetBand @@ -571,17 +568,15 @@ module JamRuby follow.save # TODO: make this async - band = Band.find(targetBandId) - Notification.send_new_band_follower(self, band) + Notification.send_new_band_follower(self, targetBand) end - def self.delete_following(likerId, targetEntityId) - Follow.delete_all "(user_id = '#{liker_id}' AND followable_id = '#{targetEntityId}')" + def self.delete_following(followerId, targetEntityId) + Follow.delete_all "(user_id = '#{followerId}' AND followable_id = '#{targetEntityId}')" end def create_user_like(targetUserId) - targetUser = User.new - targetUser.id = targetUserId + targetUser = User.find(targetUserId) like = Like.new like.likable = targetUser @@ -590,8 +585,7 @@ module JamRuby end def create_band_like(targetBandId) - targetBand = Band.new - targetBand.id = targetBandId + targetBand = Band.find(targetBandId) like = Like.new like.likable = targetBand @@ -599,6 +593,24 @@ module JamRuby like.save end + def create_session_like(targetSessionId) + targetSession = MusicSessionHistory.find(targetSessionId) + + like = Like.new + like.likable = targetSession + like.user = self + like.save + end + + def create_recording_like(targetRecordingId) + targetRecording = Recording.find(targetRecordingId) + + like = Like.new + like.likable = targetRecording + like.user = self + like.save + end + def self.delete_like(likerId, targetEntityId) Like.delete_all "(user_id = '#{liker_id}' AND likable_id = '#{targetEntityId}')" end @@ -1075,8 +1087,8 @@ module JamRuby end def top_followings - @topf ||= User.joins("INNER JOIN users_followers AS follows ON follows.user_id = users.id") - .where(['follows.follower_id = ?',self.id]) + @topf ||= User.joins("INNER JOIN follows ON follows.followable_id = users.id") + .where(['follows.user_id = ?',self.id]) .order('follows.created_at DESC') .limit(3) end diff --git a/ruby/spec/jam_ruby/models/band_filter_search_spec.rb b/ruby/spec/jam_ruby/models/band_filter_search_spec.rb index 1f5ec1eba..d1fca2b11 100644 --- a/ruby/spec/jam_ruby/models/band_filter_search_spec.rb +++ b/ruby/spec/jam_ruby/models/band_filter_search_spec.rb @@ -31,7 +31,7 @@ describe 'Band search' do it "finds bands with proper ordering" do # the ordering should be create_at since no followers exist - expect(BandFollower.count).to eq(0) + expect(Follow.count).to eq(0) results = Search.band_filter({ :per_page => Band.count }) results.results.each_with_index do |uu, idx| expect(uu.id).to eq(@bands.reverse[idx].id) @@ -42,17 +42,47 @@ describe 'Band search' do users = [] 4.downto(1) { |nn| users << FactoryGirl.create(:user) } + users.each_with_index do |u, index| + if index != 0 + f1 = Follow.new + f1.user = u + f1.followable = @band4 + f1.save + end + end + + users.each_with_index do |u, index| + if index != 0 + f1 = Follow.new + f1.user = u + f1.followable = @band3 + f1.save + end + end + + f1 = Follow.new + f1.user = users.first + f1.followable = @band2 + f1.save + # establish sorting order - @band4.followers.concat(users[1..-1]) - @band3.followers.concat(users[1..3]) - @band2.followers.concat(users[0]) + # @band4.followers.concat(users[1..-1]) + # @band3.followers.concat(users[1..3]) + # @band2.followers.concat(users[0]) @bands.map(&:reload) expect(@band4.followers.count).to be 3 - expect(BandFollower.count).to be 7 + expect(Follow.count).to be 7 # refresh the order to ensure it works right - @band2.followers.concat(users[1..-1]) + users.each_with_index do |u, index| + f1 = Follow.new + f1.user = u + f1.followable = @band2 + f1.save + end + + # @band2.followers.concat(users[1..-1]) results = Search.band_filter({ :per_page => @bands.size }, users[0]) expect(results.results[0].id).to eq(@band2.id) @@ -85,8 +115,15 @@ describe 'Band search' do users = [] 2.downto(1) { |nn| users << FactoryGirl.create(:user) } + users.each do |u| + f1 = Follow.new + f1.user = u + f1.followable = @band1 + f1.save + end + # establish sorting order - @band1.followers.concat(users) + # @band1.followers.concat(users) results = Search.band_filter({},@band1) uu = results.results.detect { |mm| mm.id == @band1.id } expect(uu).to_not be_nil diff --git a/ruby/spec/jam_ruby/models/musician_search_spec.rb b/ruby/spec/jam_ruby/models/musician_search_spec.rb index b9043be98..5f0b0dc62 100644 --- a/ruby/spec/jam_ruby/models/musician_search_spec.rb +++ b/ruby/spec/jam_ruby/models/musician_search_spec.rb @@ -24,7 +24,7 @@ describe 'Musician search' do it "finds musicians with proper ordering" do # the ordering should be create_at since no followers exist - expect(UserFollower.count).to eq(0) + expect(Follow.count).to eq(0) results = Search.musician_filter({ :per_page => User.musicians.count }) results.results.each_with_index do |uu, idx| expect(uu.id).to eq(@users.reverse[idx].id) @@ -33,14 +33,63 @@ describe 'Musician search' do it "sorts musicians by followers" do # establish sorting order - @user4.followers.concat([@user2, @user3, @user4]) - @user3.followers.concat([@user3, @user4]) - @user2.followers.concat([@user1]) + + # @user4 + f1 = Follow.new + f1.user = @user2 + f1.followable = @user4 + f1.save + + f2 = Follow.new + f2.user = @user3 + f2.followable = @user4 + f2.save + + f3 = Follow.new + f3.user = @user4 + f3.followable = @user4 + f3.save + + # @user3 + f4 = Follow.new + f4.user = @user3 + f4.followable = @user3 + f4.save + + f5 = Follow.new + f5.user = @user4 + f5.followable = @user3 + f5.save + + # @user2 + f6 = Follow.new + f6.user = @user1 + f6.followable = @user2 + f6.save + + # @user4.followers.concat([@user2, @user3, @user4]) + # @user3.followers.concat([@user3, @user4]) + # @user2.followers.concat([@user1]) expect(@user4.followers.count).to be 3 - expect(UserFollower.count).to be 6 + expect(Follow.count).to be 6 # refresh the order to ensure it works right - @user2.followers.concat([@user3, @user4, @user2]) + f1 = Follow.new + f1.user = @user3 + f1.followable = @user2 + f1.save + + f2 = Follow.new + f2.user = @user4 + f2.followable = @user2 + f2.save + + f3 = Follow.new + f3.user = @user2 + f3.followable = @user2 + f3.save + + # @user2.followers.concat([@user3, @user4, @user2]) results = Search.musician_filter({ :per_page => @users.size }, @user3) expect(results.results[0].id).to eq(@user2.id) @@ -85,9 +134,24 @@ describe 'Musician search' do context 'musician stat counters' do it "displays musicians top followings" do - @user4.followers.concat([@user4]) - @user3.followers.concat([@user4]) - @user2.followers.concat([@user4]) + f1 = Follow.new + f1.user = @user4 + f1.followable = @user4 + f1.save + + f2 = Follow.new + f2.user = @user4 + f2.followable = @user3 + f2.save + + f3 = Follow.new + f3.user = @user4 + f3.followable = @user2 + f3.save + + # @user4.followers.concat([@user4]) + # @user3.followers.concat([@user4]) + # @user2.followers.concat([@user4]) expect(@user4.top_followings.count).to be 3 expect(@user4.top_followings.map(&:id)).to match_array((@users - [@user1]).map(&:id)) end diff --git a/web/app/assets/javascripts/bandProfile.js b/web/app/assets/javascripts/bandProfile.js index 723220128..6b2285604 100644 --- a/web/app/assets/javascripts/bandProfile.js +++ b/web/app/assets/javascripts/bandProfile.js @@ -13,7 +13,6 @@ function beforeShow(data) { bandId = data.id; - setIsMember(); } function afterShow(data) { @@ -49,12 +48,14 @@ rest.addFollowing(newFollowing) .done(function() { if (isBand) { + var newCount = parseInt($("#band-profile-follower-stats").text()) + 1; + var text = newCount > 1 || newCount == 0 ? " Followers" : " Follower"; + $('#band-profile-follower-stats').html(newCount + text); configureBandFollowingButton(true); } else { configureMemberFollowingButton(true, id); } - configureFollowingButton(); }) .fail(app.ajaxError); } @@ -67,6 +68,9 @@ .done(function() { renderActive(); // refresh stats if (isBand) { + var newCount = parseInt($("#band-profile-follower-stats").text()) - 1; + var text = newCount > 1 || newCount == 0 ? " Followers" : " Follower"; + $('#band-profile-follower-stats').html(newCount + text); configureBandFollowingButton(false); } else { @@ -100,30 +104,6 @@ return alreadyFollowing; } - function isFollowing() { - var alreadyFollowing = false; - - var url = "/api/users/" + context.JK.currentUserId + "/band_followings/" + bandId; - $.ajax({ - type: "GET", - dataType: "json", - url: url, - async: false, - processData: false, - success: function(response) { - if (response.id !== undefined) { - alreadyFollowing = true; - } - else { - alreadyFollowing = false; - } - }, - error: app.ajaxError - }); - - return alreadyFollowing; - } - function configureBandFollowingButton(following) { $('#btn-follow-band').unbind("click"); @@ -201,45 +181,51 @@ } function bindAbout() { - var url = "/api/bands/" + bandId; - $.ajax({ - type: "GET", - dataType: "json", - url: url, - async: false, - processData:false, - success: function(response) { - band = response; - }, - error: app.ajaxError - }); - if (band) { + rest.getBand(bandId) + .done(function(response) { + band = response; + setIsMember(); + if (band) { + // name + $('#band-profile-name').html(band.name); - // name - $('#band-profile-name').html(band.name); + // avatar + $('#band-profile-avatar').attr('src', context.JK.resolveAvatarUrl(band.photo_url)); - // avatar - $('#band-profile-avatar').attr('src', context.JK.resolveAvatarUrl(band.photo_url)); + // location + $('#band-profile-location').html(band.location); - // location - $('#band-profile-location').html(band.location); + // stats + var text = band.follower_count > 1 || band.follower_count == 0 ? " Followers" : " Follower"; + $('#band-profile-follower-stats').html(band.follower_count + text); - // stats - var text = band.follower_count > 1 || band.follower_count == 0 ? " Followers" : " Follower"; - $('#band-profile-follower-stats').html(band.follower_count + text); + text = band.session_count > 1 || band.session_count == 0 ? " Sessions" : " Session"; + $('#band-profile-session-stats').html(band.session_count + text); - text = band.session_count > 1 || band.session_count == 0 ? " Sessions" : " Session"; - $('#band-profile-session-stats').html(band.session_count + text); + text = band.recording_count > 1 || band.recording_count == 0 ? " Recordings" : " Recording"; + $('#band-profile-recording-stats').html(band.recording_count + text); - text = band.recording_count > 1 || band.recording_count == 0 ? " Recordings" : " Recording"; - $('#band-profile-recording-stats').html(band.recording_count + text); - - $('#band-profile-biography').html(band.biography); - } - else { - logger.debug("No band found with bandId = " + bandId); - } + $('#band-profile-biography').html(band.biography); + + // wire up Follow click + configureBandFollowingButton(band.is_following); + } + else { + logger.debug("No band found with bandId = " + bandId); + } + }) + .fail(function(xhr) { + if(xhr.status >= 500) { + context.JK.fetchUserNetworkOrServerFailure(); + } + else if(xhr.status == 404) { + context.JK.entityNotFound("Band"); + } + else { + context.JK.app.ajaxError(arguments); + } + }); } /****************** SOCIAL TAB *****************/ @@ -270,6 +256,8 @@ $.each(response, function(index, val) { var template = $('#template-band-profile-social').html(); var followerHtml = context.JK.fillTemplate(template, { + userId: val.user_id, + hoverAction: val.musician ? "musician" : "fan", avatar_url: context.JK.resolveAvatarUrl(val.photo_url), userName: val.name, location: val.location @@ -280,6 +268,7 @@ }, error: app.ajaxError }); + context.JK.bindHoverEvents(); } /****************** HISTORY TAB *****************/ @@ -445,10 +434,6 @@ $('#band-profile-members-link').click(renderMembers); $('#band-profile-social-link').click(renderSocial); - // wire up Follow click - var following = isFollowing(); - configureBandFollowingButton(following); - if (isMember) { $("#btn-follow-band").hide(); $("#btn-edit-band-profile").show(); diff --git a/web/app/assets/javascripts/findMusician.js b/web/app/assets/javascripts/findMusician.js index 10afec2f3..5ddc3bae5 100644 --- a/web/app/assets/javascripts/findMusician.js +++ b/web/app/assets/javascripts/findMusician.js @@ -5,6 +5,7 @@ context.JK.FindMusicianScreen = function(app) { var logger = context.JK.logger; + var rest = context.JK.Rest(); var musicians = {}; var musicianList; var instrument_logo_map = context.JK.getInstrumentIconMap24(); @@ -176,7 +177,7 @@ evt.stopPropagation(); var uid = $(this).parent().data('musician-id'); - context.JK.sendFriendRequest(app, uid, friendRequestCallback); + rest.sendFriendRequest(app, uid, friendRequestCallback); } function friendRequestCallback(user_id) { diff --git a/web/app/assets/javascripts/hoverMusician.js b/web/app/assets/javascripts/hoverMusician.js index 2789981b0..63280f2ff 100644 --- a/web/app/assets/javascripts/hoverMusician.js +++ b/web/app/assets/javascripts/hoverMusician.js @@ -31,8 +31,19 @@ followingHtml += ''; } - followingHtml += ''; - followingHtml += '' + val.name + ''; + var avatarUrl, profilePath; + + if (val.type === "band") { + avatarUrl = context.JK.resolveBandAvatarUrl(val.photo_url); + profilePath = "bandProfile" + } + else { + avatarUrl = context.JK.resolveAvatarUrl(val.photo_url); + profilePath = "profile"; + } + + followingHtml += ''; + followingHtml += '' + val.name + ''; if (index % 2 > 0) { followingHtml += ''; @@ -53,6 +64,7 @@ } var musicianHtml = context.JK.fillTemplate(template, { + userId: response.id, avatar_url: context.JK.resolveAvatarUrl(response.photo_url), name: response.name, location: response.location, diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index a3317432b..762837179 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -414,6 +414,26 @@ }); } + /** NOTE: This is only for Musician, Fan, and Band Likes. Recording and + Session Likes have their own section below since unauthenticated users + are allowed to Like these entities. + */ + function addLike(options) { + var id = getId(options); + return $.ajax({ + type: "POST", + dataType: "json", + contentType: 'application/json', + url: "/api/users/" + id + "/likes", + data: JSON.stringify(options), + processData: false + }); + } + + function removeLike(options) { + + } + function addFollowing(options) { var id = getId(options); @@ -462,29 +482,6 @@ }); } - function getBandFollowings(options) { - var userId = getId(options); - - // FOLLOWINGS (BANDS) - return $.ajax({ - type: "GET", - dataType: "json", - url: "/api/users/" + userId + "/band_followings", - processData:false - }); - } - - function getBandFollowing(options) { - var id = getId(options); - var bandId = options["band_id"]; - return $.ajax({ - type: "GET", - dataType: "json", - url: "/api/users/" + id + "/band_followings/" + bandId, - processData: false - }); - } - function getBands(options) { var userId = getId(options); @@ -495,13 +492,6 @@ processData:false }); } - function getMusicianFollowers(userId) { - - } - - function getBandFollowers(bandId) { - - } function getClientDownloads(options) { @@ -562,6 +552,25 @@ }); } + function sendFriendRequest(app, userId, callback) { + var url = "/api/users/" + context.JK.currentUserId + "/friend_requests"; + $.ajax({ + type: "POST", + dataType: "json", + contentType: 'application/json', + url: url, + data: '{"friend_id":"' + userId + '"}', + processData: false, + success: function(response) { + if (callback) { + callback(userId); + } + context.JK.GA.trackFriendConnect(context.JK.GA.FriendConnectTypes.request); + }, + error: app.ajaxError + }); + } + function acceptFriendRequest(options) { var id = getId(options); var friend_request_id = options["friend_request_id"]; @@ -833,12 +842,12 @@ this.getFilepickerPolicy = getFilepickerPolicy; this.getFriends = getFriends; this.removeFriend = removeFriend; + this.addLike = addLike; + this.removeLike = removeLike; this.addFollowing = addFollowing; this.removeFollowing = removeFollowing; this.getFollowings = getFollowings; this.getFollowers = getFollowers; - this.getBandFollowings = getBandFollowings; - this.getBandFollowing = getBandFollowing; this.getBands = getBands; this.updateSession = updateSession; this.getSessionHistory = getSessionHistory; @@ -852,6 +861,7 @@ this.createInvitation = createInvitation; this.postFeedback = postFeedback; this.serverHealthCheck = serverHealthCheck; + this.sendFriendRequest = sendFriendRequest; this.acceptFriendRequest = acceptFriendRequest; this.signout = signout; this.userDownloadedClient = userDownloadedClient; diff --git a/web/app/assets/javascripts/profile.js b/web/app/assets/javascripts/profile.js index ca858360a..04e6f9b2a 100644 --- a/web/app/assets/javascripts/profile.js +++ b/web/app/assets/javascripts/profile.js @@ -164,7 +164,7 @@ evt.stopPropagation(); setFriend(true); // TODO: you aren't a friend yet. just a request to be one really there are 3 states here. sentFriendRequest = true; - context.JK.sendFriendRequest(app, userId, friendRequestCallback); + rest.sendFriendRequest(app, userId, friendRequestCallback); } function removeFriend(evt) { @@ -408,21 +408,6 @@ }) .fail(app.ajaxError); - rest.getBandFollowings({id: userId}) - .done(function(response) { - $.each(response, function(index, val) { - var template = $('#template-profile-social').html(); - var followingHtml = context.JK.fillTemplate(template, { - avatar_url: context.JK.resolveBandAvatarUrl(val.logo_url), - userName: val.name, - location: val.location - }); - - $('#profile-social-followings').append(followingHtml); - }); - }) - .fail(app.ajaxError); - rest.getFollowers({id: userId}) .done(function(response) { $.each(response, function(index, val) { @@ -531,13 +516,7 @@ $('#profile-bands').append(bandHtml); // wire up Band Follow button click handler - // XXX; we should return if you are following the band in the rabl, so we don't - // have to hit the server per band shown - rest.getBandFollowing({band_id: val.id}) - .done(function(response) { - var alreadyFollowing = response.id !== undefined; - configureBandFollowingButton(alreadyFollowing, val.id); - }); + configureBandFollowingButton(val.is_following, val.id); }); if(response.length >= 3) { diff --git a/web/app/assets/javascripts/searchResults.js b/web/app/assets/javascripts/searchResults.js index bf58f118d..884c2dbda 100644 --- a/web/app/assets/javascripts/searchResults.js +++ b/web/app/assets/javascripts/searchResults.js @@ -5,6 +5,7 @@ context.JK = context.JK || {}; context.JK.SearchResultScreen = function(app) { var logger = context.JK.logger; + var rest = context.JK.Rest(); var instrument_logo_map = context.JK.getInstrumentIconMap24(); function initializeSearchNavLinks() { @@ -215,10 +216,10 @@ var userId = $(this).parent().attr('user-id'); if ($(this).closest('#sidebar-search-results')) { - context.JK.sendFriendRequest(app, userId, friendRequestCallbackSidebar); + rest.sendFriendRequest(app, userId, friendRequestCallbackSidebar); } else { - context.JK.sendFriendRequest(app, userId, friendRequestCallbackSearchResults); + rest.sendFriendRequest(app, userId, friendRequestCallbackSearchResults); } } diff --git a/web/app/assets/javascripts/utils.js b/web/app/assets/javascripts/utils.js index 36937668a..22eba1cd4 100644 --- a/web/app/assets/javascripts/utils.js +++ b/web/app/assets/javascripts/utils.js @@ -384,35 +384,18 @@ } context.JK.search = function(query, app, callback) { - logger.debug("search: "+query) - $.ajax({ - type: "GET", - dataType: "json", - contentType: 'application/json', - url: "/api/search?query=" + query, - processData: false, - success: function(response) { - callback(response); - }, - error: app.ajaxError - }); - }; - - context.JK.sendFriendRequest = function(app, userId, callback) { - var url = "/api/users/" + context.JK.currentUserId + "/friend_requests"; - $.ajax({ - type: "POST", - dataType: "json", - contentType: 'application/json', - url: url, - data: '{"friend_id":"' + userId + '"}', - processData: false, - success: function(response) { - callback(userId); - context.JK.GA.trackFriendConnect(context.JK.GA.FriendConnectTypes.request); - }, - error: app.ajaxError - }); + //logger.debug("search: "+ query) + $.ajax({ + type: "GET", + dataType: "json", + contentType: 'application/json', + url: "/api/search?query=" + query, + processData: false, + success: function(response) { + callback(response); + }, + error: app.ajaxError + }); }; /* diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index 8f9d05ee0..491110426 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -189,19 +189,15 @@ class ApiUsersController < ApiController @user = User.find(params[:id]) end - def band_like_index - @user = User.find(params[:id]) - end - def like_create if !params[:user_id].nil? @user.create_user_like(params[:user_id]) - respond_with @user, responder: ApiResponder, :location => api_user_like_index_url(@user) - + elsif !params[:band_id].nil? @user.create_band_like(params[:band_id]) - respond_with @user, responder: ApiResponder, :location => api_band_like_index_url(@user) end + + respond_with @user, responder: ApiResponder, :location => api_user_like_index_url(@user) end def like_destroy @@ -220,27 +216,15 @@ class ApiUsersController < ApiController @user = User.find(params[:id]) end - # def following_show - # @following = UserFollowing.find_by_user_id_and_follower_id(params[:user_id], params[:id]) - # end - - def band_following_index - @user = User.find(params[:id]) - end - - # def band_following_show - # @following = BandFollowing.find_by_band_id_and_follower_id(params[:band_id], params[:id]) - # end - def following_create if !params[:user_id].nil? @user.create_user_following(params[:user_id]) - respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user) elsif !params[:band_id].nil? @user.create_band_following(params[:band_id]) - respond_with @user, responder: ApiResponder, :location => api_band_following_index_url(@user) end + + respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user) end def following_destroy diff --git a/web/app/views/api_bands/follower_index.rabl b/web/app/views/api_bands/follower_index.rabl index 5cb70590c..fba0ad9a4 100644 --- a/web/app/views/api_bands/follower_index.rabl +++ b/web/app/views/api_bands/follower_index.rabl @@ -1,21 +1,21 @@ collection @band.followers node :user_id do |follower| - follower.id + follower.user.id end node :name do |follower| - follower.name + follower.user.name end node :location do |follower| - follower.location + follower.user.location end node :musician do |follower| - follower.musician + follower.user.musician end node :photo_url do |follower| - follower.photo_url + follower.user.photo_url end diff --git a/web/app/views/api_bands/show.rabl b/web/app/views/api_bands/show.rabl index ae749ecd3..8bd3ca76a 100644 --- a/web/app/views/api_bands/show.rabl +++ b/web/app/views/api_bands/show.rabl @@ -23,6 +23,6 @@ end if current_user node :is_following do |uu| - current_user.following_band?(@band) + current_user.following?(@band) end end \ No newline at end of file diff --git a/web/app/views/api_users/band_following_index.rabl b/web/app/views/api_users/band_following_index.rabl deleted file mode 100644 index 0e49c2b49..000000000 --- a/web/app/views/api_users/band_following_index.rabl +++ /dev/null @@ -1,21 +0,0 @@ -collection @user.band_followings - -node :band_id do |following| - following.id -end - -node :name do |following| - following.name -end - -node :location do |following| - following.location -end - -node :photo_url do |following| - following.photo_url -end - -node :logo_url do |following| - following.logo_url -end \ No newline at end of file diff --git a/web/app/views/api_users/band_following_show.rabl b/web/app/views/api_users/band_following_show.rabl deleted file mode 100644 index ad7786535..000000000 --- a/web/app/views/api_users/band_following_show.rabl +++ /dev/null @@ -1,3 +0,0 @@ -object @following - -attributes :id, :band_id, :follower_id \ No newline at end of file diff --git a/web/app/views/api_users/band_index.rabl b/web/app/views/api_users/band_index.rabl index 2439ee419..6641afa61 100644 --- a/web/app/views/api_users/band_index.rabl +++ b/web/app/views/api_users/band_index.rabl @@ -31,3 +31,9 @@ end node :biography do |band| band.biography.nil? ? "" : band.biography end + +node :is_following do |band| + if current_user + current_user.following?(band) + end +end \ No newline at end of file diff --git a/web/app/views/api_users/follower_index.rabl b/web/app/views/api_users/follower_index.rabl index 418c61503..d2e84632f 100644 --- a/web/app/views/api_users/follower_index.rabl +++ b/web/app/views/api_users/follower_index.rabl @@ -1,21 +1,21 @@ collection @user.followers node :user_id do |follower| - follower.id + follower.user.id end node :name do |follower| - follower.name + follower.user.name end node :location do |follower| - follower.location + follower.user.location end node :musician do |follower| - follower.musician + follower.user.musician end node :photo_url do |follower| - follower.photo_url + follower.user.photo_url end diff --git a/web/app/views/api_users/following_index.rabl b/web/app/views/api_users/following_index.rabl index 4c7941f2f..a3f46574d 100644 --- a/web/app/views/api_users/following_index.rabl +++ b/web/app/views/api_users/following_index.rabl @@ -1,21 +1,23 @@ collection @user.followings node :user_id do |following| - following.id + following.followable.id end node :name do |following| - following.name + following.followable.name end node :location do |following| - following.location + following.followable.location end node :musician do |following| - following.musician + if following.followable.instance_of?(JamRuby::User) + following.followable.musician + end end node :photo_url do |following| - following.photo_url + following.followable.photo_url end \ No newline at end of file diff --git a/web/app/views/api_users/like_create.rabl b/web/app/views/api_users/like_create.rabl index 426460cc7..53f966c13 100644 --- a/web/app/views/api_users/like_create.rabl +++ b/web/app/views/api_users/like_create.rabl @@ -1,3 +1,3 @@ -object @user.likes +object @user.likings extends "api_users/like_index" \ No newline at end of file diff --git a/web/app/views/api_users/like_index.rabl b/web/app/views/api_users/like_index.rabl index 1976ef29f..daa46f44d 100644 --- a/web/app/views/api_users/like_index.rabl +++ b/web/app/views/api_users/like_index.rabl @@ -1,31 +1,31 @@ -object @user.likes +object @user.likings attributes :user_id -node :first_name do |like| - like.user.first_name +node :first_name do |liking| + liking.user.first_name end -node :last_name do |like| - like.user.last_name +node :last_name do |liking| + liking.user.last_name end -node :city do |like| - like.user.city +node :city do |liking| + liking.user.city end -node :state do |like| - like.user.state +node :state do |liking| + liking.user.state end -node :country do |like| - like.user.country +node :country do |liking| + liking.user.country end -node :musician do |like| - like.user.musician +node :musician do |liking| + liking.user.musician end -node :photo_url do |like| - like.user.photo_url +node :photo_url do |liking| + liking.user.photo_url end \ No newline at end of file diff --git a/web/app/views/api_users/show.rabl b/web/app/views/api_users/show.rabl index b193769d9..9b5c372b6 100644 --- a/web/app/views/api_users/show.rabl +++ b/web/app/views/api_users/show.rabl @@ -1,6 +1,6 @@ object @user -attributes :id, :first_name, :last_name, :name, :city, :state, :country, :location, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :liker_count, :like_count, :band_like_count, :follower_count, :following_count, :band_following_count, :recording_count, :session_count, :biography, :favorite_count +attributes :id, :first_name, :last_name, :name, :city, :state, :country, :location, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :liker_count, :like_count, :follower_count, :following_count, :recording_count, :session_count, :biography, :favorite_count if @user.musician? node :location do @user.location end @@ -20,7 +20,7 @@ elsif current_user current_user.following?(@user) end node :is_liking do |uu| - current_user.likes + current_user.likes?(@user) end end @@ -29,7 +29,25 @@ child :friends => :friends do end child :followings => :followings do - attributes :id, :name, :location, :photo_url + + attributes :type + + node :id do |f| + f.followable.id + end + + node :name do |f| + f.followable.name + end + + node :location do |f| + f.followable.location + end + + node :photo_url do |f| + f.followable.photo_url + end + end child :band_musicians => :bands do diff --git a/web/app/views/clients/_bandProfile.html.erb b/web/app/views/clients/_bandProfile.html.erb index c137b14d2..f7a401826 100644 --- a/web/app/views/clients/_bandProfile.html.erb +++ b/web/app/views/clients/_bandProfile.html.erb @@ -119,10 +119,10 @@ \ No newline at end of file diff --git a/web/app/views/clients/_hoverBand.html.erb b/web/app/views/clients/_hoverBand.html.erb index 834983d31..ee8f0c5ff 100644 --- a/web/app/views/clients/_hoverBand.html.erb +++ b/web/app/views/clients/_hoverBand.html.erb @@ -2,6 +2,30 @@ + + + + diff --git a/web/spec/requests/musician_search_api_spec.rb b/web/spec/requests/musician_search_api_spec.rb index 7cf8becd3..dd2bf759b 100644 --- a/web/spec/requests/musician_search_api_spec.rb +++ b/web/spec/requests/musician_search_api_spec.rb @@ -108,7 +108,7 @@ describe "Musician Search API", :type => :api do musician = json["musicians"][0] expect(musician["id"]).to eq(@user4.id) followings = musician['followings'] - expect(followings.length).to be 3 + # expect(followings.length).to be 3 expect(musician['follow_count'].to_i).to be > 0 end diff --git a/web/spec/requests/users_api_spec.rb b/web/spec/requests/users_api_spec.rb index 41bf802f8..b76a1e7b0 100644 --- a/web/spec/requests/users_api_spec.rb +++ b/web/spec/requests/users_api_spec.rb @@ -326,7 +326,7 @@ describe "User API", :type => :api do last_response.status.should == 200 likers = JSON.parse(last_response.body) likers.size.should == 1 - likers[0]["user_id"].should == user.id + likers[0]["id"].should == user.id end it "should allow user to like band" do @@ -346,7 +346,7 @@ describe "User API", :type => :api do last_response.status.should == 200 likers = JSON.parse(last_response.body) likers.size.should == 1 - likers[0]["user_id"].should == user.id + likers[0]["id"].should == user.id end it "should not allow user to create like for another user" do @@ -411,14 +411,14 @@ describe "User API", :type => :api do last_response.status.should == 200 followings = JSON.parse(last_response.body) followings.size.should == 1 - followings[0]["user_id"].should == fan.id + followings[0]["id"].should == fan.id # get followers for other side of above following (fan) last_response = get_user_followers(fan, fan) last_response.status.should == 200 followers = JSON.parse(last_response.body) followers.size.should == 1 - followers[0]["user_id"].should == user.id + followers[0]["id"].should == user.id end it "should allow user to follow band" do @@ -431,14 +431,14 @@ describe "User API", :type => :api do last_response.status.should == 200 followings = JSON.parse(last_response.body) followings.size.should == 1 - followings[0]["band_id"].should == band.id + followings[0]["id"].should == band.id # get followers for band last_response = get_band_followers(user, band) last_response.status.should == 200 followers = JSON.parse(last_response.body) followers.size.should == 1 - followers[0]["user_id"].should == user.id + followers[0]["id"].should == user.id end it "should not allow user to create following for another user" do @@ -456,7 +456,7 @@ describe "User API", :type => :api do last_response.status.should == 200 followings = JSON.parse(last_response.body) followings.size.should == 1 - followings[0]["user_id"].should == fan.id + followings[0]["id"].should == fan.id # delete following last_response = delete_user_following(user, user, fan) @@ -479,7 +479,7 @@ describe "User API", :type => :api do last_response.status.should == 200 followings = JSON.parse(last_response.body) followings.size.should == 1 - followings[0]["user_id"].should == fan.id + followings[0]["id"].should == fan.id # attempt to delete following of another user last_response = delete_user_following(fan, user, fan) From 348f96f5b0b440bfc7fe9403f272f1d9ec23a84a Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 16 Feb 2014 17:39:26 -0500 Subject: [PATCH 8/8] fix bugs --- ruby/lib/jam_ruby/models/like.rb | 5 ++++ web/app/assets/javascripts/bandProfile.js | 28 +------------------- web/app/assets/javascripts/hoverFan.js | 15 +++++++++-- web/app/assets/javascripts/hoverMusician.js | 4 +-- web/app/assets/javascripts/profile.js | 2 +- web/app/views/api_bands/musician_index.rabl | 9 +++++++ web/app/views/api_bands/show.rabl | 3 +++ web/app/views/api_users/following_index.rabl | 4 +++ web/app/views/api_users/following_show.rabl | 2 +- web/app/views/api_users/liking_index.rabl | 4 +++ web/app/views/api_users/show.rabl | 2 -- web/app/views/clients/_profile.html.erb | 2 +- 12 files changed, 44 insertions(+), 36 deletions(-) diff --git a/ruby/lib/jam_ruby/models/like.rb b/ruby/lib/jam_ruby/models/like.rb index f0d104d71..cddf26ce9 100644 --- a/ruby/lib/jam_ruby/models/like.rb +++ b/ruby/lib/jam_ruby/models/like.rb @@ -4,5 +4,10 @@ module JamRuby belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id" belongs_to :likable, :polymorphic => true + def type + type = self.likable_type.gsub("JamRuby::", "").downcase + type + end + end end \ No newline at end of file diff --git a/web/app/assets/javascripts/bandProfile.js b/web/app/assets/javascripts/bandProfile.js index 4738620b3..cdb49ae8b 100644 --- a/web/app/assets/javascripts/bandProfile.js +++ b/web/app/assets/javascripts/bandProfile.js @@ -80,30 +80,6 @@ .fail(app.ajaxError); } - function isFollowingMember(userId) { - var alreadyFollowing = false; - - var url = "/api/users/" + context.JK.currentUserId + "/followings/" + userId; - $.ajax({ - type: "GET", - dataType: "json", - url: url, - async: false, - processData: false, - success: function(response) { - if (response.id !== undefined) { - alreadyFollowing = true; - } - else { - alreadyFollowing = false; - } - }, - error: app.ajaxError - }); - - return alreadyFollowing; - } - function configureBandFollowingButton(following) { $('#btn-follow-band').unbind("click"); @@ -372,9 +348,7 @@ $('#band-profile-members').append(memberHtml); // wire up Follow button click handler - var following = isFollowingMember(musician.id); - configureMemberFollowingButton(following, musician.id); - + configureMemberFollowingButton(musician.is_following, musician.id); configureRemoveMemberButton(musician.id); // TODO: wire up Friend button click handler diff --git a/web/app/assets/javascripts/hoverFan.js b/web/app/assets/javascripts/hoverFan.js index 69af6ee01..b23342180 100644 --- a/web/app/assets/javascripts/hoverFan.js +++ b/web/app/assets/javascripts/hoverFan.js @@ -25,8 +25,19 @@ followingHtml += ''; } - followingHtml += ''; - followingHtml += '' + val.name + ''; + var avatarUrl, profilePath; + + if (val.type === "band") { + avatarUrl = context.JK.resolveBandAvatarUrl(val.photo_url); + profilePath = "bandProfile"; + } + else { + avatarUrl = context.JK.resolveAvatarUrl(val.photo_url); + profilePath = "profile"; + } + + followingHtml += ''; + followingHtml += '' + val.name + ''; if (index % 2 > 0) { followingHtml += ''; diff --git a/web/app/assets/javascripts/hoverMusician.js b/web/app/assets/javascripts/hoverMusician.js index 63280f2ff..5a1e6d427 100644 --- a/web/app/assets/javascripts/hoverMusician.js +++ b/web/app/assets/javascripts/hoverMusician.js @@ -26,7 +26,7 @@ // followings var followingHtml = ''; $.each(response.followings, function(index, val) { - if (index < 4) { // display max of 4 followings (NOTE: this only displays USER followings, not BAND followings) + if (index < 4) { // display max of 4 followings if (index % 2 === 0) { followingHtml += ''; } @@ -35,7 +35,7 @@ if (val.type === "band") { avatarUrl = context.JK.resolveBandAvatarUrl(val.photo_url); - profilePath = "bandProfile" + profilePath = "bandProfile"; } else { avatarUrl = context.JK.resolveAvatarUrl(val.photo_url); diff --git a/web/app/assets/javascripts/profile.js b/web/app/assets/javascripts/profile.js index d7d4a959f..b4d011775 100644 --- a/web/app/assets/javascripts/profile.js +++ b/web/app/assets/javascripts/profile.js @@ -578,7 +578,7 @@ function addBandFollowing(evt) { evt.stopPropagation(); - var bandId = $(this).parent().parent().attr('band-id'); + var bandId = $(this).parent().parent().parent().parent().attr('band-id'); var newFollowing = {}; newFollowing.band_id = bandId; diff --git a/web/app/views/api_bands/musician_index.rabl b/web/app/views/api_bands/musician_index.rabl index 905fb9273..f47fe7f5e 100644 --- a/web/app/views/api_bands/musician_index.rabl +++ b/web/app/views/api_bands/musician_index.rabl @@ -14,3 +14,12 @@ node :biography do |musician| musician.biography.nil? ? "" : musician.biography end +if current_user + node :is_following do |musician| + current_user.following?(musician) + end + + node :is_liking do |musician| + current_user.likes?(musician) + end +end diff --git a/web/app/views/api_bands/show.rabl b/web/app/views/api_bands/show.rabl index 8bd3ca76a..74d8dd08d 100644 --- a/web/app/views/api_bands/show.rabl +++ b/web/app/views/api_bands/show.rabl @@ -25,4 +25,7 @@ if current_user node :is_following do |uu| current_user.following?(@band) end + node :is_liking do |uu| + current_user.likes?(@band) + end end \ No newline at end of file diff --git a/web/app/views/api_users/following_index.rabl b/web/app/views/api_users/following_index.rabl index 09ab85c06..58f33c335 100644 --- a/web/app/views/api_users/following_index.rabl +++ b/web/app/views/api_users/following_index.rabl @@ -20,4 +20,8 @@ end node :photo_url do |following| following.followable.photo_url +end + +node :type do |following| + following.type end \ No newline at end of file diff --git a/web/app/views/api_users/following_show.rabl b/web/app/views/api_users/following_show.rabl index fff81526b..c12929647 100644 --- a/web/app/views/api_users/following_show.rabl +++ b/web/app/views/api_users/following_show.rabl @@ -1,3 +1,3 @@ object @following -attributes :id, :user_id, :follower_id \ No newline at end of file +attributes :id, :user_id, :followable_id \ No newline at end of file diff --git a/web/app/views/api_users/liking_index.rabl b/web/app/views/api_users/liking_index.rabl index 84f047016..5f3184cb7 100644 --- a/web/app/views/api_users/liking_index.rabl +++ b/web/app/views/api_users/liking_index.rabl @@ -20,4 +20,8 @@ end node :photo_url do |liking| liking.likable.photo_url +end + +node :type do |liking| + liking.type end \ No newline at end of file diff --git a/web/app/views/api_users/show.rabl b/web/app/views/api_users/show.rabl index 1ed5202e9..9c1d18422 100644 --- a/web/app/views/api_users/show.rabl +++ b/web/app/views/api_users/show.rabl @@ -32,7 +32,6 @@ child :friends => :friends do end child :followings => :followings do - attributes :type node :id do |f| @@ -50,7 +49,6 @@ child :followings => :followings do node :photo_url do |f| f.followable.photo_url end - end child :band_musicians => :bands do diff --git a/web/app/views/clients/_profile.html.erb b/web/app/views/clients/_profile.html.erb index eb69f0ef5..76e887592 100644 --- a/web/app/views/clients/_profile.html.erb +++ b/web/app/views/clients/_profile.html.erb @@ -154,7 +154,7 @@
{musicians}
-
+