VRFS-81 - recording/favorite development

This commit is contained in:
Brian Smith 2012-11-21 14:48:39 -05:00
parent 773046ea42
commit d73bbb822c
16 changed files with 275 additions and 170 deletions

View File

@ -40,6 +40,8 @@ require "jam_ruby/models/user_favorite"
require "jam_ruby/models/band_follower"
require "jam_ruby/models/search"
require "jam_ruby/models/recording"
require "jam_ruby/models/musician_recording"
require "jam_ruby/models/band_recording"
include Jampb

View File

@ -3,7 +3,7 @@ module JamRuby
include Tire::Model::Search
include Tire::Model::Callbacks
attr_accessible :name, :website, :biography
attr_accessible :name, :website, :biography, :city, :state, :country
self.primary_key = 'id'
@ -15,7 +15,8 @@ module JamRuby
has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "bands_genres"
# recordings
has_and_belongs_to_many :recordings, :class_name => "JamRuby::Recording", :join_table => "bands_recordings"
has_many :band_recordings, :class_name => "JamRuby::BandRecording", :foreign_key => "band_id"
has_many :recordings, :through => :band_recordings, :class_name => "JamRuby::Recording"
# followers
has_many :followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id"
@ -36,7 +37,7 @@ module JamRuby
def follower_count
return self.followers.size
end
def location
# TODO: implement a single string version of location;
# this will be indexed into elasticsearch and returned in search
@ -44,45 +45,44 @@ module JamRuby
end
# helper method for creating / updating a Band
def self.save(params)
if params[:id].nil?
def self.save(id, name, website, biography, city, state, country, genres)
if id.nil?
band = Band.new()
else
band = Band.find(params[:id])
band = Band.find(id)
end
# name
unless params[:name].nil?
band.name = params[:name]
unless name.nil?
band.name = name
end
# website
unless params[:website].nil?
band.website = params[:website]
unless website.nil?
band.website = website
end
# biography
unless params[:biography].nil?
band.biography = params[:biography]
unless biography.nil?
band.biography = biography
end
# city
unless params[:city].nil?
band.city = params[:city]
unless city.nil?
band.city = city
end
# state
unless params[:state].nil?
band.state = params[:state]
unless state.nil?
band.state = state
end
# country
unless params[:country].nil?
band.country = params[:country]
unless country.nil?
band.country = country
end
# genres
genres = params[:genres]
unless genres.nil?
ActiveRecord::Base.transaction do
# delete all genres for this band first

View File

@ -0,0 +1,12 @@
module JamRuby
class BandRecording < ActiveRecord::Base
self.table_name = "bands_recordings"
# bands
has_many :bands, :through => :band_recordings, :class_name => "JamRuby::Band"
belongs_to :band, :class_name => "JamRuby::Band", :foreign_key => "band_id"
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
end
end

View File

@ -4,7 +4,7 @@ module JamRuby
self.primary_key = 'id'
# users
has_many :musician_instruments
has_many :musician_instruments, :class_name => "JamRuby::MusicianInstrument"
has_many :users, :through => :musician_instruments, :class_name => "JamRuby::User"
has_many :connection_tracks, :class_name => "JamRuby::ConnectionTrack", :inverse_of => :instrument

View File

@ -5,8 +5,8 @@ module JamRuby
self.primary_key = 'id'
belongs_to :user
belongs_to :instrument
belongs_to :user, :class_name => "JamRuby::User"
belongs_to :instrument, :class_name => "JamRuby::Instrument"
def description
@description = self.instrument.description

View File

@ -0,0 +1,12 @@
module JamRuby
class MusicianRecording < ActiveRecord::Base
self.table_name = "musicians_recordings"
# musicians
has_many :musicians, :through => :musician_recordings, :class_name => "JamRuby::User"
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
end
end

View File

