diff --git a/app/decorators/student_decorator.rb b/app/decorators/student_decorator.rb index 6534e17..5a3d1e7 100644 --- a/app/decorators/student_decorator.rb +++ b/app/decorators/student_decorator.rb @@ -6,7 +6,7 @@ def name end def tasks - course_membership.course.tasks.map {|t| TaskDecorator.new(course_membership, t)} + course_membership.student_tasks.map {|st| StudentTaskDecorator.new(st)} end def study_plan @@ -16,4 +16,10 @@ def study_plan def to_param course_membership.person.to_param end + + private + + def course + course_membership.course + end end diff --git a/app/decorators/student_task_decorator.rb b/app/decorators/student_task_decorator.rb new file mode 100644 index 0000000..87e5cbe --- /dev/null +++ b/app/decorators/student_task_decorator.rb @@ -0,0 +1,15 @@ +class StudentTaskDecorator < ApplicationDecorator + decorates :student_task + + def status + status = student_task.status + status == StudentTask::NOT_COMPLETE ? "Not complete" : status + end + + def complete_task_link(current_person) + if(current_person.has_role?(:instructor, student_task.course)) + h.link_to "Mark as complete", "/" + end + end + +end diff --git a/app/decorators/task_decorator.rb b/app/decorators/task_decorator.rb deleted file mode 100644 index 0a32fd4..0000000 --- a/app/decorators/task_decorator.rb +++ /dev/null @@ -1,41 +0,0 @@ -class TaskDecorator < ApplicationDecorator - - def initialize(participant, task) - @participant = participant - @task = task - end - - def id - task.id - end - - def description - task.description - end - - def status - status = participant.status_for(task) - status == CompletedTask::NOT_COMPLETE ? "Not complete" : status - end - - def complete_task_link(current_person) - if(current_person.has_role?(:instructor, course)) - h.link_to "Mark as complete", "/" - end - end - - private - - def task - @task - end - - def participant - @participant - end - - def course - participant.course - end - -end diff --git a/app/models/completed_task.rb b/app/models/completed_task.rb index 78a8e20..dceb34e 100644 --- a/app/models/completed_task.rb +++ b/app/models/completed_task.rb @@ -1,6 +1,4 @@ class CompletedTask < ActiveRecord::Base - NOT_COMPLETE = -1 - belongs_to :course_membership belongs_to :task diff --git a/app/models/course_membership.rb b/app/models/course_membership.rb index aea40a1..fc02892 100644 --- a/app/models/course_membership.rb +++ b/app/models/course_membership.rb @@ -20,24 +20,12 @@ def has_role?(has_role) has_role.to_s.capitalize == role.capitalize end - def status_for(task) - completed = get_completed(task) - completed ? completed.description : CompletedTask::NOT_COMPLETE - end - - def complete_task(task, description) - #TODO: is there a better way to do an upsert? - cg - completed = get_completed(task) || completed_tasks.build(task_id: task.id) - completed.description = description - completed.save + def student_tasks + StudentTask.build_for(self) end private - def get_completed(task) - completed_tasks.where(task_id: task.id).first - end - def person_permissions unless person && person.can_access_liskov? errors.add(:person_github_nickname, "needs Clubhouse ID and access to Liskov") diff --git a/app/models/student_task.rb b/app/models/student_task.rb new file mode 100644 index 0000000..8115a19 --- /dev/null +++ b/app/models/student_task.rb @@ -0,0 +1,47 @@ +class StudentTask + NOT_COMPLETE = -1 + + def self.build_for(course_membership) + course_membership.course.tasks.map {|t| new(course_membership, t) } + end + + def initialize(course_membership, task) + @course_membership = course_membership + @task = task + end + + def course + @course_membership.course + end + + def task_id + @task.id + end + + def description + @task.description + end + + def status + completed = get_completed_task + completed ? completed.description : NOT_COMPLETE + end + + def complete_task(description) + #TODO: is there a better way to do an upsert? - cg + completed = get_completed_task || completed_tasks.build(task_id: task_id) + completed.description = description + completed.save + end + + private + + def completed_tasks + @course_membership.completed_tasks + end + + def get_completed_task + completed_tasks.where(task_id: task_id).first + end + +end diff --git a/app/views/students/show.html.haml b/app/views/students/show.html.haml index 7b8a039..8529d04 100644 --- a/app/views/students/show.html.haml +++ b/app/views/students/show.html.haml @@ -8,7 +8,7 @@ %table#tasks - @student.tasks.each do |t| - %tr{:data => {:taskid => t.id}} + %tr{:data => {:taskid => t.task_id}} %td.task= t.description %td.status= t.status %td.complete=t.complete_task_link(current_person) diff --git a/test/unit/completed_task_test.rb b/test/unit/completed_task_test.rb deleted file mode 100644 index ae3fbe8..0000000 --- a/test/unit/completed_task_test.rb +++ /dev/null @@ -1,43 +0,0 @@ -require "test_helper" -require "minitest/spec" - -describe CompletedTask do - - before do - @course = FactoryGirl.create(:webdev) - @student = FactoryGirl.create(:student, course: @course) - @task = @course.tasks.first - @participant = @course.membership_for(@student) - end - - describe "when the task is not completed" do - it "should have an incomplete status" do - @participant.status_for(@task).must_equal CompletedTask::NOT_COMPLETE - end - end - - describe "when the task is completed" do - before do - @participant.complete_task(@task, "Puzzlenode") - end - - it "should have a completed status" do - @participant.status_for(@task).must_equal "Puzzlenode" - end - - describe "when it is updated after completing" do - before do - @participant.complete_task(@task, "Community") - end - - it "should not create another completed task" do - @participant.completed_tasks.count.must_equal 1 - end - - it "should have the updated status" do - @participant.status_for(@task).must_equal "Community" - end - end - - end -end diff --git a/test/unit/student_task_test.rb b/test/unit/student_task_test.rb new file mode 100644 index 0000000..1df2e80 --- /dev/null +++ b/test/unit/student_task_test.rb @@ -0,0 +1,44 @@ +require "test_helper" +require "minitest/spec" + +describe StudentTask do + + before do + course = FactoryGirl.create(:webdev) + student = FactoryGirl.create(:student, course: course) + task = course.tasks.first + @course_membership = course.membership_for(student) + @student_task = StudentTask.new(@course_membership, task) + end + + describe "when the task is not completed" do + it "should have an incomplete status" do + @student_task.status.must_equal StudentTask::NOT_COMPLETE + end + end + + describe "when the task is completed" do + before do + @student_task.complete_task("Puzzlenode") + end + + it "should have a completed status" do + @student_task.status.must_equal "Puzzlenode" + end + + describe "when it is updated after completing" do + before do + @student_task.complete_task("Community") + end + + it "should not create another completed task" do + @course_membership.completed_tasks.count.must_equal 1 + end + + it "should have the updated status" do + @student_task.status.must_equal "Community" + end + end + + end +end