Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: btelles/wildfire_pc
base: 584beb00e3
...
head fork: btelles/wildfire_pc
compare: cc93d4fbf2
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
View
35 app/controllers/bicycles_controller.rb
@@ -1,12 +1,23 @@
class BicyclesController < ApplicationController
+
+ respond_to :html, :json
+
expose :bicycle
expose(:bicycles) { Bicycle.scoped }
+ def index
+ respond_with bicycles
+ end
+
def create
- if bicycle.save
- redirect_to bicycle, notice: 'Bicycle was successfully saved.'
+ if params[:bicycle].present?
+ if bicycle.save
+ redirect_to bicycle, notice: 'Bicycle was successfully saved.'
+ else
+ respond_with bicycle
+ end
else
- render action: "new"
+ batch_create
end
end
@@ -16,4 +27,22 @@ def destroy
bicycle.destroy
redirect_to bicycles_url
end
+
+ def batch_create
+ if params[:bicycles].present?
+ bikes = []
+ all_saved = false
+ ActiveRecord::Base.transaction do
+ params[:bicycles].each do |bicycle_hash|
+ bikes << Bicycle.new(bicycle_hash)
+ end
+ all_saved = bikes.all? { |bike| bike.save}
+ end
+ all_saved ? response.status= 200 : response.status= 422
+ render :text => ''
+ else
+ response.status= 422
+ render :text => "Please provide a bicycle or a list of bicycles"
+ end
+ end
end
View
20 app/controllers/suspensions_controller.rb
@@ -0,0 +1,20 @@
+class SuspensionsController < ApplicationController
+ expose :bicycle
+ expose :suspension
+ expose(:suspensions) { bicycle.suspensions }
+
+ def create
+ if suspension.save
+ redirect_to [bicycle, suspension], notice: 'Suspension was successfully saved.'
+ else
+ render action: "new"
+ end
+ end
+
+ alias update create
+
+ def destroy
+ suspension.destroy
+ redirect_to bicycle_suspensions_url(bicycle)
+ end
+end
View
2  app/controllers/wheels_controller.rb
@@ -15,6 +15,6 @@ def create
def destroy
wheel.destroy
- redirect_to bicycles_wheels_url(bicycle)
+ redirect_to bicycle_wheels_url(bicycle)
end
end
View
3  app/models/bicycle.rb
@@ -1,3 +1,6 @@
class Bicycle < ActiveRecord::Base
has_many :wheels
+ has_many :suspensions
+
+ accepts_nested_attributes_for :suspensions, :wheels
end
View
3  app/models/suspension.rb
@@ -0,0 +1,3 @@
+class Suspension < ActiveRecord::Base
+ belongs_to :bicycle
+end
View
13 app/views/suspensions/_form.html.haml
@@ -0,0 +1,13 @@
+= form_for [bicycle, suspension] do |f|
+ -if suspension.errors.any?
+ #error_explanation
+ %h2= "#{pluralize(suspension.errors.count, "error")} prohibited this suspension from being saved:"
+ %ul
+ - suspension.errors.full_messages.each do |msg|
+ %li= msg
+
+ .field
+ = f.label :name
+ = f.text_field :name
+ .actions
+ = f.submit 'Save'
View
7 app/views/suspensions/edit.html.haml
@@ -0,0 +1,7 @@
+%h1 Editing suspension
+
+= render 'form'
+
+= link_to 'Show', [bicycle, suspension]
+\|
+= link_to 'Back', [bicycle, :suspensions]
View
19 app/views/suspensions/index.html.haml
@@ -0,0 +1,19 @@
+%h1 Listing suspensions
+
+%table
+ %tr
+ %th Name
+ %th
+ %th
+ %th
+
+ - suspensions.each do |suspension|
+ %tr
+ %td= suspension.name
+ %td= link_to 'Show', [bicycle, suspension]
+ %td= link_to 'Edit', [:edit, bicycle, suspension]
+ %td= link_to 'Destroy', [bicycle, suspension], :confirm => 'Are you sure?', :method => :delete
+
+%br
+
+= link_to 'New Wheel', [:new, bicycle, :suspension]
View
5 app/views/suspensions/new.html.haml
@@ -0,0 +1,5 @@
+%h1 New suspension
+
+= render 'form'
+
+= link_to 'Back', [bicycle, :suspensions]
View
9 app/views/suspensions/show.html.haml
@@ -0,0 +1,9 @@
+%p#notice= notice
+
+%p
+ %b Name:
+ = suspension.name
+
+= link_to 'Edit', [:edit, bicycle, suspension]
+\|
+= link_to 'Back', [bicycle, :suspensions]
View
1  config/routes.rb
@@ -1,6 +1,7 @@
WildfirePc::Application.routes.draw do
resources :bicycles do
resources :wheels
+ resources :suspensions
end
root :to => "welcome#index"
View
10 db/migrate/20120328155319_create_suspensions.rb
@@ -0,0 +1,10 @@
+class CreateSuspensions < ActiveRecord::Migration
+ def change
+ create_table :suspensions do |t|
+ t.string :name
+ t.integer :bicycle_id
+
+ t.timestamps
+ end
+ end
+end
View
9 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120327210344) do
+ActiveRecord::Schema.define(:version => 20120328155319) do
create_table "bicycles", :force => true do |t|
t.string "name"
@@ -19,6 +19,13 @@
t.datetime "updated_at", :null => false
end
+ create_table "suspensions", :force => true do |t|
+ t.string "name"
+ t.integer "bicycle_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "wheels", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
View
42 spec/api/bicycle_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe "Bicycle API", :type => :request do
+ let(:bicycle) { FactoryGirl.create(:bicycle)}
+
+ def json_response
+ JSON.parse(response.body)
+ end
+
+ it "can list bicycles" do
+ bicycle
+ get '/bicycles.json'
+ json_response[0]['name'].should == bicycle.name
+ json_response[0]['id'].should == bicycle.id
+ end
+
+ it "can create bicycles" do
+ post '/bicycles', {:bicycle => {:name => 'my first bike'}}
+ Bicycle.first.name.should == 'my first bike'
+ end
+
+ it "can create bicycles with suspensions" do
+ post '/bicycles', {:bicycle => {:name => 'my first bike', :suspensions_attributes => [{:name => 'my first suspension'}]}}
+ Bicycle.first.suspensions.first.name.should == 'my first suspension'
+ end
+
+ it "can create bicycles with wheels" do
+ post '/bicycles', {:bicycle => {:name => 'my first bike', :wheels_attributes => [{:name => 'my first wheel'}]}}
+ Bicycle.first.wheels.first.name.should == 'my first wheel'
+ end
+
+ it "can create multiple bicycles in one request" do
+ post '/bicycles', {:bicycles => [{:name => 'my first bike', :wheels_attributes => [{:name => 'my first wheel'}]},
+ {:name => 'my second bike', :wheels_attributes => [{:name => 'my second wheel'}]}]}
+ names = Bicycle.all.map(&:name)
+ names.should include('my first bike')
+ names.should include('my second bike')
+ wheels = Bicycle.all.map(&:wheels).flatten.map(&:name)
+ wheels.should include('my first wheel')
+ wheels.should include('my second wheel')
+ end
+end
View
8 spec/factories/suspensions.rb
@@ -0,0 +1,8 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :suspension do
+ name "MyString"
+ bicycle_id 1
+ end
+end
View
7 spec/models/suspension_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe Suspension do
+ it "belongs to a bicycle" do
+ Suspension.create(:name => 'my suspension', :bicycle => Bicycle.create(:name => 'my bike')).should be
+ end
+end
View
50 spec/requests/suspensions_spec.rb
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+describe "Suspensions" do
+ let(:bicycle) { FactoryGirl.create(:bicycle) }
+ let(:suspension) { FactoryGirl.create(:suspension, :name => 'First suspension', :bicycle => bicycle) }
+
+ describe "GET /suspensions" do
+ it "lists all suspensions" do
+ suspension
+ visit bicycle_suspensions_path(bicycle)
+ page.should have_css 'td', text: 'First suspension'
+ end
+ end
+
+ describe "#show" do
+ it "shows a suspension" do
+ visit bicycle_suspension_path(bicycle, suspension)
+ page.should have_css 'p', text: 'First suspension'
+ end
+ end
+
+ describe "#new" do
+ it "lets you create new suspensions" do
+ visit new_bicycle_suspension_path(bicycle)
+ fill_in('Name', with: 'my first suspension')
+ click_button 'Save'
+ page.should have_css('#notice', text: 'Suspension was successfully saved')
+ end
+ end
+
+ describe "#edit" do
+ it "lets you change existing suspensions" do
+ visit edit_bicycle_suspension_path(bicycle, suspension)
+ fill_in('Name', with: 'my second suspension')
+ click_button 'Save'
+ page.should have_css('#notice', text: 'Suspension was successfully saved')
+ page.should have_css('p', text: 'my second suspension')
+ end
+ end
+
+ describe "#destroy", js: true do
+ it "lets you destroy/remove an existing suspension" do
+ suspension
+ visit bicycle_suspensions_path(bicycle)
+ page.evaluate_script('window.confirm = function() { return true; }')
+ click_link 'Destroy'
+ page.should_not have_css('td', text: 'my first suspension')
+ end
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.