@ -3,11 +3,46 @@ module JamRuby
self.primary_key = 'id'
# musicians
has_and_belongs_to_many :users, :class_name => "JamRuby::User", :join_table => "musicians_recordings"
has_many :musician_recordings
# bands
has_and_belongs_to_many :bands, :class_name => "JamRuby::Band", :join_table => "bands_recordings"
# favorites
has_and_belongs_to_many :user_favorites, :class_name => "JamRuby::UserFavorite", :join_table => "users_favorites"
validates :description, presence: true, length: {maximum: 200}
def self.save(id, is_public, description, owner_id, is_band)
if id.nil?
recording = Recording.new()
else
recording = Recording.find(id)
end
# public flag
unless is_public.nil?
recording.public = is_public
end
# description
unless description.nil?
recording.description = description
end
if is_band?
recording.band << Band.create(id: owner_id)
else
recording.user << User.create(id: owner_id)
end
recording.updated_at = Time.now.getutc
recording.save
return recording
end
def self.delete(id, owner_id, is_band)
if is_band?
JamRuby::UserFollower.delete_all "(user_id = '#{user_id}' AND follower_id = '#{follower_id}')"
else
end
end
end
end

View File

@ -9,9 +9,6 @@ module JamRuby
# authorizations (for facebook, etc -- omniauth)
has_many :user_authorizations, :class_name => "JamRuby::UserAuthorization"
# account
belongs_to :account, :class_name => "JamRuby::Account"
# connections (websocket-gateway)
has_many :connections, :class_name => "JamRuby::Connection"
@ -20,15 +17,16 @@ module JamRuby
has_many :friend_requests, :class_name => "JamRuby::FriendRequest"
# instruments
has_many :musician_instruments
has_many :musician_instruments, :class_name => "JamRuby::MusicianInstrument"
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
# bands
has_many :band_musicians
has_many :band_musicians, :class_name => "JamRuby::BandMusician"
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
# recordings
has_and_belongs_to_many :recordings, :class_name => "JamRuby::Recording", :join_table => "musicians_recordings"
has_many :musician_recordings, :class_name => "JamRuby::MusicianRecording", :foreign_key => "user_id"
has_many :recordings, :through => :musician_recordings, :class_name => "JamRuby::Recording"
# followers
has_many :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id"
@ -43,8 +41,8 @@ module JamRuby
has_many :inverse_band_followings, :through => :band_followings, :class_name => "JamRuby::Band", :foreign_key => "band_id"
# favorites
has_many :favorites, :class_name => "JamRuby::UserFavorite", :foreign_key => "recording_id"
has_many :inverse_favorites, :through => :favorites, :source => :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
has_many :favorites, :class_name => "JamRuby::UserFavorite", :foreign_key => "user_id"
has_many :inverse_favorites, :through => :favorites, :source => :user, :class_name => "JamRuby::User"
# friends
has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id"
@ -120,6 +118,10 @@ module JamRuby
return self.followings.size + self.band_followings.size
end
def favorite_count
return self.favorites.size
end
def to_s
return email unless email.nil?
@ -131,88 +133,84 @@ module JamRuby
end
# helper method for creating / updating a User
def self.save(params)
if params[:id].nil?
def self.save(id, first_name, last_name, email, password, password_confirmation, musician, gender,
birth_date, internet_service_provider, city, state, country, instruments)
if id.nil?
user = User.new()
else
user = User.find(params[:id])
end
# account id
unless params[:account_id].nil?
user.account_id = params[:account_id]
user = User.find(id)
end
# first name
unless params[:first_name].nil?
user.first_name = params[:first_name]
unless first_name.nil?
user.first_name = first_name
end
# last name
unless params[:last_name].nil?
user.last_name = params[:last_name]
unless last_name.nil?
user.last_name = last_name
end
# email
unless params[:email].nil?
user.email = params[:email]
unless email.nil?
user.email = email
end
# password
unless params[:password].nil?
user.password = params[:password]
unless password.nil?
user.password = password
end
# password confirmation
unless params[:password_confirmation].nil?
user.password_confirmation = params[:password_confirmation]
unless password_confirmation.nil?
user.password_confirmation = password_confirmation
end
# musician flag
unless params[:musician].nil?
user.musician = params[:musician]
unless musician.nil?
user.musician = musician
end
# gender
unless params[:gender].nil?
account.gender = params[:gender]
unless gender.nil?
account.gender = gender
end
# birthdate
unless params[:birth_date].nil?
account.birth_date = params[:birth_date]
unless birth_date.nil?
account.birth_date = birth_date
end
# ISP
unless params[:internet_service_provider].nil?
account.internet_service_provider = params[:internet_service_provider]
unless internet_service_provider.nil?
account.internet_service_provider = internet_service_provider
end
# city
unless params[:city].nil?
user.city = params[:city]
unless city.nil?
user.city = city
end
# state
unless params[:state].nil?
user.state = params[:state]
unless state.nil?
user.state = state
end
# country
unless params[:country].nil?
user.country = params[:country]
unless country.nil?
user.country = country
end
# instruments
unless params[:instruments].nil?
ActiveRecord::Base.transaction do
unless instruments.nil?
UserManager.active_record_transaction do |user_manager|
# delete all instruments for this user first
unless user.id.nil? || user.id.length == 0
MusicianInstrument.delete_all(["user_id = ?", user.id])
end
# loop through each instrument in the array and save to the db
params[:instruments].each do |instrument|
instruments.each do |instrument|
mu = MusicianInstrument.new()
mu.user_id = user.id
mu.instrument_id = instrument[:id]
@ -228,102 +226,133 @@ module JamRuby
return user
end
def self.create_user_following(user_id, follower_id)
@follower = UserFollower.new()
@follower.user_id = user_id
@follower.follower_id = follower_id
@follower.save
end
def self.delete_user_following(user_id, follower_id)
JamRuby::UserFollower.delete_all "(user_id = '#{user_id}' AND follower_id = '#{follower_id}')"
end
def self.create_band_following(band_id, follower_id)
@follower = BandFollower.new()
@follower.band_id = band_id
@follower.follower_id = follower_id
@follower.save
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
@favorite.recording_id = recording_id
@favorite.save
end
def self.delete_favorite(user_id, recording_id)
JamRuby::UserFavorite.delete_all "(user_id = '#{user_id}' AND recording_id = '#{recording_id}')"
end
def limit_to_five_instruments
if instruments.count > 5
errors.add(:instruments, "No more than 5 instruments are allowed.")
end
end
# throws ActiveRecord::RecordNotFound if instrument is invalid
# throws an email delivery error if unable to connect out to SMTP
def self.signup(first_name, last_name, email, password, password_confirmation,
city, state, country, instruments, signup_confirm_url)
user = User.new
# throws ActiveRecord::RecordNotFound if instrument is invalid
# throws an email delivery error if unable to connect out to SMTP
def self.signup(first_name, last_name, email, password, password_confirmation,
city, state, country, instruments, signup_confirm_url)
user = User.new
UserManager.active_record_transaction do |user_manager|
user.first_name = first_name
user.last_name = last_name
user.email = email
UserManager.active_record_transaction do |user_manager|
user.first_name = first_name
user.last_name = last_name
user.email = email
#FIXME: Setting random password for social network logins. This
# is because we have validations all over the place on this.
# The right thing would be to have this null
if password.nil?
user.password = "blahblahblah"
user.password_confirmation = "blahblahblah"
else
user.password = password
user.password_confirmation = password_confirmation
end
#FIXME: Setting random password for social network logins. This
# is because we have validations all over the place on this.
# The right thing would be to have this null
if password.nil?
user.password = "blahblahblah"
user.password_confirmation = "blahblahblah"
else
user.password = password
user.password_confirmation = password_confirmation
end
user.admin = false
user.email_confirmed = false
user.city = city
user.state = state
user.country = country
unless instruments.nil?
instruments.each do |musician_instrument_param|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
musician_instrument = MusicianInstrument.new
musician_instrument.user = user
musician_instrument.instrument = instrument
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
musician_instrument.priority = musician_instrument_param[:priority]
musician_instrument.save
user.musician_instruments << musician_instrument
end
end
user.signup_token = SecureRandom.urlsafe_base64
user.admin = false
user.email_confirmed = false
user.city = city
user.state = state
user.country = country
unless instruments.nil?
instruments.each do |musician_instrument_param|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
musician_instrument = MusicianInstrument.new
musician_instrument.user = user
musician_instrument.instrument = instrument
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
musician_instrument.priority = musician_instrument_param[:priority]
musician_instrument.save
user.musician_instruments << musician_instrument
user.save
if user.errors.any?
raise ActiveRecord::Rollback
else
# FIXME:
# It's not standard to require a confirmation when a user signs up with Facebook.
# We should stop asking for it.
#
# any errors here should also rollback the transaction; that's OK. If emails aren't going to be delivered,
# it's already a really bad situation; make user signup again
UserMailer.welcome_message(user, signup_confirm_url.nil? ? nil : (signup_confirm_url + "/" + user.signup_token) ).deliver
end
end
user.signup_token = SecureRandom.urlsafe_base64
user.save
if user.errors.any?
raise ActiveRecord::Rollback
else
# FIXME:
# It's not standard to require a confirmation when a user signs up with Facebook.
# We should stop asking for it.
#
# any errors here should also rollback the transaction; that's OK. If emails aren't going to be delivered,
# it's already a really bad situation; make user signup again
UserMailer.welcome_message(user, signup_confirm_url.nil? ? nil : (signup_confirm_url + "/" + user.signup_token) ).deliver
end
end
return user
end
# throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user
def self.signup_confirm(signup_token)
if signup_token.nil? || signup_token.empty?
# there are plenty of confirmed users with nil signup_tokens, so we can't look on it
raise ActiveRecord::RecordNotFound
else
UserManager.active_record_transaction do |user_manager|
# throws ActiveRecord::RecordNotFound if invalid
user = User.find_by_signup_token!(signup_token)
user.signup_token = nil
user.email_confirmed = true
user.save
return user
end
end
end
# if valid credentials are supplied for an 'active' user, returns the user
# if not authenticated, returns nil
def self.authenticate(email, password)
# we only allow users that have confirmed email to authenticate
user = User.where('email_confirmed=true').find_by_email(email)
if user && user.authenticate(password)
return user
else
return nil
end
end
# throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user
def self.signup_confirm(signup_token)
if signup_token.nil? || signup_token.empty?
# there are plenty of confirmed users with nil signup_tokens, so we can't look on it
raise ActiveRecord::RecordNotFound
else
UserManager.active_record_transaction do |user_manager|
# throws ActiveRecord::RecordNotFound if invalid
user = User.find_by_signup_token!(signup_token)
user.signup_token = nil
user.email_confirmed = true
user.save
return user
end
end
end
# if valid credentials are supplied for an 'active' user, returns the user
# if not authenticated, returns nil
def self.authenticate(email, password)
# we only allow users that have confirmed email to authenticate
user = User.where('email_confirmed=true').find_by_email(email)
if user && user.authenticate(password)
return user
else
return nil
end
end
### Elasticsearch/Tire integration ###

