VRFS-3359 : Teacher profile, staged validations and specs.

This commit is contained in:
Steven Miers 2015-07-28 14:00:14 -05:00
parent 98110f68bc
commit c89d537904
2 changed files with 117 additions and 4 deletions

View File

@ -2,6 +2,7 @@ module JamRuby
class Teacher < ActiveRecord::Base
include HtmlSanitize
html_sanitize strict: [:biography, :website]
attr_accessor :validate_introduction, :validate_basics, :validate_pricing
has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "teachers_genres"
has_and_belongs_to_many :instruments, :class_name => "JamRuby::Instrument", :join_table => "teachers_instruments"
has_and_belongs_to_many :subjects, :class_name => "JamRuby::Subject", :join_table => "teachers_subjects"
@ -10,6 +11,17 @@ module JamRuby
belongs_to :user, :class_name => 'JamRuby::User'
validates :user, :presence => true
validates :biography, length: {minimum: 5, maximum: 4096}, :if => :validate_introduction
validates :years_teaching, :presence => true, :if => :validate_introduction
validates :years_playing, :presence => true, :if => :validate_introduction
validates :instruments, :length => { minimum:1, message:"At least one instrument or subject is required"}, if: :validate_basics, unless: ->(teacher){teacher.subjects.length>0}
validates :subjects, :length => { minimum:1, message:"At least one instrument or subject is required"}, if: :validate_basics, unless: ->(teacher){teacher.instruments.length>0}
validates :genres, :length => { minimum:1, message:"At least one genre is required"}, if: :validate_basics
validates :languages, :length => { minimum:1, message:"At least one language is required"}, if: :validate_basics
validate :offer_pricing, :if => :validate_pricing
validate :offer_duration, :if => :validate_pricing
class << self
def save_teacher(user, params)
@ -76,8 +88,26 @@ module JamRuby
end
end
# How to validate:
teacher.validate_introduction = !!params[:validate_introduction]
teacher.validate_basics = !!params[:validate_basics]
teacher.validate_pricing = !!params[:validate_pricing]
teacher
end
end
def offer_pricing
unless prices_per_lesson.present? || prices_per_month.present?
errors.add(:offer_pricing, "Must choose to price per lesson or per month")
end
end
def offer_duration
unless lesson_duration_30.present? || lesson_duration_45.present? || lesson_duration_60.present? || lesson_duration_90.present? || lesson_duration_120.present?
errors.add(:offer_duration, "Must offer at least one duration")
end
end
end
end

View File

@ -49,8 +49,8 @@ describe Teacher do
years_teaching: 21,
years_playing: 12
)
teacher.should_not be_nil
teacher.errors.should be_empty
teacher.id.should_not be_nil
t = Teacher.find(teacher.id)
t.biography.should == BIO
@ -73,6 +73,9 @@ describe Teacher do
teaches_advanced: true
)
teacher.should_not be_nil
teacher.errors.should be_empty
t = Teacher.find(teacher.id)
# Instruments
@ -107,6 +110,8 @@ describe Teacher do
teacher = Teacher.save_teacher(user, experience: experience)
teacher.should_not be_nil
teacher.errors.should be_empty
t = Teacher.find(teacher.id)
t.should_not be_nil
@ -134,6 +139,8 @@ describe Teacher do
teacher.should_not be_nil
teacher.id.should_not be_nil
teacher.errors.should be_empty
t = Teacher.find(teacher.id)
t.prices_per_lesson.should be_true
t.prices_per_month.should be_true
@ -150,7 +157,83 @@ describe Teacher do
t.price_duration_90_cents.should == 3000
t.price_duration_120_cents.should == 3000
end
end
end
describe "validates" do
it "introduction" do
teacher = Teacher.save_teacher(
user,
years_teaching: 21,
validate_introduction: true
)
teacher.should_not be_nil
teacher.id.should be_nil
teacher.errors.should_not be_empty
teacher.errors.should have_key(:biography)
end
it "basics" do
teacher = Teacher.save_teacher(
user,
# instruments: [instrument1, instrument2],
# 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
)
puts "basic: #{teacher.errors.inspect}"
teacher.should_not be_nil
teacher.id.should be_nil
teacher.errors.should have_key(:instruments)
teacher.errors.should have_key(:subjects)
teacher.errors.should have_key(:genres)
teacher.errors.should have_key(:languages)
end
it "pricing" do
teacher = Teacher.save_teacher(
user,
prices_per_lesson: false,
prices_per_month: false,
lesson_duration_30: false,
lesson_duration_45: false,
lesson_duration_60: false,
lesson_duration_90: false,
lesson_duration_120: false,
price_per_lesson_cents: 3000,
price_per_month_cents: 3000,
#price_duration_30_cents: 3000,
price_duration_45_cents: 3000,
#price_duration_60_cents: 3000,
#price_duration_90_cents: 3000,
price_duration_120_cents: 3000,
validate_pricing:true
)
teacher.should_not be_nil
teacher.id.should be_nil
teacher.errors.should_not be_empty
teacher.errors.should have_key(:offer_pricing)
teacher.errors.should have_key(:offer_duration)
teacher = Teacher.save_teacher(
user,
prices_per_month: true,
lesson_duration_45: true,
validate_pricing:true
)
teacher.should_not be_nil
teacher.id.should_not be_nil
teacher.errors.should be_empty
end # pricing
end # validates
end # spec