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? %>
+
+ <% @workout.errors.full_messages.each do |message| %>
+ - <%= message %>
+ <% end %>
+
+ <% 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 %>
+
<% @workouts.each do |workout| %>
-
- <%= link_to workout.date, workout %> [
+ <%= link_to workout.date, workout %> [
<%= link_to 'Edit', edit_workout_path(workout) %> |
+ <%= link_to 'Delete', workout_path(workout),
+ data: { confirm: 'Are you sure?' }, :method => :delete %>
]
<% end %>
diff --git a/app/views/workouts/new.html.erb b/app/views/workouts/new.html.erb
new file mode 100644
index 0000000..786950e
--- /dev/null
+++ b/app/views/workouts/new.html.erb
@@ -0,0 +1 @@
+<%= render :partial => 'form' %>
diff --git a/spec/features/workouts_spec.rb b/spec/features/workouts_spec.rb
index ca16488..02fc7a9 100644
--- a/spec/features/workouts_spec.rb
+++ b/spec/features/workouts_spec.rb
@@ -27,12 +27,74 @@ describe "Workouts" do
end
end
- describe "PUT /workouts/(:id)" do
- it "should edit the workout" do
+ describe "GET /workouts/(:id)" do
+ it "should show the edit form" do
visit workouts_path
find("#workout_#{@workout.id}").click_link 'Edit'
current_path.should == edit_workout_path(@workout)
end
end
+
+ describe "PUT /workouts/edit/(:id)" do
+ it 'should update a workout' do
+ visit workouts_path
+ find("#workout_#{@workout.id}").click_link 'Edit'
+
+ fill_in 'Date', :with => Date.today
+ click_button 'Update Workout'
+
+ page.should have_content 'Workout updated'
+ end
+ end
+
+ describe "GET /workouts/new" do
+ it "should show the create workout form" do
+ visit workouts_path
+ click_link 'Create'
+
+ current_path.should == new_workout_path
+ end
+ end
+
+ describe "POST /workouts/create" do
+ it "should create a new workout" do
+ visit workouts_path
+ click_link 'Create'
+
+ fill_in 'Date', :with => Date.today
+ click_button 'Create Workout'
+
+ page.should have_content 'Workout created'
+ end
+
+ it "should have a working javascript datepicker", :js => true do
+ visit workouts_path
+ click_link 'Create'
+
+ page.execute_script %Q{ $('#workout_date').trigger("focus") }
+ page.execute_script %Q{ $("a.ui-state-default:contains('10')").trigger("click") }
+ click_button 'Create Workout'
+
+ page.should have_content 'Workout created'
+ end
+
+ it "should not allow a blank date" do
+ visit workouts_path
+ click_link 'Create'
+ click_button 'Create Workout'
+
+ page.should have_content "Date can't be blank"
+ end
+ end
+
+ describe "DELETE /workouts/(:id)" do
+ it 'should delete a workout' do
+ visit workouts_path
+ find("#workout_#{@workout.id}").click_link 'Delete'
+
+ page.should have_content 'Workout deleted'
+ end
+ end
end
+
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 0fb565c..61655a3 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,16 +1,11 @@
-# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
-# Requires supporting ruby files with custom matchers and macros, etc,
-# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
-# Checks for pending migrations before tests are run.
-# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
@@ -25,23 +20,14 @@ RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
- # If you're not using ActiveRecord, or you'd prefer not to run each of your
- # examples within a transaction, remove the following line or assign false
- # instead of true.
config.use_transactional_fixtures = false
- # If true, the base class of anonymous controllers will be inferred
- # automatically. This will be the default behavior in future versions of
- # rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
- # Run specs in random order to surface order dependencies. If you find an
- # order dependency and want to debug it, you can fix the order by providing
- # the seed, which is printed after each run.
- # --seed 1234
config.order = "random"
config.before(:suite) do
+ Capybara.javascript_driver = :webkit #:webkit, #selenium
DatabaseCleaner.strategy = :truncation
end