VRFS-2693 VRFS-2694 wip new musician profile models and specs
This commit is contained in:
parent
8d19e4510e
commit
79507f5f06
|
|
@ -1,19 +1,37 @@
|
|||
module JamRuby
|
||||
class PerformanceSample < ActiveRecord::Base
|
||||
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
|
||||
belongs_to :claimed_recording, :class_name => "JamRuby::ClaimedRecording" :foreign_key => "claimed_recording_id"
|
||||
belongs_to :claimed_recording, :class_name => "JamRuby::ClaimedRecording", :foreign_key => "claimed_recording_id"
|
||||
|
||||
validates :user_type_recording_unique :if => lambda { |p| p.type == "jamkazam" }
|
||||
validates :user_type_service_unique :if => lambda { |p| p.type != "jamkazam" }
|
||||
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" }
|
||||
|
||||
def user_type_recording_unique
|
||||
match = PerformanceSample.exists?(:user_id => user.id, :claimed_recording_id => self.claimed_recording_id)
|
||||
raise "You already have this JamKazam recording listed as a sample" if match
|
||||
raise ConflictError, "You already have this JamKazam recording listed as a sample" if match
|
||||
end
|
||||
|
||||
def user_type_service_unique
|
||||
match = PerformanceSample.exists?(:user_id => user.id, :service_id => self.service_id)
|
||||
raise "You already have this #{self.type} sample listed (#{self.service_id}." if match
|
||||
raise ConflictError, "You already have this #{self.type} sample listed (#{self.service_id}." if match
|
||||
end
|
||||
|
||||
def self.index(options = {})
|
||||
raise JamArgumentError, "The user is not specified." if options[:id].blank?
|
||||
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])
|
||||
end
|
||||
|
||||
def self.destroy(current_user, options = {})
|
||||
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
|
||||
raise JamArgumentError, "The performance sample ID is missing." if options[:id].blank?
|
||||
PerformanceSample.destroy_all("id = ?", options[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,9 +1,25 @@
|
|||
module JamRuby
|
||||
class UserPresence < ActiveRecord::Base
|
||||
|
||||
attr_accessible :user_id, :type, :username
|
||||
belongs_to :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
|
||||
|
||||
def self.save(user, params)
|
||||
UserPresence.create(:user => user, :type => params[:type], :username => username)
|
||||
validates :type, presence:true, length: {maximum: 100}
|
||||
validates :username, presence:true, length: {maximum: 100}
|
||||
|
||||
def self.index(options = {})
|
||||
raise JamArgumentError, "The user is not specified." if options[:id].blank?
|
||||
UserPresence.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
|
||||
UserPresence.create(:user => current_user, :type => options[:type], :username => options[:username])
|
||||
end
|
||||
|
||||
def self.destroy(current_user, options = {})
|
||||
raise PermissionError, "You do not have permission to perform this operation" if current_user.nil? || options[:user_id] != current_user.id
|
||||
UserPresence.destroy_all("id = ?", options[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe UserPresence do
|
||||
|
||||
let(:user1) { FactoryGirl.create(:user) }
|
||||
let(:user2) { FactoryGirl.create(:user) }
|
||||
|
||||
describe "index" do
|
||||
before(:all) do
|
||||
@user1_presence1 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :type => "facebook")
|
||||
@user1_presence1.save!
|
||||
|
||||
@user1_presence2 = UserPresence.new(:user_id => user1.id, :username => "myonlineusername", :type => "twitter")
|
||||
@user1_presence2.save!
|
||||
|
||||
@user2_presence1 = UserPresence.new(:user_id => user2.id, :username => "myonlineusername", :type => "soundcloud")
|
||||
@user2_presence1.save!
|
||||
end
|
||||
|
||||
context "when request is valid" do
|
||||
it "should return all records for user" do
|
||||
presence = UserPresence.index({:id => user1.id})
|
||||
presence.count.should == 2
|
||||
|
||||
presence = UserPresence.index({:id => user2.id})
|
||||
presence.count.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
context "when request is invalid" do
|
||||
it "should raise error when options are missing" do
|
||||
lambda{UserPresence.index}.should raise_error(JamArgumentError)
|
||||
end
|
||||
|
||||
it "should raise error when user id is missing" do
|
||||
lambda{UserPresence.index({:id => ""})}.should raise_error(JamArgumentError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "save" do
|
||||
context "when request is valid" do
|
||||
it "should save successfully" do
|
||||
end
|
||||
end
|
||||
|
||||
context "when request is not valid" do
|
||||
it "should raise PermissionError if requester id does not match id in request" do
|
||||
end
|
||||
|
||||
it "should raise error if type is missing" do
|
||||
end
|
||||
|
||||
it "should raise error if username is missing" do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "destroy" do
|
||||
context "when request is valid" do
|
||||
it "should destroy successfully" do
|
||||
end
|
||||
end
|
||||
|
||||
context "when request is not valid" do
|
||||
it "should raise PermissionError if requester id does not match id in request" do
|
||||
end
|
||||
|
||||
it "should raise error if user presence id is missing" do
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue