From 456a071bf38424f0e0850ba7dc40dd07bf481356 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sat, 10 Aug 2013 08:36:43 -0400 Subject: [PATCH] Add more workouts functionality and tests --- app/assets/javascripts/workouts.js.coffee | 2 + app/controllers/workouts_controller.rb | 33 ++++++++++++ app/models/workout.rb | 1 + app/views/workouts/_form.html.erb | 7 +++ app/views/workouts/index.html.erb | 6 ++- app/views/workouts/new.html.erb | 1 + spec/features/workouts_spec.rb | 66 ++++++++++++++++++++++- spec/spec_helper.rb | 16 +----- 8 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 app/views/workouts/new.html.erb diff --git a/app/assets/javascripts/workouts.js.coffee b/app/assets/javascripts/workouts.js.coffee index 24f83d1..9016f8a 100644 --- a/app/assets/javascripts/workouts.js.coffee +++ b/app/assets/javascripts/workouts.js.coffee @@ -1,3 +1,5 @@ # 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://coffeescript.org/ +$ -> + $("#workout_date").datepicker({ dateFormat: 'yy-mm-dd' }); diff --git a/app/controllers/workouts_controller.rb b/app/controllers/workouts_controller.rb index 0ffd9c9..cfeefae 100644 --- a/app/controllers/workouts_controller.rb +++ b/app/controllers/workouts_controller.rb @@ -10,4 +10,37 @@ class WorkoutsController < ApplicationController def edit @workout = Workout.find(params[:id]) end + + def update + params.permit! + @workout = Workout.find(params[:id]) + + if @workout.update_attributes(params[:workout]) + redirect_to @workout, notice: 'Workout updated' + else + render action: 'edit' + end + end + + def new + @workout = Workout.new + end + + def create + params.permit! + @workout = Workout.new(params[:workout]) + + if @workout.save + redirect_to @workout, notice: 'Workout created' + else + render action: 'new' + end + end + + def destroy + @workout = Workout.find(params[:id]) + @workout.destroy + + redirect_to workouts_url, notice: 'Workout deleted' + end end diff --git a/app/models/workout.rb b/app/models/workout.rb index 6a57f89..ccfe050 100644 --- a/app/models/workout.rb +++ b/app/models/workout.rb @@ -1,2 +1,3 @@ class Workout < ActiveRecord::Base + validates :date, :presence => true end diff --git a/app/views/workouts/_form.html.erb b/app/views/workouts/_form.html.erb index 9c00b8d..7aef6e5 100644 --- a/app/views/workouts/_form.html.erb +++ b/app/views/workouts/_form.html.erb @@ -1,4 +1,11 @@ <%= form_for @workout do |f| %> + <% if @workout.errors.any? %> + + <% end %> <%= f.label :date %> <%= f.text_field :date %> <%= f.submit nil %> diff --git a/app/views/workouts/index.html.erb b/app/views/workouts/index.html.erb index a6be907..c572df8 100644 --- a/app/views/workouts/index.html.erb +++ b/app/views/workouts/index.html.erb @@ -1,8 +1,12 @@ +<%= link_to 'Create', new_workout_path %> +