From 2558c03f3d9ca242b9a514ccee9d56595202e29f Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Tue, 25 Aug 2015 19:21:48 -0500 Subject: [PATCH] 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 --- ruby/lib/jam_ruby/models/teacher.rb | 34 ++++++++++++------- .../lib/jam_ruby/models/teacher_experience.rb | 4 +++ ruby/spec/factories.rb | 4 +-- ruby/spec/jam_ruby/models/teacher_spec.rb | 22 ++++++++++-- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/ruby/lib/jam_ruby/models/teacher.rb b/ruby/lib/jam_ruby/models/teacher.rb index b1ef51109..625d26f7d 100644 --- a/ruby/lib/jam_ruby/models/teacher.rb +++ b/ruby/lib/jam_ruby/models/teacher.rb @@ -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] diff --git a/ruby/lib/jam_ruby/models/teacher_experience.rb b/ruby/lib/jam_ruby/models/teacher_experience.rb index 181a62c08..e544b9ea2 100644 --- a/ruby/lib/jam_ruby/models/teacher_experience.rb +++ b/ruby/lib/jam_ruby/models/teacher_experience.rb @@ -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 diff --git a/ruby/spec/factories.rb b/ruby/spec/factories.rb index 9888bc8aa..a82c83a4a 100644 --- a/ruby/spec/factories.rb +++ b/ruby/spec/factories.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/teacher_spec.rb b/ruby/spec/jam_ruby/models/teacher_spec.rb index 588031659..9164829b2 100644 --- a/ruby/spec/jam_ruby/models/teacher_spec.rb +++ b/ruby/spec/jam_ruby/models/teacher_spec.rb @@ -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