rails g scaffold OrderItem...

This commit is contained in:
Skud
2013-05-15 13:45:32 +10:00
parent 1d54a63f5d
commit 7d74203735
24 changed files with 562 additions and 5 deletions

View File

@@ -0,0 +1,3 @@
# 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://jashkenas.github.com/coffee-script/

View File

@@ -0,0 +1,83 @@
class OrderItemsController < ApplicationController
# GET /order_items
# GET /order_items.json
def index
@order_items = OrderItem.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @order_items }
end
end
# GET /order_items/1
# GET /order_items/1.json
def show
@order_item = OrderItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @order_item }
end
end
# GET /order_items/new
# GET /order_items/new.json
def new
@order_item = OrderItem.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @order_item }
end
end
# GET /order_items/1/edit
def edit
@order_item = OrderItem.find(params[:id])
end
# POST /order_items
# POST /order_items.json
def create
@order_item = OrderItem.new(params[:order_item])
respond_to do |format|
if @order_item.save
format.html { redirect_to @order_item, notice: 'Order item was successfully created.' }
format.json { render json: @order_item, status: :created, location: @order_item }
else
format.html { render action: "new" }
format.json { render json: @order_item.errors, status: :unprocessable_entity }
end
end
end
# PUT /order_items/1
# PUT /order_items/1.json
def update
@order_item = OrderItem.find(params[:id])
respond_to do |format|
if @order_item.update_attributes(params[:order_item])
format.html { redirect_to @order_item, notice: 'Order item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @order_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /order_items/1
# DELETE /order_items/1.json
def destroy
@order_item = OrderItem.find(params[:id])
@order_item.destroy
respond_to do |format|
format.html { redirect_to order_items_url }
format.json { head :no_content }
end
end
end

View File

@@ -0,0 +1,2 @@
module OrderItemsHelper
end

View File

@@ -2,7 +2,7 @@ class Order < ActiveRecord::Base
attr_accessible :member_id, :completed_at
belongs_to :member
has_and_belongs_to_many :products
has_many :order_items
default_scope order('created_at DESC')
end

7
app/models/order_item.rb Normal file
View File

@@ -0,0 +1,7 @@
class OrderItem < ActiveRecord::Base
attr_accessible :order_id, :price, :product_id, :quantity
belongs_to :order
belongs_to :product
end

View File

@@ -0,0 +1,22 @@
= form_for @order_item do |f|
- if @order_item.errors.any?
#error_explanation
%h2= "#{pluralize(@order_item.errors.count, "error")} prohibited this order_item from being saved:"
%ul
- @order_item.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :order_id
= f.number_field :order_id
.field
= f.label :product_id
= f.number_field :product_id
.field
= f.label :price
= f.text_field :price
.field
= f.label :quantity
= f.number_field :quantity
.actions
= f.submit 'Save'

View File

@@ -0,0 +1,7 @@
%h1 Editing order_item
= render 'form'
= link_to 'Show', @order_item
\|
= link_to 'Back', order_items_path

View File

@@ -0,0 +1,25 @@
%h1 Listing order_items
%table
%tr
%th Order
%th Product
%th Price
%th Quantity
%th
%th
%th
- @order_items.each do |order_item|
%tr
%td= order_item.order_id
%td= order_item.product_id
%td= order_item.price
%td= order_item.quantity
%td= link_to 'Show', order_item
%td= link_to 'Edit', edit_order_item_path(order_item)
%td= link_to 'Destroy', order_item, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Order item', new_order_item_path

View File

@@ -0,0 +1,5 @@
%h1 New order_item
= render 'form'
= link_to 'Back', order_items_path

View File

@@ -0,0 +1,18 @@
%p#notice= notice
%p
%b Order:
= @order_item.order_id
%p
%b Product:
= @order_item.product_id
%p
%b Price:
= @order_item.price
%p
%b Quantity:
= @order_item.quantity
= link_to 'Edit', edit_order_item_path(@order_item)
\|
= link_to 'Back', order_items_path

View File

@@ -1,5 +1,8 @@
Growstuff::Application.routes.draw do
resources :order_items
devise_for :members, :controllers => { :registrations => "registrations" }
resources :authentications

View File

@@ -0,0 +1,12 @@
class CreateOrderItems < ActiveRecord::Migration
def change
create_table :order_items do |t|
t.integer :order_id
t.integer :product_id
t.decimal :price
t.integer :quantity
t.timestamps
end
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130508050711) do
ActiveRecord::Schema.define(:version => 20130515033842) do
create_table "authentications", :force => true do |t|
t.integer "member_id", :null => false
@@ -40,6 +40,7 @@ ActiveRecord::Schema.define(:version => 20130508050711) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
t.integer "parent_id"
end
add_index "crops", ["slug"], :name => "index_crops_on_slug", :unique => true
@@ -120,6 +121,15 @@ ActiveRecord::Schema.define(:version => 20130508050711) do
t.datetime "updated_at", :null => false
end
create_table "order_items", :force => true do |t|
t.integer "order_id"
t.integer "product_id"
t.decimal "price"
t.integer "quantity"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "orders", :force => true do |t|
t.string "member_id", :null => false
t.datetime "created_at", :null => false
@@ -140,6 +150,19 @@ ActiveRecord::Schema.define(:version => 20130508050711) do
t.datetime "updated_at", :null => false
end
create_table "photos", :force => true do |t|
t.integer "owner_id", :null => false
t.integer "flickr_photo_id", :null => false
t.string "thumbnail_url", :null => false
t.string "fullsize_url", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "title", :null => false
t.string "license_name", :null => false
t.string "license_url"
t.string "link_url", :null => false
end
create_table "plantings", :force => true do |t|
t.integer "garden_id", :null => false
t.integer "crop_id", :null => false

