Skip to content

Commit

Permalink
Fix occupied to be correct when user.student_access_allowed? is false
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Feb 24, 2016
1 parent b136d77 commit 03f26e0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/api/events_endpoints.rb
Expand Up @@ -25,7 +25,7 @@ def represent(dataset)
Interactors::Api::RepresentEventsJson.perform(
events: filtered.events,
params: params,
student_output_permitted: current_user.student_access_allowed?
include_student_ids: current_user.student_access_allowed?
).to_hash
when :ical
FormatEventsIcal.perform(events: filtered.events)
Expand Down
22 changes: 7 additions & 15 deletions app/interactors/api/represent_events_json.rb
Expand Up @@ -10,32 +10,24 @@ class RepresentEventsJson

attr_reader :meta, :events, :compounds, :representer

def perform(events:, params:, student_output_permitted: true)
def perform(events:, params:, include_student_ids: true)
paginated = Paginate.perform(dataset: events, params: params)
@meta = paginated.meta
@events = paginated.dataset

@include_student_ids = include_student_ids
@compounds = CompoundEvents.perform(events: @events, params: params).compounds
converted_events = convert_events(@events.eager(:parallel).all, student_output_permitted)
@representer = EventsRepresenter.for_collection.prepare(converted_events)
@representer = EventsRepresenter.for_collection.prepare(@events.eager(:parallel).all)
end

def to_hash(options = {})
representer.to_hash(
representer.to_hash({
include_student_ids: @include_student_ids,
courses: compounds[:courses].to_a,
teachers: compounds[:teachers].to_a,
schedule_exceptions: compounds[:schedule_exceptions].to_a,
'meta' => meta)
end

def convert_events(dataset, student_output_permitted)
if student_output_permitted
dataset
else
dataset.map do |evt|
evt.tap {|e| e.student_ids = [] }
end
end
'meta' => meta
})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/representers/events_representer.rb
Expand Up @@ -25,7 +25,7 @@ class EventsRepresenter < Roar::Decorator
property :course_id, as: :course
property :room_id, as: :room
collection :teacher_ids, as: :teachers
collection :student_ids, as: :students
collection :student_ids, as: :students, if: -> (args) { args[:include_student_ids] }
property :applied_schedule_exception_ids, as: :applied_exceptions
end

Expand Down
2 changes: 1 addition & 1 deletion spec/api/events_endpoints_spec.rb
Expand Up @@ -155,7 +155,7 @@
links: {
room: event.room.to_s,
course: event.course_id,
students: event.student_ids,
#students: event.student_ids, excluded for the current_user in the test context
teachers: event.teacher_ids
}
}.to_json
Expand Down
24 changes: 23 additions & 1 deletion spec/interactors/api/represent_events_json_spec.rb
Expand Up @@ -2,6 +2,7 @@
require 'api/represent_events_json'

describe Interactors::Api::RepresentEventsJson do

let(:include_param) { 'teachers,courses' }
let(:params) do
{
Expand All @@ -11,7 +12,11 @@
}
end

subject(:interactor) { described_class.perform(events: dataset, params: params) }
let(:include_student_ids) { true }

subject(:interactor) do
described_class.perform(events: dataset, params: params, include_student_ids: include_student_ids)
end

describe '#to_hash' do
let(:events_cnt) { 5 }
Expand Down Expand Up @@ -62,6 +67,23 @@
end
end

describe 'event' do
subject(:event) { result['events'].first }

describe 'links.students' do
subject { event['links']['students'] }

context 'when include_student_ids: false' do
let(:include_student_ids) { false }
it { should be_nil }
end

context 'when include_student_ids: true' do
let(:include_student_ids) { true }
it { should_not be_empty }
end
end
end
end

end

0 comments on commit 03f26e0

Please sign in to comment.