Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[EPIC B] Add support for suspensions.

  • Loading branch information...
commit e77b93a2bce500594520148543b15c198c4fc0fc 1 parent 584beb0
Bernie Telles authored March 28, 2012
20  app/controllers/suspensions_controller.rb
... ...
@@ -0,0 +1,20 @@
  1
+class SuspensionsController < ApplicationController
  2
+  expose :bicycle
  3
+  expose :suspension
  4
+  expose(:suspensions) { bicycle.suspensions }
  5
+
  6
+  def create
  7
+    if suspension.save
  8
+      redirect_to [bicycle, suspension], notice: 'Suspension was successfully saved.'
  9
+    else
  10
+      render action: "new"
  11
+    end
  12
+  end
  13
+
  14
+  alias update create
  15
+
  16
+  def destroy
  17
+    suspension.destroy
  18
+    redirect_to bicycle_suspensions_url(bicycle)
  19
+  end
  20
+end
2  app/controllers/wheels_controller.rb
@@ -15,6 +15,6 @@ def create
15 15
 
16 16
   def destroy
17 17
     wheel.destroy
18  
-    redirect_to bicycles_wheels_url(bicycle)
  18
+    redirect_to bicycle_wheels_url(bicycle)
19 19
   end
20 20
 end
1  app/models/bicycle.rb
... ...
@@ -1,3 +1,4 @@
1 1
 class Bicycle < ActiveRecord::Base
2 2
   has_many :wheels
  3
+  has_many :suspensions
3 4
 end
3  app/models/suspension.rb
... ...
@@ -0,0 +1,3 @@
  1
+class Suspension < ActiveRecord::Base
  2
+  belongs_to :bicycle
  3
+end
13  app/views/suspensions/_form.html.haml
... ...
@@ -0,0 +1,13 @@
  1
+= form_for [bicycle, suspension] do |f|
  2
+  -if suspension.errors.any?
  3
+    #error_explanation
  4
+      %h2= "#{pluralize(suspension.errors.count, "error")} prohibited this suspension from being saved:"
  5
+      %ul
  6
+        - suspension.errors.full_messages.each do |msg|
  7
+          %li= msg
  8
+
  9
+  .field
  10
+    = f.label :name
  11
+    = f.text_field :name
  12
+  .actions
  13
+    = f.submit 'Save'
7  app/views/suspensions/edit.html.haml
... ...
@@ -0,0 +1,7 @@
  1
+%h1 Editing suspension
  2
+
  3
+= render 'form'
  4
+
  5
+= link_to 'Show', [bicycle, suspension]
  6
+\|
  7
+= link_to 'Back', [bicycle, :suspensions]
19  app/views/suspensions/index.html.haml
... ...
@@ -0,0 +1,19 @@
  1
+%h1 Listing suspensions
  2
+
  3
+%table
  4
+  %tr
  5
+    %th Name
  6
+    %th
  7
+    %th
  8
+    %th
  9
+
  10
+  - suspensions.each do |suspension|
  11
+    %tr
  12
+      %td= suspension.name
  13
+      %td= link_to 'Show', [bicycle, suspension]
  14
+      %td= link_to 'Edit', [:edit, bicycle, suspension]
  15
+      %td= link_to 'Destroy', [bicycle, suspension], :confirm => 'Are you sure?', :method => :delete
  16
+
  17
+%br
  18
+
  19
+= link_to 'New Wheel', [:new, bicycle, :suspension]
5  app/views/suspensions/new.html.haml
... ...
@@ -0,0 +1,5 @@
  1
+%h1 New suspension
  2
+
  3
+= render 'form'
  4
+
  5
+= link_to 'Back', [bicycle, :suspensions]
9  app/views/suspensions/show.html.haml
... ...
@@ -0,0 +1,9 @@
  1
+%p#notice= notice
  2
+
  3
+%p
  4
+  %b Name:
  5
+  = suspension.name
  6
+
  7
+= link_to 'Edit', [:edit, bicycle, suspension]
  8
+\|
  9
+= link_to 'Back', [bicycle, :suspensions]
1  config/routes.rb
... ...
@@ -1,6 +1,7 @@
1 1
 WildfirePc::Application.routes.draw do
2 2
   resources :bicycles do
3 3
     resources :wheels
  4
+    resources :suspensions
4 5
   end
