VRFS-3359 : Updates to teacher models and tests

* Scope experience based on type: teaching, education, awards
* Save experience using these different types
* Add new tests to verify
This commit is contained in:
Steven Miers 2015-08-25 19:21:48 -05:00
parent 66e17c64d6
commit 2558c03f3d
4 changed files with 48 additions and 16 deletions

View File

@ -3,12 +3,15 @@ module JamRuby
include HtmlSanitize
html_sanitize strict: [:biography, :website]
attr_accessor :validate_introduction, :validate_basics, :validate_pricing
attr_accessible :teacher_experiences, :experiences_teaching, :experiences_education, :experiences_award
has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "teachers_genres", :order=>"description"
has_and_belongs_to_many :instruments, :class_name => "JamRuby::Instrument", :join_table => "teachers_instruments", :order=>"description"
has_and_belongs_to_many :subjects, :class_name => "JamRuby::Subject", :join_table => "teachers_subjects", :order=>"description"
has_and_belongs_to_many :languages, :class_name => "JamRuby::Language", :join_table => "teachers_languages", :order=>"description"
has_many :teacher_experiences, :class_name => "JamRuby::TeacherExperience"
has_many :experiences_teaching, :class_name => "JamRuby::TeacherExperience", conditions: {experience_type: 'teaching'}
has_many :experiences_education, :class_name => "JamRuby::TeacherExperience", conditions: {experience_type: 'education'}
has_many :experiences_award, :class_name => "JamRuby::TeacherExperience", conditions: {experience_type: 'award'}
belongs_to :user, :class_name => 'JamRuby::User'
validates :user, :presence => true
@ -79,17 +82,24 @@ module JamRuby
teacher.languages = params[:languages].collect{|language_id|Language.find(language_id)} if params[:languages].present?
# Experience:
if params[:experience].present?
teacher.teacher_experiences = params[:experience].collect do |exp|
TeacherExperience.new(
name: exp[:name],
experience_type: exp[:experience_type],
organization: exp[:organization],
start_year: exp[:start_year],
end_year: exp[:end_year]
)
end
end
[:teaching, :education, :award].each do |experience_type|
key = "experiences_#{experience_type}".to_sym
if params[key].present?
experiences = params[key].collect do |exp|
TeacherExperience.new(
name: exp[:name],
experience_type: experience_type,
organization: exp[:organization],
start_year: exp[:start_year],
end_year: exp[:end_year]
)
end # collect
# Dynamically call the appropriate method (just setting the
# value doesn't result in the behavior we need)
teacher.send("#{key.to_s}=", experiences)
end # if
end # do
# How to validate:
teacher.validate_introduction = !!params[:validate_introduction]

View File

@ -3,5 +3,9 @@ module JamRuby
include HtmlSanitize
html_sanitize strict: [:name, :organization]
belongs_to :teacher, :class_name => "JamRuby::Teacher"
scope :teaching, where(experience_type: 'teaching')
scope :education, where(experience_type: 'education')
scope :awards, where(experience_type: 'award')
end
end

View File

@ -224,12 +224,12 @@ FactoryGirl.define do
end
factory :language, :class => JamRuby::Language do
name { |n| "Language #{n}" }
id { |n| "Language #{n}" }
description { |n| "Language #{n}" }
end
factory :subject, :class => JamRuby::Subject do
name { |n| "Subject #{n}" }
id { |n| "Subject #{n}" }
description { |n| "Subject #{n}" }
end

View File

@ -98,7 +98,6 @@ describe Teacher do
it "experience" do
experience = [{
experience_type: "teaching",
name: "Professor",
organization: "SHSU",
start_year: 1994,
@ -106,14 +105,33 @@ describe Teacher do
}
]
teacher = Teacher.save_teacher(user, experience: experience)
teacher = Teacher.save_teacher(user, experiences_teaching: experience)
teacher.should_not be_nil
teacher.errors.should be_empty
t = Teacher.find(teacher.id)
t.should_not be_nil
t.teacher_experiences.should have(1).items
t.experiences_teaching.should have(1).items
t.experiences_education.should have(0).items
t.experiences_award.should have(0).items
# Save some awards and re-check teacher object:
teacher = Teacher.save_teacher(user, experiences_award: experience)
teacher.should_not be_nil
teacher.errors.should be_empty
t.reload
t.teacher_experiences.should have(2).items
t.experiences_teaching.should have(1).items
t.experiences_education.should have(0).items
t.experiences_award.should have(1).items
end
it "lesson pricing" do