Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ gem 'paranoia'
gem 'petit', github: 'code-dot-org/petit' # For URL shortening

# JSON model serializer for REST APIs.
gem 'active_model_serializers', '~> 0.10.0'
gem 'active_model_serializers', github: 'rails-api/active_model_serializers', ref: '2962f3f64e7c672bfb5a13a8f739b5db073e5473'

# AWS SDK and associated service APIs.
gem 'aws-sdk-acm'
Expand Down
18 changes: 9 additions & 9 deletions 18 Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ GIT
specs:
full-name-splitter (0.1.2)

GIT
remote: https://github.com/rails-api/active_model_serializers.git
revision: 2962f3f64e7c672bfb5a13a8f739b5db073e5473
ref: 2962f3f64e7c672bfb5a13a8f739b5db073e5473
specs:
active_model_serializers (0.10.0.pre)
activemodel (>= 4.0)

GIT
remote: https://github.com/wjordan/gctools.git
revision: 729c6bbb32c4bcc2ebfed16543991e4d126f09cc
Expand Down Expand Up @@ -227,11 +235,6 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
active_model_serializers (0.10.10)
actionpack (>= 4.1, < 6.1)
activemodel (>= 4.1, < 6.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
active_record_query_trace (1.5.3)
active_record_union (1.3.0)
activerecord (>= 4.0)
Expand Down Expand Up @@ -340,8 +343,6 @@ GEM
brakeman (4.5.0)
builder (3.2.3)
cancancan (1.15.0)
case_transform (0.2)
activesupport
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
chronic (0.10.2)
Expand Down Expand Up @@ -526,7 +527,6 @@ GEM
bindata
json-schema (2.8.1)
addressable (>= 2.4)
jsonapi-renderer (0.2.2)
jsonapi-serializers (1.0.0)
activesupport
jumphash (0.1.0)
Expand Down Expand Up @@ -905,7 +905,7 @@ PLATFORMS
DEPENDENCIES
StreetAddress
acmesmith (~> 2.3.1)
active_model_serializers (~> 0.10.0)
active_model_serializers!
active_record_query_trace
active_record_union
activerecord-import
Expand Down
28 changes: 22 additions & 6 deletions 28 dashboard/app/controllers/api/v1/pd/applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ def quick_view

respond_to do |format|
format.json do
prefetched_applications = prefetch(applications, role: role)
render json: prefetched_applications, each_serializer: ApplicationQuickViewSerializer
serialized_applications = prefetch_and_serialize(
applications,
role: role,
serializer: ApplicationQuickViewSerializer
)
render json: serialized_applications
end
format.csv do
csv_text = get_csv_text applications, role
Expand Down Expand Up @@ -93,8 +97,13 @@ def cohort_view

respond_to do |format|
format.json do
prefetched_applications = prefetch(applications, role: role)
render json: prefetched_applications, each_serializer: serializer, scope: {user: current_user}
serialized_applications = prefetch_and_serialize(
applications,
role: role,
serializer: serializer,
scope: {user: current_user}
)
render json: serialized_applications
end
format.csv do
csv_text = get_csv_text applications, role
Expand Down Expand Up @@ -200,7 +209,8 @@ def search
user: user
)

render json: filtered_applications, each_serializer: ApplicationSearchSerializer
serialized_applications = filtered_applications.map {|a| ApplicationSearchSerializer.new(a).attributes}
render json: serialized_applications
end

private
Expand Down Expand Up @@ -294,10 +304,16 @@ def get_optional_columns(_regional_partner_value)
{registered_workshop: false}
end

def prefetch_and_serialize(applications, role: nil, serializer:, scope: {})
prefetch applications, role: role
applications.map do |application|
serializer.new(application, scope: scope).attributes
end
end

def prefetch(applications, role: nil)
type = TYPES_BY_ROLE[role.try(&:to_sym)]
type.prefetch_associated_models applications
applications
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ def find

# Find the matching partner, even if it has no workshops
partner = @partners.find_by_region(zip_code, state) || RegionalPartner.find_by_region(zip_code, state)
# To preserve existing behavior after upgrading to ActiveModelSerializers 10.x,
# initialize partner to an object with nil values if not found.
partner ||= RegionalPartner.new

render json: partner,
serializer: Api::V1::Pd::RegionalPartnerWorkshopsSerializer,
scope: {course: @course, subject: @subject}
render json: serialize_partner_workshops(partner)
end

