VRFS-2693 VRFS-2694 performance sample model wip

This commit is contained in:
Brian Smith 2015-02-11 22:33:10 -05:00
parent e3f27eeebc
commit 895fb92365
5 changed files with 231 additions and 18 deletions

View File

@ -12,6 +12,7 @@ module JamRuby
has_many :playing_sessions, :class_name => "JamRuby::ActiveMusicSession"
has_many :likes, :class_name => "JamRuby::RecordingLiker", :foreign_key => "claimed_recording_id", :dependent => :destroy
has_many :plays, :class_name => "JamRuby::PlayablePlay", :foreign_key => "claimed_recording_id", :dependent => :destroy
has_many :performance_samples, :class_name => "JamRuby::PerformanceSample", :foreign_key => "claimed_recording_id", :dependent => :destroy
has_one :share_token, :class_name => "JamRuby::ShareToken", :inverse_of => :shareable, :foreign_key => 'shareable_id', :dependent => :destroy
validates :name, no_profanity: true, length: {minimum: 3, maximum: 64}, presence: true

View File

@ -1,21 +1,39 @@
module JamRuby
class PerformanceSample < ActiveRecord::Base
PERMISSION_MSG = "You do not have permission to perform this operation."
attr_accessible :user_id, :service_type, :claimed_recording_id, :service_id, :url
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
belongs_to :claimed_recording, :class_name => "JamRuby::ClaimedRecording", :foreign_key => "claimed_recording_id"
validates :type, presence:true, length: {maximum: 100}
validates :username, presence:true, length: {maximum: 100}
validate :user_type_recording_unique, :if => lambda { |p| p.type == "jamkazam" }
validate :user_type_service_unique, :if => lambda { |p| p.type != "jamkazam" }
validates :service_type, presence:true, length: {maximum: 100}
# JamKazam validators
validate :claimed_recording_id_present, :if => lambda { |p| p.service_type == "jamkazam" }
validate :user_type_recording_unique, :if => lambda { |p| p.service_type == "jamkazam" }
# Non-JamKazam validators
validate :service_id_present, :if => lambda { |p| p.service_type != "jamkazam" }
validate :user_type_service_unique, :if => lambda { |p| p.service_type != "jamkazam" }
def claimed_recording_id_present
raise StateError, "Claimed recording is required for JamKazam performance samples" if self.claimed_recording_id.blank?
end
def user_type_recording_unique
match = PerformanceSample.exists?(:user_id => user.id, :claimed_recording_id => self.claimed_recording_id)
match = PerformanceSample.exists?(:user_id => self.user_id, :claimed_recording_id => self.claimed_recording_id, :service_type => self.service_type)
raise ConflictError, "You already have this JamKazam recording listed as a sample" if match
end
def service_id_present
raise StateError, "Service ID is required for non-JamKazam performance samples" if self.service_id.blank?
end
def user_type_service_unique
match = PerformanceSample.exists?(:user_id => user.id, :service_id => self.service_id)
raise ConflictError, "You already have this #{self.type} sample listed (#{self.service_id}." if match
match = PerformanceSample.exists?(:user_id => self.user_id, :service_id => self.service_id, :service_type => self.service_type)
raise ConflictError, "You already have this #{self.service_type} sample listed (#{self.service_id})." if match
end
def self.index(options = {})
@ -23,9 +41,31 @@ module JamRuby
PerformanceSample.where("user_id = ?", options[:id])
end
def self.save(current_user, options = {})
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
PerformanceSample.create(:user => user, :type => options[:type], :username => options[:username])
def self.create(current_user, options = {})
auth_user(current_user, options)
raise StateError, "Missing required information" if options[:service_type].blank?
ps = PerformanceSample.new({
:user_id => current_user.id,
:service_type => options[:service_type],
:claimed_recording_id => options[:claimed_recording_id],
:service_id => options[:service_id],
:url => options[:url]
})
ps.save!
end
def self.update(current_user, options = {})
auth_user(current_user, options)
raise StateError, "Missing required information" if options[:service_type].blank? || options[:username].blank? || options[:id].blank?
ps = PerformanceSample.find(options[:id])
ps.service_type = options[:service_type]
ps.claimed_recording_id = options[:claimed_recording_id]
ps.service_id = options[:service_id]
ps.url = options[:url]
ps.save!
end
def self.destroy(current_user, options = {})
@ -33,5 +73,10 @@ module JamRuby
raise JamArgumentError, "The performance sample ID is missing." if options[:id].blank?
PerformanceSample.destroy_all("id = ?", options[:id])
end
private
def self.auth_user(current_user, options={})
raise PermissionError, PERMISSION_MSG if current_user.nil? || options[:user_id] != current_user.id
end
end
end