View File

@ -6,5 +6,6 @@ module JamRuby
self.primary_key = 'id'
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id", :inverse_of => :inverse_favorites
belongs_to :recording, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
end
end

View File

@ -1,17 +1,21 @@
FactoryGirl.define do
factory :user, :class => JamRuby::User do
sequence(:email) { |n| "person_#{n}@example.com"}
sequence(:first_name) { |n| "Person" }
sequence(:last_name) { |n| "#{n}" }
password "foobar"
password_confirmation "foobar"
email_confirmed true
city "Apex"
state "NC"
country "USA"
factory :admin do
admin true
end
end
factory :music_session, :class => "JamRuby::MusicSession" do
factory :music_session, :class => JamRuby::MusicSession do
sequence(:description) { |n| "Music Session #{n}" }
fan_chat true
fan_access true
@ -32,6 +36,10 @@ FactoryGirl.define do
end
factory :band, :class => JamRuby::Band do
sequence(:name) { |n| "Band" }
biography "My Biography"
city "Apex"
state "NC"
country "USA"
end
end

View File

@ -11,7 +11,7 @@ describe ConnectionManager do
end
def create_user(first_name, last_name, email, options = {:musician => true})
@conn.exec("INSERT INTO users (first_name, last_name, email, musician, password_digest) VALUES ($1, $2, $3, $4, $5) RETURNING id", [first_name, last_name, email, options[:musician], '1']) do |result|
@conn.exec("INSERT INTO users (first_name, last_name, email, musician, password_digest, city, state, country) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id", [first_name, last_name, email, options[:musician], '1', 'Apex', 'NC', 'USA']) do |result|
return result.getvalue(0, 0)
end
end

