module JamRuby class Teacher < ActiveRecord::Base include HtmlSanitize html_sanitize strict: [:biography, :website] attr_accessor :validate_introduction, :validate_basics, :validate_pricing attr_accessible :genres, :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 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 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 default_scope { includes(:genres) } class << self def save_teacher(user, params) teacher = build_teacher(user, params) teacher.save teacher end def build_teacher(user, params) # ensure person creating this Teacher is a Musician unless user && user.musician? raise JamPermissionError, "must be a musician" end 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) teacher.introductory_video = params[:introductory_video] if params.key?(:introductory_video) teacher.years_teaching = params[:years_teaching] if params.key?(:years_teaching) teacher.years_playing = params[:years_playing] if params.key?(:years_playing) teacher.teaches_age_lower = params[:teaches_age_lower] if params.key?(:teaches_age_lower) teacher.teaches_age_upper = params[:teaches_age_upper] if params.key?(:teaches_age_upper) teacher.website = params[:website] if params.key?(:website) teacher.biography = params[:biography] if params.key?(:biography) teacher.teaches_beginner = params[:teaches_beginner] if params.key?(:teaches_beginner) teacher.teaches_intermediate = params[:teaches_intermediate] if params.key?(:teaches_intermediate) teacher.teaches_advanced = params[:teaches_advanced] if params.key?(:teaches_advanced) teacher.prices_per_lesson = params[:prices_per_lesson] if params.key?(:prices_per_lesson) teacher.prices_per_month = params[:prices_per_month] if params.key?(:prices_per_month) teacher.lesson_duration_30 = params[:lesson_duration_30] if params.key?(:lesson_duration_30) teacher.lesson_duration_45 = params[:lesson_duration_45] if params.key?(:lesson_duration_45) teacher.lesson_duration_60 = params[:lesson_duration_60] if params.key?(:lesson_duration_60) teacher.lesson_duration_90 = params[:lesson_duration_90] if params.key?(:lesson_duration_90) teacher.lesson_duration_120 = params[:lesson_duration_120] if params.key?(:lesson_duration_120) teacher.price_per_lesson_cents = params[:price_per_lesson_cents] if params.key?(:price_per_lesson_cents) teacher.price_per_month_cents = params[:price_per_month_cents] if params.key?(:price_per_month_cents) teacher.price_duration_30_cents = params[:price_duration_30_cents] if params.key?(:price_duration_30_cents) teacher.price_duration_45_cents = params[:price_duration_45_cents] if params.key?(:price_duration_45_cents) teacher.price_duration_60_cents = params[:price_duration_60_cents] if params.key?(:price_duration_60_cents) teacher.price_duration_90_cents = params[:price_duration_90_cents] if params.key?(:price_duration_90_cents) teacher.price_duration_120_cents = params[:price_duration_120_cents] if params.key?(:price_duration_120_cents) # Many-to-many relations: teacher.genres = params[:genres].collect{|genre_id|Genre.find(genre_id)} if params[:genres].present? teacher.instruments = params[:instruments].collect{|instrument_id|Instrument.find(instrument_id)} if params[:instruments].present? teacher.subjects = params[:subjects].collect{|subject_id|Subject.find(subject_id)} if params[:subjects].present? teacher.languages = params[:languages].collect{|language_id|Language.find(language_id)} if params[:languages].present? # Experience: [: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] 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