View File

@ -1,7 +1,7 @@
module JamRuby
class UserPresence < ActiveRecord::Base
PERMISSION_MSG = "You do not have permission to perform this operation"
PERMISSION_MSG = "You do not have permission to perform this operation."
attr_accessible :user_id, :service_type, :username
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
@ -9,6 +9,13 @@ module JamRuby
validates :service_type, presence:true, length: {maximum: 100}
validates :username, presence:true, length: {maximum: 100}
validate :username_service_type_unique
def username_service_type_unique
match = UserPresence.exists?(:username => self.username, :service_type => self.service_type)
raise ConflictError, "Username #{self.username} is already associated with a #{self.service_type} account" if match
end
def self.index(options = {})
raise JamArgumentError, "The user is not specified." if options[:id].blank?
UserPresence.where("user_id = ?", options[:id])

View File

@ -0,0 +1,141 @@
require 'spec_helper'
describe PerformanceSample do
let(:user1) { FactoryGirl.create(:user) }
let(:user2) { FactoryGirl.create(:user) }
let(:claimed_recording) { FactoryGirl.create(:claimed_recording) }
describe "index" do
before(:all) do
@user1_sample1 = PerformanceSample.new(:user_id => user1.id, :service_type => "jamkazam", :claimed_recording_id => claimed_recording.id)
@user1_sample1.save!
@user1_sample2 = PerformanceSample.new(:user_id => user1.id, :service_type => "youtube", :service_id => "12345")
@user1_sample2.save!
@user2_sample1 = PerformanceSample.new(:user_id => user2.id, :service_type => "soundcloud", :service_id => "67890")
@user2_sample1.save!
end
context "when request is valid" do
it "should return all records for user" do
sample = PerformanceSample.index({:id => user1.id})
sample.count.should == 2
sample = PerformanceSample.index({:id => user2.id})
sample.count.should == 1
end
end
context "when request is invalid" do
it "should raise error when options are missing" do
lambda{PerformanceSample.index}.should raise_error(JamArgumentError)
end
it "should raise error when user id is missing" do
lambda{PerformanceSample.index({:id => ""})}.should raise_error(JamArgumentError)
end
end
end
describe "create" do
context "when request is valid" do
it "should save successfully" do
PerformanceSample.create(user1, {:user_id => user1.id, :service_type => "youtube", :service_id => "12345"})
# make sure we can save a second PerformanceSample for same user and type
PerformanceSample.create(user1, {:user_id => user1.id, :service_type => "youtube", :service_id => "67890"})
end
end
context "when request is not valid" do
it "should raise PermissionError if requester id does not match id in request" do
lambda{PerformanceSample.create(user1, {:user_id => user2.id, :service_type => "soundcloud", :username => "soundclouduser2"})}.should raise_error(PermissionError)
end
it "should raise error if service type is missing" do
lambda{PerformanceSample.create(user1, {:user_id => user1.id, :username => "soundclouduser2"})}.should raise_error(StateError)
end
end
end
# describe "update" do
# before(:all) do
# @user_sample = PerformanceSample.new(:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1")
# @user_sample.save!
# end
# context "when request is valid" do
# it "should save successfully" do
# up_list = PerformanceSample.index({:id => user1.id})
# up_list.count.should == 1
# up_list.first.service_type.should == "soundcloud"
# up_list.first.username.should == "soundclouduser1"
# PerformanceSample.update(user1, {:id => @user_sample.id, :user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser2"})
# up_list = PerformanceSample.index({:id => user1.id})
# up_list.count.should == 1
# up_list.first.service_type.should == "soundcloud"
# up_list.first.username.should == "soundclouduser2"
# end
# end
# context "when request is not valid" do
# it "should raise PermissionError if requester id does not match id in request" do
# lambda{PerformanceSample.update(user1, {:user_id => user2.id, :id => @user_sample.id, :service_type => "soundcloud", :username => "soundclouduser2"})}.should raise_error(PermissionError)
# end
# it "should raise error if type is missing" do
# lambda{PerformanceSample.update(user1, {:user_id => user1.id, :id => @user_sample.id, :username => "soundclouduser2"})}.should raise_error(StateError)
# end
# it "should raise error if username is missing" do
# lambda{PerformanceSample.update(user1, {:user_id => user1.id, :id => @user_sample.id, :service_type => "soundcloud"})}.should raise_error(StateError)
# end
# it "should raise error if user sample id is missing" do
# lambda{PerformanceSample.update(user1, {:user_id => user1.id, :username => "soundclouduser2", :service_type => "soundcloud"})}.should raise_error(StateError)
# end
# end
# end
# describe "destroy" do
# before(:all) do
# @user_sample = PerformanceSample.new(:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1")
# @user_sample.save!
# end
# context "when request is valid" do
# it "should destroy successfully" do
# up_list = PerformanceSample.index({:id => user1.id})
# up_list.count.should == 1
# up_list.first.service_type.should == "soundcloud"
# up_list.first.username.should == "soundclouduser1"
# PerformanceSample.delete(user1, {:user_id => user1.id, :id => @user_sample.id})
# up_list = PerformanceSample.index({:id => user1.id})
# up_list.count.should == 0
# end
# end
# context "when request is not valid" do
# it "should raise PermissionError if requester id does not match id in request" do
# lambda{PerformanceSample.delete(user2, {:user_id => user1.id, :id => @user_sample.id})}.should raise_error(PermissionError)
# end
# it "should raise error if user sample id is missing" do
# lambda{PerformanceSample.delete(user1, {:user_id => user1.id})}.should raise_error(StateError)
# end
# end
# end
end

