VRFS-3359 : Teacher API detail method
* Now includes sub-records: genres, instruments, languages, subjects * Test to verify
This commit is contained in:
parent
86a196e7c8
commit
c1927dd20a
|
|
@ -3,10 +3,11 @@ module JamRuby
|
|||
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"
|
||||
has_and_belongs_to_many :languages, :class_name => "JamRuby::Language", :join_table => "teachers_languages"
|
||||
|
||||
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"
|
||||
belongs_to :user, :class_name => 'JamRuby::User'
|
||||
|
||||
|
|
@ -24,6 +25,7 @@ module JamRuby
|
|||
validate :offer_pricing, :if => :validate_pricing
|
||||
validate :offer_duration, :if => :validate_pricing
|
||||
|
||||
default_scope { includes(:genres) }
|
||||
|
||||
class << self
|
||||
def save_teacher(user, params)
|
||||
|
|
|
|||
|
|
@ -12,8 +12,16 @@ class ApiTeachersController < ApiController
|
|||
|
||||
def detail
|
||||
teacher_id=(params[:teacher_id].present?) ? params[:teacher_id] : (current_user.teacher && current_user.teacher.id)
|
||||
@teacher = Teacher.find(teacher_id)
|
||||
respond_with_model(@teacher)
|
||||
@teacher = Teacher.where("id=?", teacher_id)
|
||||
.includes([:genres])
|
||||
.includes([:instruments])
|
||||
.includes([:languages])
|
||||
.includes([:subjects])
|
||||
.first
|
||||
|
||||
puts "DETAIL GENRES: #{@teacher.genres}"
|
||||
puts "TEACHER: #{@teacher.inspect}"
|
||||
respond_with_model(@teacher, {genres: @teacher.genres})
|
||||
end
|
||||
|
||||
def delete
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
object @teacher
|
||||
|
||||
attributes :id,
|
||||
:biography,
|
||||
:created_at,
|
||||
:id,
|
||||
:introductory_video,
|
||||
:lesson_duration_120,
|
||||
:lesson_duration_30,
|
||||
:lesson_duration_45,
|
||||
:lesson_duration_60,
|
||||
:lesson_duration_90,
|
||||
:price_duration_120_cents,
|
||||
:price_duration_30_cents,
|
||||
:price_duration_45_cents,
|
||||
:price_duration_60_cents,
|
||||
:price_duration_90_cents,
|
||||
:price_per_lesson_cents,
|
||||
:price_per_month_cents,
|
||||
:prices_per_lesson,
|
||||
:prices_per_month,
|
||||
:teaches_advanced,
|
||||
:teaches_age_lower,
|
||||
:teaches_age_upper,
|
||||
:teaches_beginner,
|
||||
:teaches_intermediate,
|
||||
:updated_at,
|
||||
:user_id,
|
||||
:website,
|
||||
:years_playing,
|
||||
:years_teaching
|
||||
|
||||
|
||||
child :instruments => :instruments do
|
||||
attributes :id, :description
|
||||
end
|
||||
|
||||
child :subjects => :subjects do
|
||||
attributes :id, :description
|
||||
end
|
||||
|
||||
child :genres => :genres do
|
||||
attributes :id, :description
|
||||
end
|
||||
|
||||
child :languages => :languages do
|
||||
attributes :id, :description
|
||||
end
|
||||
|
||||
|
|
@ -5,12 +5,12 @@ describe ApiTeachersController do
|
|||
BIO = "Once a man learned a guitar."
|
||||
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:genre1) { FactoryGirl.create(:genre) }
|
||||
let(:genre2) { FactoryGirl.create(:genre) }
|
||||
let(:genre1) { FactoryGirl.create(:genre, :description => "g1") }
|
||||
let(:genre2) { FactoryGirl.create(:genre, :description => "g2") }
|
||||
let(:subject1) { FactoryGirl.create(:subject) }
|
||||
let(:subject2) { FactoryGirl.create(:subject) }
|
||||
let(:language1) { FactoryGirl.create(:language) }
|
||||
let(:language2) { FactoryGirl.create(:language) }
|
||||
let(:language1) { FactoryGirl.create(:language, :description => "l1") }
|
||||
let(:language2) { FactoryGirl.create(:language, :description => "l2") }
|
||||
let(:instrument1) { FactoryGirl.create(:instrument, :description => 'a great instrument')}
|
||||
let(:instrument2) { FactoryGirl.create(:instrument, :description => 'an ok instrument')}
|
||||
|
||||
|
|
@ -82,8 +82,13 @@ describe ApiTeachersController do
|
|||
@teacher = Teacher.save_teacher(
|
||||
user,
|
||||
years_teaching: 21,
|
||||
biography: BIO
|
||||
biography: BIO,
|
||||
genres: [genre1, genre2],
|
||||
instruments: [instrument1, instrument2],
|
||||
languages: [language1, language2],
|
||||
subjects: [subject1, subject2]
|
||||
)
|
||||
|
||||
@teacher.should_not be_nil
|
||||
@teacher.id.should_not be_nil
|
||||
end
|
||||
|
|
@ -110,6 +115,23 @@ describe ApiTeachersController do
|
|||
response.status.should eq(404)
|
||||
end
|
||||
|
||||
|
||||
it "and retrieves sub-records" do
|
||||
get :detail, {:format => 'json', teacher_id: @teacher.id}
|
||||
response.should be_success
|
||||
json = JSON.parse(response.body)
|
||||
puts "GGG: #{JSON.pretty_generate(json)}"
|
||||
|
||||
json["genres"].should have(2).items
|
||||
json["instruments"].should have(2).items
|
||||
json["subjects"].should have(2).items
|
||||
json["languages"].should have(2).items
|
||||
|
||||
json["genres"].first["description"].should eq(genre1.description)
|
||||
json["instruments"].last["description"].should eq(instrument2.description)
|
||||
json["subjects"].first["description"].should eq(subject1.description)
|
||||
json["languages"].last["description"].should eq(language2.description)
|
||||
end
|
||||
end
|
||||
|
||||
describe "to existing" do
|
||||
|
|
|
|||
Loading…
Reference in New Issue