# GET /api/v1/pd/regional_partner_workshops
def index
render json: @partners,
each_serializer: Api::V1::Pd::RegionalPartnerWorkshopsSerializer,
scope: {course: @course, subject: @subject}
render json: @partners.map {|p| serialize_partner_workshops(p)}
end

private

def serialize_partner_workshops(partner)
# The scope is not being passed to the serializer with `render json: serializer:` syntax,
# so initialize this explicitly.
# TODO (Andrew): Look into updating our very outdated version of ActiveModelSerializers
Api::V1::Pd::RegionalPartnerWorkshopsSerializer.new(
partner,
scope: {course: @course, subject: @subject}
).attributes
end

def get_filtered_workshops
@partners = RegionalPartner.includes(:pd_workshops)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def authorize_update_scholarship_info!

# GET /api/v1/pd/workshops/1/enrollments
def index
response = render_to_json @workshop.enrollments, each_serializer: Api::V1::Pd::WorkshopEnrollmentSerializer

respond_to do |format|
format.json do
render json: @workshop.enrollments, each_serializer: Api::V1::Pd::WorkshopEnrollmentSerializer
render json: response
end
format.csv do
response = render_to_json @workshop.enrollments, each_serializer: Api::V1::Pd::WorkshopEnrollmentSerializer
send_as_csv_attachment response, 'workshop_enrollments.csv'
end
end
Expand Down Expand Up @@ -80,7 +81,8 @@ def create
# POST /api/v1/pd/enrollment/:enrollment_id/scholarship_info
def update_scholarship_info
@enrollment.update_scholarship_status(params[:scholarship_status])
render json: @enrollment, serializer: Api::V1::Pd::WorkshopEnrollmentSerializer
serialized_enrollment = Api::V1::Pd::WorkshopEnrollmentSerializer.new(@enrollment).attributes
render json: serialized_enrollment
end

# DELETE /api/v1/pd/workshops/1/enrollments/1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def count
User.find(feedback.teacher_id).authorized_teacher?
end

render json: @all_unseen_feedbacks.count
render json: @all_unseen_feedbacks.count, each_serializer: Api::V1::TeacherFeedbackSerializer
end

# POST /teacher_feedbacks
Expand Down
2 changes: 1 addition & 1 deletion 2 dashboard/app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_donor_teacher_banner_details

# GET /api/v1/users/<user_id>/school_donor_name
def get_school_donor_name
render json: @user.school_donor_name.nil? ? 'null' : @user.school_donor_name.inspect
render json: @user.school_donor_name
end

# POST /api/v1/users/<user_id>/using_text_mode
Expand Down
2 changes: 1 addition & 1 deletion 2 dashboard/app/controllers/script_levels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def hidden_stage_ids

stage_ids = current_user ? current_user.get_hidden_stage_ids(params[:script_id]) : []

render json: stage_ids.map(&:to_s)
render json: stage_ids
end

# toggles whether or not a stage is hidden for a section
Expand Down
43 changes: 21 additions & 22 deletions 43 dashboard/app/serializers/api/v1/pd/cohort_view_serializer_base.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
class Api::V1::Pd::CohortViewSerializerBase < ActiveModel::Serializer
# Declare attributes individually so we can make :locked a conditional attribute
attribute :id
attribute :date_accepted
attribute :applicant_name
attribute :district_name
attribute :school_name
attribute :email
attribute :assigned_workshop
attribute :registered_workshop
attribute :status
attribute :notes
attribute :notes_2
attribute :notes_3
attribute :notes_4
attribute :notes_5
attribute :locked, if: :include_locked?
attributes(
:id,
:date_accepted,
:applicant_name,
:district_name,
:school_name,
:email,
:assigned_workshop,
:registered_workshop,
:status,
:notes,
:notes_2,
:notes_3,
:notes_4,
:notes_5
)

def locked
object.locked?
# Dynamically add locked where applicable
def attributes(attrs = {})
super(attrs).tap do |data|
data[:locked] = object.locked? if object.class.can_see_locked_status?(@scope[:user])
end
end

def email
Expand All @@ -33,8 +36,4 @@ def registered_workshop
object.registered_workshop? ? 'Yes' : 'No'
end
end

def include_locked?
object.class.can_see_locked_status?(scope[:user])
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Write a flat list of workshop attendance by session for the enrollment
class Api::V1::Pd::EnrollmentFlatAttendanceSerializer < ActiveModel::Serializer
attributes :first_name, :last_name, :email, :district_name, :school, :role, :grades_teaching, :cdo_scholarship, :other_scholarship
attributes :first_name, :last_name, :email, :district_name, :school, :role, :grades_teaching