View File

@ -8,14 +8,16 @@ describe UserPresence do
describe "index" do
before(:all) do
@user1_presence1 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :service_type => "facebook")
@user1_presence1.save!
UserPresence.delete_all
@user1_presence2 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :service_type => "twitter")
@user1_presence2.save!
user1_presence1 = UserPresence.new({:user_id => user1.id, :username => "myonlineusername", :service_type => "facebook"})
user1_presence1.save!
@user2_presence1 = UserPresence.new(:user_id => user2.id, :username => "myonlineusername", :service_type => "soundcloud")
@user2_presence1.save!
user1_presence2 = UserPresence.new({:user_id => user1.id, :username => "myonlineusername", :service_type => "twitter"})
user1_presence2.save!
user2_presence1 = UserPresence.new({:user_id => user2.id, :username => "myonlineusername", :service_type => "soundcloud"})
user2_presence1.save!
end
context "when request is valid" do
@ -40,12 +42,19 @@ describe UserPresence do
end
describe "create" do
before(:all) do
UserPresence.delete_all
end
context "when request is valid" do
it "should save successfully" do
UserPresence.create(user1, {:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1"})
# make sure we can save a second UserPresence for same user and type
UserPresence.create(user1, {:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser2"})
UserPresence.index({:id => user1.id}).count.should == 2
end
end
@ -54,19 +63,28 @@ describe UserPresence do
lambda{UserPresence.create(user1, {:user_id => user2.id, :service_type => "soundcloud", :username => "soundclouduser2"})}.should raise_error(PermissionError)
end
it "should raise error if type is missing" do
it "should raise error if service type is missing" do
lambda{UserPresence.create(user1, {:user_id => user1.id, :username => "soundclouduser2"})}.should raise_error(StateError)
end
it "should raise error if username is missing" do
lambda{UserPresence.create(user1, {:user_id => user1.id, :service_type => "soundcloud"})}.should raise_error(StateError)
end
it "should not allow duplicates of the same username / service type combination" do
UserPresence.create(user1, {:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1"})
UserPresence.index({:id => user1.id}).count.should == 1
lambda{UserPresence.create(user1, {:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1"})}.should raise_error(ConflictError)
UserPresence.index({:id => user1.id}).count.should == 1
end
end
end
describe "update" do
before(:all) do
UserPresence.delete_all
@user_presence = UserPresence.new(:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1")
@user_presence.save!
end
@ -110,6 +128,7 @@ describe UserPresence do
describe "destroy" do
before(:all) do
UserPresence.delete_all
@user_presence = UserPresence.new(:user_id => user1.id, :service_type => "soundcloud", :username => "soundclouduser1")
@user_presence.save!
end