View File

@@ -0,0 +1,164 @@
require 'spec_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
describe OrderItemsController do
# This should return the minimal set of attributes required to create a valid
# OrderItem. As you add validations to OrderItem, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ "order_id" => "1" }
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# OrderItemsController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all order_items as @order_items" do
order_item = OrderItem.create! valid_attributes
get :index, {}, valid_session
assigns(:order_items).should eq([order_item])
end
end
describe "GET show" do
it "assigns the requested order_item as @order_item" do
order_item = OrderItem.create! valid_attributes
get :show, {:id => order_item.to_param}, valid_session
assigns(:order_item).should eq(order_item)
end
end
describe "GET new" do
it "assigns a new order_item as @order_item" do
get :new, {}, valid_session
assigns(:order_item).should be_a_new(OrderItem)
end
end
describe "GET edit" do
it "assigns the requested order_item as @order_item" do
order_item = OrderItem.create! valid_attributes
get :edit, {:id => order_item.to_param}, valid_session
assigns(:order_item).should eq(order_item)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new OrderItem" do
expect {
post :create, {:order_item => valid_attributes}, valid_session
}.to change(OrderItem, :count).by(1)
end
it "assigns a newly created order_item as @order_item" do
post :create, {:order_item => valid_attributes}, valid_session
assigns(:order_item).should be_a(OrderItem)
assigns(:order_item).should be_persisted
end
it "redirects to the created order_item" do
post :create, {:order_item => valid_attributes}, valid_session
response.should redirect_to(OrderItem.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved order_item as @order_item" do
# Trigger the behavior that occurs when invalid params are submitted
OrderItem.any_instance.stub(:save).and_return(false)
post :create, {:order_item => { "order_id" => "invalid value" }}, valid_session
assigns(:order_item).should be_a_new(OrderItem)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
OrderItem.any_instance.stub(:save).and_return(false)
post :create, {:order_item => { "order_id" => "invalid value" }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested order_item" do
order_item = OrderItem.create! valid_attributes
# Assuming there are no other order_items in the database, this
# specifies that the OrderItem created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
OrderItem.any_instance.should_receive(:update_attributes).with({ "order_id" => "1" })
put :update, {:id => order_item.to_param, :order_item => { "order_id" => "1" }}, valid_session
end
it "assigns the requested order_item as @order_item" do
order_item = OrderItem.create! valid_attributes
put :update, {:id => order_item.to_param, :order_item => valid_attributes}, valid_session
assigns(:order_item).should eq(order_item)
end
it "redirects to the order_item" do
order_item = OrderItem.create! valid_attributes
put :update, {:id => order_item.to_param, :order_item => valid_attributes}, valid_session
response.should redirect_to(order_item)
end
end
describe "with invalid params" do
it "assigns the order_item as @order_item" do
order_item = OrderItem.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
OrderItem.any_instance.stub(:save).and_return(false)
put :update, {:id => order_item.to_param, :order_item => { "order_id" => "invalid value" }}, valid_session
assigns(:order_item).should eq(order_item)
end
it "re-renders the 'edit' template" do
order_item = OrderItem.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
OrderItem.any_instance.stub(:save).and_return(false)
put :update, {:id => order_item.to_param, :order_item => { "order_id" => "invalid value" }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested order_item" do
order_item = OrderItem.create! valid_attributes
expect {
delete :destroy, {:id => order_item.to_param}, valid_session
}.to change(OrderItem, :count).by(-1)
end
it "redirects to the order_items list" do
order_item = OrderItem.create! valid_attributes
delete :destroy, {:id => order_item.to_param}, valid_session
response.should redirect_to(order_items_url)
end
end
end

View File

@@ -0,0 +1,10 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :order_item do
order
product
price "9.99"
quantity 1
end
end

View File

@@ -0,0 +1,15 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the OrderItemsHelper. For example:
#
# describe OrderItemsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe OrderItemsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,13 @@
require 'spec_helper'
describe OrderItem do
before(:each) do
@order_item = FactoryGirl.create(:order_item)
end
it "has an order and a product" do
@order_item.order.should be_an_instance_of Order
@order_item.product.should be_an_instance_of Product
end
end

View File

@@ -4,11 +4,12 @@ describe Order do
before(:each) do
@order = FactoryGirl.create(:order)
@product = FactoryGirl.create(:product)
@order.products << @product
@order_item = FactoryGirl.create(:order_item,
:order_id => @order.id, :product_id => @product.id)
end
it 'has products' do
@order.products.first.should eq @product
it 'has order_items' do
@order.order_items.first.should eq @order_item
end
it 'sorts by created_at DESC' do

View File

@@ -0,0 +1,11 @@
require 'spec_helper'
describe "OrderItems" do
describe "GET /order_items" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get order_items_path
response.status.should be(200)
end
end
end

View File

@@ -0,0 +1,35 @@
require "spec_helper"
describe OrderItemsController do
describe "routing" do
it "routes to #index" do
get("/order_items").should route_to("order_items#index")
end
it "routes to #new" do
get("/order_items/new").should route_to("order_items#new")
end
it "routes to #show" do
get("/order_items/1").should route_to("order_items#show", :id => "1")
end
it "routes to #edit" do
get("/order_items/1/edit").should route_to("order_items#edit", :id => "1")
end
it "routes to #create" do
post("/order_items").should route_to("order_items#create")
end
it "routes to #update" do
put("/order_items/1").should route_to("order_items#update", :id => "1")
end
it "routes to #destroy" do
delete("/order_items/1").should route_to("order_items#destroy", :id => "1")
end
end
end

View File

@@ -0,0 +1,24 @@
require 'spec_helper'
describe "order_items/edit" do
before(:each) do
@order_item = assign(:order_item, stub_model(OrderItem,
:order_id => 1,
:product_id => 1,
:price => "9.99",
:quantity => 1
))
end
it "renders the edit order_item form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => order_items_path(@order_item), :method => "post" do
assert_select "input#order_item_order_id", :name => "order_item[order_id]"
assert_select "input#order_item_product_id", :name => "order_item[product_id]"
assert_select "input#order_item_price", :name => "order_item[price]"
assert_select "input#order_item_quantity", :name => "order_item[quantity]"
end
end
end

View File

@@ -0,0 +1,29 @@
require 'spec_helper'
describe "order_items/index" do
before(:each) do
assign(:order_items, [
stub_model(OrderItem,
:order_id => 1,
:product_id => 2,
:price => "9.99",
:quantity => 3
),
stub_model(OrderItem,
:order_id => 1,
:product_id => 2,
:price => "9.99",
:quantity => 3
)
])
end
it "renders a list of order_items" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => 1.to_s, :count => 2
assert_select "tr>td", :text => 2.to_s, :count => 2
assert_select "tr>td", :text => "9.99".to_s, :count => 2
assert_select "tr>td", :text => 3.to_s, :count => 2
end
end

View File

@@ -0,0 +1,24 @@
require 'spec_helper'
describe "order_items/new" do
before(:each) do
assign(:order_item, stub_model(OrderItem,
:order_id => 1,
:product_id => 1,
:price => "9.99",
:quantity => 1
).as_new_record)
end
it "renders new order_item form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => order_items_path, :method => "post" do
assert_select "input#order_item_order_id", :name => "order_item[order_id]"
assert_select "input#order_item_product_id", :name => "order_item[product_id]"
assert_select "input#order_item_price", :name => "order_item[price]"
assert_select "input#order_item_quantity", :name => "order_item[quantity]"
end
end
end

View File

@@ -0,0 +1,21 @@
require 'spec_helper'
describe "order_items/show" do
before(:each) do
@order_item = assign(:order_item, stub_model(OrderItem,
:order_id => 1,
:product_id => 2,
:price => "9.99",
:quantity => 3
))
end
it "renders attributes in <p>" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/1/)
rendered.should match(/2/)
rendered.should match(/9.99/)
rendered.should match(/3/)
end
end