Permalink
Browse files

Merge branch 'master' of https://github.com/wicz/liskov into wicz-master

  • Loading branch information...
2 parents 7a47f90 + ed62136 commit 27b05b60cc4cbf9fd00b8e4fbad37096dcba16cb @jordanbyron jordanbyron committed Mar 20, 2012
View
@@ -11,4 +11,6 @@
# Configuration Files
/config/database.yml
/config/initializers/omniauth.rb
-/config/initializers/secret_token.rb
+/config/initializers/secret_token.rb
+
+.rvmrc
@@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base
def current_person
begin
- @current_person ||= Clubhouse::Client::Person.new(session[:person_github_nickname])
+ @current_person ||= clubhouse_person(session[:person_github_nickname])
rescue Clubhouse::Client::PersonNotFound
end
@@ -31,6 +31,13 @@ def person_required
end
def login_path
- Rails.env.development? ? '/auth/developer' : '/auth/github'
+ Rails.env.production? ? '/auth/github' : '/auth/developer'
end
+
+ private
+
+ def clubhouse_person(github_nickname)
+ PersonDecorator.new(Clubhouse::Client::Person.new(github_nickname))
+ end
+
end
@@ -1,6 +1,7 @@
class CoursesController < ApplicationController
def show
- @course = CourseDecorator.find(params[:id])
- @members = @course.course_memberships
+ @course = CourseDecorator.find(params[:id])
+ @members = @course.course_memberships
+ @tasks = @course.tasks
end
end
@@ -3,7 +3,7 @@ class SessionsController < ApplicationController
def create
begin
- person = Clubhouse::Client::Person.new(auth_hash['info']['nickname'])
+ person = clubhouse_person(auth_hash['info']['nickname'])
rescue Clubhouse::Client::PersonNotFound
flash[:error] = "Sorry, but we couldn't find your record in Clubhouse"
end
@@ -0,0 +1,42 @@
+class TasksController < ApplicationController
+ before_filter :find_course, :check_permission
+
+ def new
+ @task = @course.tasks.build
+ end
+
+ def create
+ task = @course.tasks.build(params[:task])
+
+ if task.save
+ redirect_to(@course, notice: "Task created")
+ else
+ redirect_to new_course_task_path(@course)
+ end
+ end
+
+ def destroy
+ task = Task.find(params[:id])
+
+ if task.destroy
+ flash[:notice] = "Task removed successfully"
+ else
+ flash[:alert] = "Sorry, there was an error remove the task from the course"
+ end
+ redirect_to @course
+ end
+
+ private
+
+ def check_permission
+ unless current_person.has_role?(:instructor, @course)
+ redirect_to(@course, alert: "Unauthorized access")
+ end
+ end
+
+ def find_course
+ @course = Course.find(params[:course_id])
+ rescue ActiveRecord::RecordNotFound
+ redirect_to(root_url, alert: "Couldn't find course")
+ end
+end
@@ -1,37 +1,8 @@
class CourseDecorator < ApplicationDecorator
decorates :course
- # Accessing Helpers
- # You can access any helper via a proxy
- #
- # Normal Usage: helpers.number_to_currency(2)
- # Abbreviated : h.number_to_currency(2)
- #
- # Or, optionally enable "lazy helpers" by including this module:
- # include Draper::LazyHelpers
- # Then use the helpers with no proxy:
- # number_to_currency(2)
-
- # Defining an Interface
- # Control access to the wrapped subject's methods using one of the following:
- #
- # To allow only the listed methods (whitelist):
- # allows :method1, :method2
- #
- # To allow everything except the listed methods (blacklist):
- # denies :method1, :method2
-
- # Presentation Methods
- # Define your own instance methods, even overriding accessors
- # generated by ActiveRecord:
- #
- # def created_at
- # h.content_tag :span, time.strftime("%a %m/%d/%y"),
- # :class => 'timestamp'
- # end
-
def description
#TODO process with markdown
course.description
end
-end
+end
@@ -0,0 +1,10 @@
+class PersonDecorator < ApplicationDecorator
+ decorates :person, :class => Clubhouse::Client::Person
+
+ allows :name, :email, :github_nickname, :permissions
+
+ def has_role?(role, course)
+ membership = course.course_memberships.for_person(person).first
+ membership.has_role?(role) if membership
+ end
+end
View
@@ -1,8 +1,10 @@
class Course < ActiveRecord::Base
has_many :discussions
has_many :course_memberships
+ has_many :tasks
def people
@people ||= course_memberships.map {|cm| cm.person }
end
+
end
@@ -7,12 +7,18 @@ class CourseMembership < ActiveRecord::Base
validates_uniqueness_of :person_github_nickname, :scope => :course_id
validate :person_permissions
+ scope :for_person, lambda { |person| where(person_github_nickname: person.github_nickname) }
+
def person
@person ||= Clubhouse::Client::Person.new(person_github_nickname)
rescue Clubhouse::Client::PersonNotFound
return nil
end
+ def has_role?(has_role)
+ has_role.to_s.capitalize == role.capitalize
+ end
+
private
def person_permissions
View
@@ -0,0 +1,6 @@
+class Task < ActiveRecord::Base
+ belongs_to :course
+
+ validates_presence_of :course_id, :description
+ validates_uniqueness_of :description, :scope => :course_id
+end
@@ -9,4 +9,4 @@
%br
= f.select :role, CourseMembership::ROLES
%p
- = f.submit "Add to course"
+ = f.submit "Add to course"
@@ -0,0 +1,11 @@
+= render 'course_memberships/participants'
+
+%h3 Add a new participant to this course
+
+%p
+ %em
+ NOTE: In development, this requires you have fixtures in
+ %strong test/fixtures/clubhouse/people
+ for the people you are adding.
+
+= render 'course_memberships/form'
@@ -0,0 +1,5 @@
+= render 'tasks/list'
+
+- if current_person.has_role?(:instructor, @course)
+ = link_to("Add Task", new_course_task_path(@course))
+
@@ -1,13 +1,4 @@
%h1= @course.name
-= render 'course_memberships/participants'
-
-%h3 Add a new participant to this course
-
-%p
- %em
- NOTE: In development, this requires you have fixtures in
- %strong test/fixtures/clubhouse/people
- for the people you are adding.
-
-= render 'course_memberships/form'
+= render 'participants'
+= render 'tasks'
@@ -0,0 +1,7 @@
+= form_for([@course, @task]) do |f|
+ %p
+ = f.label :description, "Description"
+ %br
+ = f.text_field :description
+ %p
+ = f.submit "Create Task"
@@ -0,0 +1,8 @@
+%h2 Tasks
+
+%ul
+ - @tasks.each do |task|
+ %li
+ = "#{task.description}"
+ \-
+ = # link_to "Remove", task_path(task), :method => :delete
@@ -0,0 +1,3 @@
+%h2 Add Task
+
+= render "form"
View
@@ -5,9 +5,9 @@
match '/auth/failure' => 'sessions#failure'
match '/logout' => 'sessions#destroy', as: 'logout'
- resources :courses do
- resources :discussions
- end
+ resources :courses do
+ resources :tasks, :discussions
+ end
resources :course_memberships
-end
+end
@@ -0,0 +1,10 @@
+class CreateTasks < ActiveRecord::Migration
+ def change
+ create_table :tasks do |t|
+ t.belongs_to :course
+ t.string :description
+
+ t.timestamps
+ end
+ end
+end
View
@@ -38,4 +38,11 @@
t.boolean "archived", :default => false
end
+ create_table "tasks", :force => true do |t|
+ t.integer "course_id"
+ t.string "description"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
end
@@ -0,0 +1,7 @@
+FactoryGirl.define do
+ factory :course_membership do
+ course nil
+ person_github_nickname nil
+ role nil
+ end
+end
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :webdev, class: Course do
+ name "Web Development"
+ description "MU's Web Development Course"
+ end
+end
View
@@ -0,0 +1,33 @@
+FactoryGirl.define do
+ factory :person, class: OpenStruct do
+ name "Test"
+ email "test@gmail.com"
+ email_hash "6d2661c5a65f24c8f0bb579b6e0a0d81"
+ group "Visitor"
+ website nil
+ membership_date nil
+ github_uid "1234"
+ github_nickname "github_nickname"
+ permissions Hash[[["Clubhouse", "Administrator"],
+ ["Community", "Administrator"],
+ ["Liskov", "Administrator"]]]
+ end
+
+ factory :instructor, parent: "person" do
+ github_nickname "instructor"
+ course_membership {
+ FactoryGirl.create(:course_membership, course: course,
+ person_github_nickname: "#{github_nickname}",
+ role: "Instructor") if course
+ }
+ end
+
+ factory :student, parent: "person" do
+ github_nickname "student"
+ course_membership {
+ FactoryGirl.create(:course_membership, course: course,
+ person_github_nickname: "#{github_nickname}",
+ role: "Student") if course
+ }
+ end
+end
@@ -0,0 +1,10 @@
+{ "name":"Instructor",
+ "email":"instructor@gmail.com",
+ "email_hash":"6d2661c5a65f24c8f0bb579b6e0a0d81",
+ "group":"Visitor",
+ "website":null,
+ "membership_date":null,
+ "github_uid":"1234599",
+ "github_nickname":"instructor",
+ "permissions":{"Clubhouse":"Administrator","Community":"Administrator","Liskov":"Administrator"}
+}
@@ -0,0 +1,10 @@
+{ "name":"Student",
+ "email":"student@gmail.com",
+ "email_hash":"6d2661c5a65f24c8f0bb579b6e0a0d81",
+ "group":"Visitor",
+ "website":null,
+ "membership_date":null,
+ "github_uid":"1234599",
+ "github_nickname":"student",
+ "permissions":{"Clubhouse":"Administrator","Community":"Administrator","Liskov":"Administrator"}
+}
@@ -0,0 +1,27 @@
+require "test_helper"
+
+describe TasksController do
+ before do
+ @course = FactoryGirl.create(:webdev)
+ @controller.current_person = build_person(:instructor, @course)
+ end
+
+ it "#new is not allowed to students" do
+ @controller.current_person = build_person(:student)
+
+ get(:new, course_id: @course.id)
+ response.redirect?.must_equal true
+ flash[:alert].must_equal "Unauthorized access"
+ end
+
+ it "#new is allowed to instructors" do
+ get(:new, course_id: @course.id)
+ response.success?.must_equal true
+ end
+
+ it "redirects to root if can't find course" do
+ get(:new, course_id: 'ohai!')
+ response.redirect?.must_equal true
+ flash[:alert].must_equal "Couldn't find course"
+ end
+end
Oops, something went wrong.

0 comments on commit 27b05b6

Please sign in to comment.