Skip to content

Commit

Permalink
Merge pull request mendicant-original#6 from wicz/student_task_decorator
Browse files Browse the repository at this point in the history
Student task decorator
  • Loading branch information
seejee committed Mar 27, 2012
2 parents 1faa27e + 8266369 commit 29885d2
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 102 deletions.
8 changes: 7 additions & 1 deletion app/decorators/student_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,4 +16,10 @@ def study_plan
def to_param
course_membership.person.to_param
end

private

def course
course_membership.course
end
end
15 changes: 15 additions & 0 deletions app/decorators/student_task_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 0 additions & 41 deletions app/decorators/task_decorator.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/models/completed_task.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class CompletedTask < ActiveRecord::Base
NOT_COMPLETE = -1

belongs_to :course_membership
belongs_to :task

Expand Down
16 changes: 2 additions & 14 deletions app/models/course_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
47 changes: 47 additions & 0 deletions app/models/student_task.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/students/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
43 changes: 0 additions & 43 deletions test/unit/completed_task_test.rb

This file was deleted.

44 changes: 44 additions & 0 deletions test/unit/student_task_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 29885d2

Please sign in to comment.