Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

submitting exercises

  • Loading branch information...
commit d226821a3e477e3530a601fe0ca40163a26c128e 1 parent c3ca712
@lfborjas authored
View
3  app/assets/javascripts/exercises.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
95 app/controllers/exercises_controller.rb
@@ -0,0 +1,95 @@
+class ExercisesController < ApplicationController
+ # GET /exercises
+ # GET /exercises.json
+ before_filter :set_scope
+
+ def index
+ @exercises = Exercise.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @exercises }
+ end
+ end
+
+ # GET /exercises/1
+ # GET /exercises/1.json
+ def show
+ @exercise = @exercises_scope.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @exercise }
+ end
+ end
+
+ # GET /exercises/new
+ # GET /exercises/new.json
+ def new
+ @exercise = Exercise.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @exercise }
+ end
+ end
+
+ # GET /exercises/1/edit
+ def edit
+ @exercise = Exercise.find(params[:id])
+ end
+
+ # POST /exercises
+ # POST /exercises.json
+ def create
+ @exercise = @student.exercises.build(params[:exercise])
+
+ respond_to do |format|
+ if @exercise.save
+ format.html { redirect_to @exercise, notice: 'Exercise was successfully created.' }
+ format.json { render json: @exercise, status: :created, location: @exercise }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @exercise.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /exercises/1
+ # PUT /exercises/1.json
+ def update
+ @exercise = Exercise.find(params[:id])
+
+ respond_to do |format|
+ if @exercise.update_attributes(params[:exercise])
+ format.html { redirect_to @exercise, notice: 'Exercise was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @exercise.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /exercises/1
+ # DELETE /exercises/1.json
+ def destroy
+ @exercise = Exercise.find(params[:id])
+ @exercise.destroy
+
+ respond_to do |format|
+ format.html { redirect_to exercises_url }
+ format.json { head :no_content }
+ end
+ end
+
+ private
+ def set_scope
+ @exercises_scope = if params[:student_id].present?
+ @student = Student.find_by_account_number(params[:student_id])
+ @student.exercises
+ else
+ Exercise
+ end
+ end
+end
View
2  app/controllers/students_controller.rb
@@ -1,7 +1,7 @@
class StudentsController < ApplicationController
respond_to :json
def create
- @student = Student.find_or_create_by_email(params[:student][:email])
+ @student = Student.find_or_create_by_account_number(params[:student][:account_number])
respond_with @student
end
end
View
2  app/helpers/exercises_helper.rb
@@ -0,0 +1,2 @@
+module ExercisesHelper
+end
View
3  app/models/exercise.rb
@@ -0,0 +1,3 @@
+class Exercise < ActiveRecord::Base
+ belongs_to :student
+end
View
21 app/models/student.rb
@@ -1,18 +1,19 @@
class Student < ActiveRecord::Base
- belongs_to :challenge
- belongs_to :task
+ has_many :exercises
- def current_challenge
- challenge.to_param
+ def exercise_display
+ "#{exercises.count} / 10"
end
- def current_task
- task.try(:to_param)
+ def exercise_status
+ case exercises.count
+ when 0..4 then 'error'
+ when 5..9 then 'warning'
+ else 'success'
+ end
end
- def as_json(options = nil)
- options ||= {}
- options[:methods] = [:current_challenge, :current_task]
- super
+ def to_param
+ account_number
end
end
View
31 app/views/exercises/_form.html.erb
@@ -0,0 +1,31 @@
+<%= form_for @exercise, :html => { :class => 'form-horizontal' } do |f| %>
+ <fieldset>
+ <legend><%= controller.action_name.capitalize %> /Exercise</legend>
+
+ <div class="control-group">
+ <%= f.label :name, :class => 'control-label' %>
+ <div class="controls">
+ <%= f.text_field :name, :class => 'text_field' %>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <%= f.label :contents, :class => 'control-label' %>
+ <div class="controls">
+ <%= f.text_area :contents, :class => 'text_area' %>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <%= f.label :student_id, :class => 'control-label' %>
+ <div class="controls">
+ <%= f.number_field :student_id, :class => 'number_field' %>
+ </div>
+ </div>
+
+ <div class="form-actions">
+ <%= f.submit nil, :class => 'btn btn-primary' %>
+ <%= link_to 'Cancel', exercises_path, :class => 'btn' %>
+ </div>
+ </fieldset>
+<% end %>
View
1  app/views/exercises/edit.html.erb
@@ -0,0 +1 @@
+<%= render :partial => 'form' %>
View
26 app/views/exercises/index.html.erb
@@ -0,0 +1,26 @@
+<h1>Exercises</h1>
+<table class="table table-striped">
+ <thead>
+ <tr>
+ <th>ID</th>
+ <th>Name</th>
+ <th>Created at</th>
+ <th>Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @exercises.each do |exercise| %>
+ <tr>
+ <td><%= exercise.id %></td>
+ <td><%= link_to exercise.name, exercise_path(exercise) %></td>
+ <td><%= exercise.created_at %></td>
+ <td>
+ <%= link_to 'Edit', edit_exercise_path(exercise), :class => 'btn btn-mini' %>
+ <%= link_to 'Destroy', exercise_path(exercise), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+
+<%= link_to 'New', new_exercise_path, :class => 'btn btn-primary' %>
View
1  app/views/exercises/new.html.erb
@@ -0,0 +1 @@
+<%= render :partial => 'form' %>
View
22 app/views/exercises/show.html.erb
@@ -0,0 +1,22 @@
+<h1>/Exercise</h1>
+
+<p>
+ <b>Name</b><br>
+ <%= @exercise.name %>
+</p>
+
+<p>
+ <b>Contents</b><br>
+ <%= @exercise.contents %>
+</p>
+
+<p>
+ <b>Student</b><br>
+ <%= @exercise.student_id %>
+</p>
+
+<div class="form-actions">
+ <%= link_to 'Back', exercises_path, :class => 'btn' %>
+ <%= link_to 'Edit', edit_exercise_path(@exercise), :class => 'btn' %>
+ <%= link_to 'Delete', exercise_path(@exercise), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
+</div>
View
11 app/views/layouts/application.html.erb
@@ -63,17 +63,6 @@
<div class="span9">
<%= yield %>
</div>
- <div class="span3">
- <div class="well sidebar-nav">
- <h3>Sidebar</h3>
- <ul class="nav nav-list">
- <li class="nav-header">Sidebar</li>
- <li><%= link_to "Link1", "/path1" %></li>
- <li><%= link_to "Link2", "/path2" %></li>
- <li><%= link_to "Link3", "/path3" %></li>
- </ul>
- </div><!--/.well -->
- </div><!--/span-->
</div><!--/row-->
</div><!--/content-->
View
11 config/routes.rb
@@ -1,4 +1,8 @@
Teach::Application.routes.draw do
+ resources :exercises
+
+ root to: 'exercises#index'
+
devise_for :users
resources :tasks
@@ -6,12 +10,7 @@
resources :challenges
resources :students do
- resources :challenges, only: [:show] do
- post :take, on: :member
- resources :tasks, only: [:show] do
- get :current, on: :collection
- end
- end
+ resources :exercises, only: [:show, :create, :update]
end
# The priority is based upon order of creation:
View
6 db/migrate/20120413032510_add_account_number_to_students.rb
@@ -0,0 +1,6 @@
+class AddAccountNumberToStudents < ActiveRecord::Migration
+ def change
+ add_column :students, :account_number, :string
+
+ end
+end
View
11 db/migrate/20120413035451_create_exercises.rb
@@ -0,0 +1,11 @@
+class CreateExercises < ActiveRecord::Migration
+ def change
+ create_table :exercises do |t|
+ t.string :name
+ t.text :contents
+ t.integer :student_id
+
+ t.timestamps
+ end
+ end
+end
View
11 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120405005449) do
+ActiveRecord::Schema.define(:version => 20120413035451) do
create_table "challenges", :force => true do |t|
t.string "title"
@@ -27,6 +27,14 @@
t.datetime "updated_at", :null => false
end
+ create_table "exercises", :force => true do |t|
+ t.string "name"
+ t.text "contents"
+ t.integer "student_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "students", :force => true do |t|
t.string "email"
t.string "token"
@@ -35,6 +43,7 @@
t.integer "challenge_id"
t.integer "task_id"
t.datetime "latest_task_at"
+ t.string "account_number"
end
create_table "tasks", :force => true do |t|
View
11 test/fixtures/exercises.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ name: MyString
+ contents: MyText
+ student_id: 1
+
+two:
+ name: MyString
+ contents: MyText
+ student_id: 1
View
49 test/functional/exercises_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class ExercisesControllerTest < ActionController::TestCase
+ setup do
+ @exercise = exercises(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:exercises)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create exercise" do
+ assert_difference('Exercise.count') do
+ post :create, exercise: @exercise.attributes
+ end
+
+ assert_redirected_to exercise_path(assigns(:exercise))
+ end
+
+ test "should show exercise" do
+ get :show, id: @exercise
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @exercise
+ assert_response :success
+ end
+
+ test "should update exercise" do
+ put :update, id: @exercise, exercise: @exercise.attributes
+ assert_redirected_to exercise_path(assigns(:exercise))
+ end
+
+ test "should destroy exercise" do
+ assert_difference('Exercise.count', -1) do
+ delete :destroy, id: @exercise
+ end
+
+ assert_redirected_to exercises_path
+ end
+end
View
13 test/integration/exercises_api_test.rb
@@ -0,0 +1,13 @@
+require 'test_helper'
+
+class ExercisesApiTest < ActionDispatch::IntegrationTest
+ test "that an exercise can be posted" do
+ student = Student.create(account_number: '10611066')
+
+ post "/students/#{student.to_param}/exercises", :format => :json,
+ exercise: {name: "ex1.rb", contents: "hola mundo"}
+
+ assert_equal 1, student.exercises.count
+ assert_equal 'ex1.rb', student.exercises.first.name
+ end
+end
View
7 test/unit/exercise_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ExerciseTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/exercises_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class ExercisesHelperTest < ActionView::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.