View File

@ -6,7 +6,7 @@ describe User do
Band.delete_search_index
Band.create_search_index
@band = Band.save(name: "Example Band", website: "www.bands.com", biography: "zomg we rock")
@band = Band.save(nil, "Example Band", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", nil)
# you have to poke elasticsearch because it will batch requests internally for a second
Band.search_index.refresh
@ -58,7 +58,7 @@ describe User do
end
it "should tokenize correctly" do
@band2 = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@band2 = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", nil)
Band.search_index.refresh
ws = Band.search("pea")
ws.results.length.should == 1
@ -68,7 +68,7 @@ describe User do
it "should not return anything with a 1 character search" do
@band2 = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@band2 = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", nil)
Band.search_index.refresh
ws = Band.search("pe")
ws.results.length.should == 1

View File

@ -11,9 +11,9 @@ describe Search do
def create_peachy_data
@user = FactoryGirl.create(:user, first_name: "Peach", last_name: "Pit", email: "user@example.com", musician: true)
@fan = FactoryGirl.create(:user, first_name: "Peach Peach", last_name: "Pit", email: "fan@example.com", musician: false)
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@user = FactoryGirl.create(:user, first_name: "Peach", last_name: "Pit", email: "user@example.com", musician: true, city: "Apex", state: "NC", country:"USA")
@fan = FactoryGirl.create(:user, first_name: "Peach Peach", last_name: "Pit", email: "fan@example.com", musician: false, city: "Apex", state: "NC", country:"USA")
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock", city: "Apex", state: "NC", country:"USA")
end
def assert_peachy_data

View File

@ -20,7 +20,8 @@ describe "tire search" do
end
it "full search for single user" do
@user = FactoryGirl.create(:user, first_name: "User", last_name: "One", email: "user@example.com", musician: true)
@user = FactoryGirl.create(:user, first_name: "User", last_name: "One", email: "user@example.com", musician: true,
city: "Apex", state: "NC", country: "USA")
User.search_index.refresh
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
@ -34,7 +35,9 @@ describe "tire search" do
end
it "full search for single band" do
@band = FactoryGirl.create(:band, name: "Example Band", website: "www.bands.com", biography: "zomg we rock")
@band = FactoryGirl.create(:band, name: "Example Band", website: "www.bands.com", biography: "zomg we rock",
city: "Apex", state: "NC", country: "USA")
Band.search_index.refresh
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
@ -48,8 +51,8 @@ describe "tire search" do
end
it "full search for a band & user" do
@user = FactoryGirl.create(:user, first_name: "Peach", last_name: "Foo", email: "user@example.com", musician: true)
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@user = FactoryGirl.create(:user, first_name: "Peach", last_name: "Foo", email: "user@example.com", musician: true, city: "Apex", state: "NC", country: "USA")
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock", city: "Apex", state: "NC", country: "USA")
User.search_index.refresh
Band.search_index.refresh
@ -70,8 +73,8 @@ describe "tire search" do
it "pagination" do
@user = FactoryGirl.create(:user, first_name: "Peach", last_name: "foo", email: "user@example.com", musician: true)
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@user = FactoryGirl.create(:user, first_name: "Peach", last_name: "foo", email: "user@example.com", musician: true, city: "Apex", state: "NC", country: "USA")
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock", city: "Apex", state: "NC", country: "USA")
User.search_index.refresh
Band.search_index.refresh

View File

@ -7,7 +7,8 @@ describe User do
User.create_search_index
@user = FactoryGirl.create(:user, first_name: "Example", last_name: "User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true)
password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true,
city: "Apex", state: "NC", country: "USA")
# you have to poke elasticsearch because it will batch requests internally for a second
User.search_index.refresh
end
@ -50,7 +51,7 @@ describe User do
User.search_index.refresh
ws = User.search("Example User")
ws.results.length.should == 0
ws.results.length.should == 1
ws = User.search("Bonus")
ws.results.length.should == 1
@ -61,7 +62,8 @@ describe User do
it "should tokenize correctly" do
@user2 = FactoryGirl.create(:user, first_name: "peaches", last_name: "test", email: "peach@example.com",
password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true)
password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true,
city: "Apex", state: "NC", country: "USA")
User.search_index.refresh
ws = User.search("pea")
ws.results.length.should == 1
@ -72,7 +74,8 @@ describe User do
it "users who have signed up, but not confirmed should not show up in search index" do
@user3 = FactoryGirl.create(:user, first_name: "unconfirmed", last_name: "unconfirmed", email: "unconfirmed@example.com",
password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: false)
password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: false,
city: "Apex", state: "NC", country: "USA")
User.search_index.refresh
ws = User.search("unconfirmed")
ws.results.length.should == 0

View File

@ -4,7 +4,7 @@ describe User do
before do
@user = User.new(first_name: "Example", last_name: "User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
password: "foobar", password_confirmation: "foobar", city: "Apex", state: "NC", country: "USA")
end
subject { @user }