-
-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement get casa cases endpoint + specs
- Loading branch information
Showing
6 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Api::V1::CasaCasesController < Api::V1::BaseController | ||
before_action :set_casa_case, only: [:show, :update, :destroy] | ||
|
||
# GET /api/v1/casa_cases | ||
def index | ||
@casa_cases = current_user.casa_cases.all | ||
# puts current_user.inspect | ||
render json: @casa_cases, each_serializer: Api::V1::CasaCaseSerializer, status: :ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Api::V1::CasaCaseSerializer < ActiveModel::Serializer | ||
type "casa_case" | ||
attributes :id, :case_number, :casa_org_id, :birthday, :court_report_status, :court_report_submit_date, :youth_date_in_care | ||
|
||
def youth_date_in_care | ||
object.date_in_care.strftime("%Y-%m-%d") if object.date_in_care.present? | ||
end | ||
|
||
def birthday | ||
object.birth_month_year_youth.strftime("%Y-%m-%d") if object.birth_month_year_youth.present? | ||
end | ||
|
||
def court_report_submit_date | ||
object.court_report_submitted_at.strftime("%Y-%m-%d") if object.court_report_submitted_at.present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "rails_helper" | ||
require "spec_helper" | ||
|
||
RSpec.describe Api::V1::CasaCasesController, type: :api do | ||
describe "as a volunteer" do | ||
describe "GET /api/v1/casa_cases" do | ||
let(:casa_org) { create(:casa_org) } | ||
let(:volunteer) { create(:volunteer, casa_org: casa_org) } | ||
|
||
it "should return a list of volunteer's assigned casa cases" do | ||
casa_cases = create_list(:casa_case, 5, casa_org: casa_org) | ||
create_list(:casa_case, 2, casa_org: casa_org) | ||
volunteer.casa_cases << casa_cases | ||
puts volunteer.casa_cases.inspect | ||
header("Authorization", "Token token=#{volunteer.token}, email=#{volunteer.email}") | ||
get "/api/v1/casa_cases" | ||
expect(last_response.status).to eq 200 | ||
expect(last_response.content_type).to eq("application/json; charset=utf-8") | ||
|
||
body = JSON.parse(last_response.body, symbolize_names: true) | ||
puts body.inspect | ||
expect(body.length).to eq 5 | ||
expect(body).to match_array casa_cases.map { |casa_case| Api::V1::CasaCaseSerializer.new(casa_case).as_json } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Api::V1::CasaCaseSerializer, type: :serializer do | ||
before(:each) do | ||
@casa_case = create(:casa_case) | ||
@serializer = Api::V1::CasaCaseSerializer.new(@casa_case) | ||
@serialization = ActiveModelSerializers::Adapter.create(@serializer) | ||
end | ||
|
||
subject { JSON.parse(@serialization.to_json) } | ||
|
||
it "should have matching attributes" do | ||
# match some attributes returned by serializer | ||
expect(subject["id"]).to eq(@casa_case.id) | ||
expect(subject["case_number"]).to eq(@casa_case.case_number) | ||
expect(subject["birthday"]).to eq(@casa_case.birth_month_year_youth.strftime("%Y-%m-%d")) | ||
expect(subject["casa_org_id"]).to eq(@casa_case.casa_org_id) | ||
expect(subject.length).to eq(7) | ||
end | ||
end |