def district_name
object.school_info&.school_district&.name
Expand All @@ -10,21 +10,15 @@ def school
object.school_info&.school&.name || object.school_info&.school_name || object.school
end

def cdo_scholarship
object.scholarship_status == Pd::ScholarshipInfoConstants::YES_CDO ? 'Yes' : ''
end

def other_scholarship
object.scholarship_status == Pd::ScholarshipInfoConstants::YES_OTHER ? 'Yes' : ''
end

# Add dynamic attributes for each session's date and attendance
def attributes(requested_attrs = nil, reload = false)
super(requested_attrs, reload).tap do |data|
def attributes(attrs = {})
super(attrs).tap do |data|
object.workshop.sessions.each_with_index do |session, i|
data["session_#{i + 1}_date".to_sym] = session.formatted_date
data["session_#{i + 1}_attendance".to_sym] = session.attendances.where(pd_enrollment_id: object.id).exists?
end
data[:cdo_scholarship] = object.scholarship_status == Pd::ScholarshipInfoConstants::YES_CDO ? 'Yes' : ''
data[:other_scholarship] = object.scholarship_status == Pd::ScholarshipInfoConstants::YES_OTHER ? 'Yes' : ''
end
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Api::V1::Pd
class FacilitatorApplicationCohortViewSerializer < CohortViewSerializerBase
# Declare attributes individually instead of using attributes list, to preserve attributes declared on base class

attribute :assigned_fit
attribute :registered_fit
attributes(
*superclass._attributes,
:assigned_fit,
:registered_fit
)

def assigned_fit
object.fit_workshop.try(&:date_and_location_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ class Api::V1::Pd::RegionalPartnerWorkshopsSerializer < ActiveModel::Serializer
attributes :id, :name, :group, :workshops, :has_csf

def workshops
return nil if object.id.nil?

workshops = object.pd_workshops.future
workshops = workshops.where(course: @scope[:course]) if @scope.try(:[], :course)
workshops = workshops.where(subject: @scope[:subject]) if @scope.try(:[], :subject)
workshops.map do |workshop|
{
id: workshop.id,
dates: workshop.friendly_date_range,
location: workshop.location_address
}
object.try do |partner|
workshops = partner.pd_workshops.future
workshops = workshops.where(course: @scope[:course]) if @scope.try(:[], :course)
workshops = workshops.where(subject: @scope[:subject]) if @scope.try(:[], :subject)
workshops.map do |workshop|
{
id: workshop.id,
dates: workshop.friendly_date_range,
location: workshop.location_address
}
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Api::V1::Pd
class TeacherApplicationCohortViewSerializer < CohortViewSerializerBase
# Declare attributes individually instead of using attributes list, to preserve attributes declared on base class
attribute :friendly_scholarship_status
attribute :registered_workshop_id
attributes(*superclass._attributes, :friendly_scholarship_status, :registered_workshop_id)

def registered_workshop_id
if object.workshop.try(:local_summer?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class Api::V1::TeacherFeedbackSerializer < ActiveModel::Serializer
attributes :id, :teacher_name, :student_id, :level_id, :script_level_id, :comment, :performance, :created_at

private

def teacher_name
object.teacher.name
end
Expand Down
16 changes: 15 additions & 1 deletion 16 dashboard/config/initializers/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
ActiveModel::Serializer.config.adapter = :attributes
# Patch to allow serializing String and Hash objects by default
class StringSerializer < ActiveModel::Serializer
def attributes(obj)
@object.to_s
end
end
class IntegerSerializer < StringSerializer; end
class HashSerializer < ActiveModel::Serializer
def attributes(obj)
@object.as_json
end
end
class ActiveModel::ErrorsSerializer < HashSerializer; end

ActiveModel::Serializer.config.adapter = :Json
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,6 @@ class Api::V1::Pd::WorkshopEnrollmentsControllerTest < ::ActionController::TestC
assert_equal @enrollment.email, response_json[0]['email']
end

test 'facilitators can see enrollments in their workshops in csv format' do
sign_in @facilitator
get :index, params: {workshop_id: @workshop.id, format: :csv}
assert_response :success

response_csv = CSV.parse(@response.body)
assert_equal 2, response_csv.length
header_row = response_csv[0]
enrollment_row = response_csv[1]
assert_equal @enrollment.email, enrollment_row[header_row.find_index('Email')]
end

test 'facilitators cannot see enrollments in workshops they are not facilitating' do
sign_in @facilitator
get :index, params: {workshop_id: @unrelated_workshop.id}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.