5 6
 
6 7
   root :to => "welcome#index"
10  db/migrate/20120328155319_create_suspensions.rb
... ...
@@ -0,0 +1,10 @@
  1
+class CreateSuspensions < ActiveRecord::Migration
  2
+  def change
  3
+    create_table :suspensions do |t|
  4
+      t.string :name
  5
+      t.integer :bicycle_id
  6
+
  7
+      t.timestamps
  8
+    end
  9
+  end
  10
+end
9  db/schema.rb
@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended to check this file into your version control system.
13 13
 
14  
-ActiveRecord::Schema.define(:version => 20120327210344) do
  14
+ActiveRecord::Schema.define(:version => 20120328155319) do
15 15
 
16 16
   create_table "bicycles", :force => true do |t|
17 17
     t.string   "name"
@@ -19,6 +19,13 @@
19 19
     t.datetime "updated_at", :null => false
20 20
   end
21 21
 
  22
+  create_table "suspensions", :force => true do |t|
  23
+    t.string   "name"
  24
+    t.integer  "bicycle_id"
  25
+    t.datetime "created_at", :null => false
  26
+    t.datetime "updated_at", :null => false
  27
+  end
  28
+
22 29
   create_table "wheels", :force => true do |t|
23 30
     t.string   "name"
24 31
     t.datetime "created_at", :null => false
8  spec/factories/suspensions.rb
... ...
@@ -0,0 +1,8 @@
  1
+# Read about factories at https://github.com/thoughtbot/factory_girl
  2
+
  3
+FactoryGirl.define do
  4
+  factory :suspension do
  5
+    name "MyString"
  6
+    bicycle_id 1
  7
+  end
  8
+end
7  spec/models/suspension_spec.rb
... ...
@@ -0,0 +1,7 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Suspension do
  4
+  it "belongs to a bicycle" do
  5
+    Suspension.create(:name => 'my suspension', :bicycle => Bicycle.create(:name => 'my bike')).should be
  6
+  end
  7
+end
50  spec/requests/suspensions_spec.rb
... ...
@@ -0,0 +1,50 @@
  1
+require 'spec_helper'
  2
+
  3
+describe "Suspensions" do
  4
+  let(:bicycle) { FactoryGirl.create(:bicycle) }
  5
+  let(:suspension) { FactoryGirl.create(:suspension, :name => 'First suspension', :bicycle => bicycle) }
  6
+
  7
+  describe "GET /suspensions" do
  8
+    it "lists all suspensions" do
  9
+      suspension
  10
+      visit bicycle_suspensions_path(bicycle)
  11
+      page.should have_css 'td', text: 'First suspension'
  12
+    end
  13
+  end
  14
+
  15
+  describe "#show" do
  16
+    it "shows a suspension" do
  17
+      visit bicycle_suspension_path(bicycle, suspension)
  18
+      page.should have_css 'p', text: 'First suspension'
  19
+    end
  20
+  end
  21
+
  22
+  describe "#new" do
  23
+    it "lets you create new suspensions" do
  24
+      visit new_bicycle_suspension_path(bicycle)
  25
+      fill_in('Name', with: 'my first suspension')
  26
+      click_button 'Save'
  27
+      page.should have_css('#notice', text: 'Suspension was successfully saved')
  28
+    end
  29
+  end
  30
+
  31
+  describe "#edit" do
  32
+    it "lets you change existing suspensions" do
  33
+      visit edit_bicycle_suspension_path(bicycle, suspension)
  34
+      fill_in('Name', with: 'my second suspension')
  35
+      click_button 'Save'
  36
+      page.should have_css('#notice', text: 'Suspension was successfully saved')
  37
+      page.should have_css('p', text: 'my second suspension')
  38
+    end
  39
+  end
  40
+
  41
+  describe "#destroy", js: true do
  42
+    it "lets you destroy/remove an existing suspension" do
  43
+      suspension
  44
+      visit bicycle_suspensions_path(bicycle)
  45
+      page.evaluate_script('window.confirm = function() { return true; }')
  46
+      click_link 'Destroy'
  47
+      page.should_not have_css('td', text: 'my first suspension')
  48
+    end
  49
+  end
  50
+end

0 notes on commit e77b93a

Please sign in to comment.
Something went wrong with that request. Please try again.