Add more workouts functionality and tests

This commit is contained in:
Andrew Tomaka 2013-08-10 08:36:43 -04:00
parent 10d4075f48
commit 456a071bf3
8 changed files with 114 additions and 18 deletions

View file

@ -1,3 +1,5 @@
# Place all the behaviors and hooks related to the matching controller here. # Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js. # All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/ # You can use CoffeeScript in this file: http://coffeescript.org/
$ ->
$("#workout_date").datepicker({ dateFormat: 'yy-mm-dd' });

View file

@ -10,4 +10,37 @@ class WorkoutsController < ApplicationController
def edit def edit
@workout = Workout.find(params[:id]) @workout = Workout.find(params[:id])
end 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 end

View file

@ -1,2 +1,3 @@
class Workout < ActiveRecord::Base class Workout < ActiveRecord::Base
validates :date, :presence => true
end end

View file

@ -1,4 +1,11 @@
<%= form_for @workout do |f| %> <%= form_for @workout do |f| %>
<% if @workout.errors.any? %>
<ul>
<% @workout.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<%= f.label :date %> <%= f.label :date %>
<%= f.text_field :date %> <%= f.text_field :date %>
<%= f.submit nil %> <%= f.submit nil %>

View file

@ -1,8 +1,12 @@
<%= link_to 'Create', new_workout_path %>
<ul> <ul>
<% @workouts.each do |workout| %> <% @workouts.each do |workout| %>
<li id="workout_<%= workout.id %>"> <li id="workout_<%= workout.id %>">
<%= link_to workout.date, workout %> [ <%= link_to workout.date, workout %> [
<%= link_to 'Edit', edit_workout_path(workout) %> | <%= link_to 'Edit', edit_workout_path(workout) %> |
<%= link_to 'Delete', workout_path(workout),
data: { confirm: 'Are you sure?' }, :method => :delete %>
] ]
</li> </li>
<% end %> <% end %>

View file

@ -0,0 +1 @@
<%= render :partial => 'form' %>

View file

@ -27,12 +27,74 @@ describe "Workouts" do
end end
end end
describe "PUT /workouts/(:id)" do describe "GET /workouts/(:id)" do
it "should edit the workout" do it "should show the edit form" do
visit workouts_path visit workouts_path
find("#workout_#{@workout.id}").click_link 'Edit' find("#workout_#{@workout.id}").click_link 'Edit'
current_path.should == edit_workout_path(@workout) current_path.should == edit_workout_path(@workout)
end end
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
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

View file

@ -1,16 +1,11 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test' ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__) require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails' require 'rspec/rails'
require 'rspec/autorun' require 'rspec/autorun'
require 'capybara/rspec' 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 } 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) ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config| RSpec.configure do |config|
@ -25,23 +20,14 @@ RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/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 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 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.order = "random"
config.before(:suite) do config.before(:suite) do
Capybara.javascript_driver = :webkit #:webkit, #selenium
DatabaseCleaner.strategy = :truncation DatabaseCleaner.strategy = :truncation
end end