diff --git a/ruby/lib/jam_ruby/models/teacher.rb b/ruby/lib/jam_ruby/models/teacher.rb index 0f6447411..bff7826a5 100644 --- a/ruby/lib/jam_ruby/models/teacher.rb +++ b/ruby/lib/jam_ruby/models/teacher.rb @@ -12,6 +12,7 @@ module JamRuby validates :user, :presence => true validates :biography, length: {minimum: 5, maximum: 4096}, :if => :validate_introduction + validates :introductory_video, :format=> {:with=> /^(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})/, message: "is not a valid youtube URL"}, :allow_blank => true, :if => :validate_introduction validates :years_teaching, :presence => true, :if => :validate_introduction validates :years_playing, :presence => true, :if => :validate_introduction @@ -23,6 +24,7 @@ module JamRuby validate :offer_pricing, :if => :validate_pricing validate :offer_duration, :if => :validate_pricing + class << self def save_teacher(user, params) teacher = build_teacher(user, params) @@ -31,15 +33,14 @@ module JamRuby end def build_teacher(user, params) - id = params[:id] - # ensure person creating this Teacher is a Musician unless user && user.musician? raise JamPermissionError, "must be a musician" end - teacher = (id.blank?) ? Teacher.new : Teacher.find(id) - teacher.user = user + teacher = user.teacher + teacher ||= user.build_teacher() + teacher.website = params[:website] if params.key?(:website) teacher.biography = params[:biography] if params.key?(:biography) teacher.introductory_video = params[:introductory_video] if params.key?(:introductory_video) diff --git a/ruby/spec/jam_ruby/models/teacher_spec.rb b/ruby/spec/jam_ruby/models/teacher_spec.rb index 8bd27800e..84d4460ff 100644 --- a/ruby/spec/jam_ruby/models/teacher_spec.rb +++ b/ruby/spec/jam_ruby/models/teacher_spec.rb @@ -2,33 +2,32 @@ require 'spec_helper' describe Teacher do - let(:user) { FactoryGirl.create(:user) } - let(:user2) { FactoryGirl.create(:user) } - let(:fan) { FactoryGirl.create(:fan) } - let(:genre1) { FactoryGirl.create(:genre) } - let(:genre2) { FactoryGirl.create(:genre) } - let(:subject1) { FactoryGirl.create(:subject) } - let(:subject2) { FactoryGirl.create(:subject) } - let(:language1) { FactoryGirl.create(:language) } - let(:language2) { FactoryGirl.create(:language) } + let(:user) { FactoryGirl.create(:user) } + let(:genre1) { FactoryGirl.create(:genre) } + let(:genre2) { FactoryGirl.create(:genre) } + let(:subject1) { FactoryGirl.create(:subject) } + let(:subject2) { FactoryGirl.create(:subject) } + let(:language1) { FactoryGirl.create(:language) } + let(:language2) { FactoryGirl.create(:language) } let(:instrument1) { FactoryGirl.create(:instrument, :description => 'a great instrument')} let(:instrument2) { FactoryGirl.create(:instrument, :description => 'an ok instrument')} BIO = "Once a man learned a guitar." + GOOD_YOUTUBE_URL = "http://youtube.com/watch?v=1234567890" describe "can create" do it "a simple teacher" do teacher = Teacher.new teacher.user = user; teacher.biography = BIO - teacher.introductory_video = "youtube.com?xyz" + teacher.introductory_video = GOOD_YOUTUBE_URL teacher.save.should be_true t = Teacher.find(teacher.id) t.biography.should == BIO - t.introductory_video.should == "youtube.com?xyz" + t.introductory_video.should == GOOD_YOUTUBE_URL end - it "a simple teacher" do + it "with instruments" do teacher = user.build_teacher teacher.instruments << instrument1 teacher.instruments << instrument2 @@ -45,7 +44,7 @@ describe Teacher do teacher = Teacher.save_teacher( user, biography: BIO, - introductory_video: "youtube.com?xyz", + introductory_video: GOOD_YOUTUBE_URL, years_teaching: 21, years_playing: 12 ) @@ -54,7 +53,7 @@ describe Teacher do teacher.id.should_not be_nil t = Teacher.find(teacher.id) t.biography.should == BIO - t.introductory_video.should == "youtube.com?xyz" + t.introductory_video.should == GOOD_YOUTUBE_URL t.years_teaching.should == 21 t.years_playing.should == 12 end @@ -174,6 +173,33 @@ describe Teacher do teacher.errors.should have_key(:biography) end + it "introductory video" do + teacher = Teacher.save_teacher( + user, + biography: BIO, + introductory_video: "fubar.com/nothing", + validate_introduction: true + ) + + teacher.should_not be_nil + teacher.id.should be_nil + teacher.errors.should_not be_empty + + teacher.errors.should have_key(:introductory_video) + + teacher = Teacher.save_teacher( + user, + biography: BIO, + introductory_video: GOOD_YOUTUBE_URL, + validate_introduction: true + ) + + teacher.should_not be_nil + teacher.id.should_not be_nil + teacher.errors.should be_empty + + end + it "basics" do teacher = Teacher.save_teacher( user, @@ -181,11 +207,11 @@ describe Teacher do # subjects: [subject1, subject2], # genres: [genre1, genre2], # languages: [language1, language2], - validate_basics: true, teaches_age_lower: 10, teaches_beginner: true, teaches_intermediate: false, - teaches_advanced: true + teaches_advanced: true, + validate_basics: true ) puts "basic: #{teacher.errors.inspect}"