diff --git a/app/assets/javascripts/order_items.js.coffee b/app/assets/javascripts/order_items.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/order_items.js.coffee @@ -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/ diff --git a/app/controllers/order_items_controller.rb b/app/controllers/order_items_controller.rb new file mode 100644 index 000000000..ae076281e --- /dev/null +++ b/app/controllers/order_items_controller.rb @@ -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 diff --git a/app/helpers/order_items_helper.rb b/app/helpers/order_items_helper.rb new file mode 100644 index 000000000..e197528ae --- /dev/null +++ b/app/helpers/order_items_helper.rb @@ -0,0 +1,2 @@ +module OrderItemsHelper +end diff --git a/app/models/order.rb b/app/models/order.rb index 9e86912a5..3b524a97b 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/order_item.rb b/app/models/order_item.rb new file mode 100644 index 000000000..250723909 --- /dev/null +++ b/app/models/order_item.rb @@ -0,0 +1,7 @@ +class OrderItem < ActiveRecord::Base + attr_accessible :order_id, :price, :product_id, :quantity + + belongs_to :order + belongs_to :product + +end diff --git a/app/views/order_items/_form.html.haml b/app/views/order_items/_form.html.haml new file mode 100644 index 000000000..ff2924af4 --- /dev/null +++ b/app/views/order_items/_form.html.haml @@ -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' diff --git a/app/views/order_items/edit.html.haml b/app/views/order_items/edit.html.haml new file mode 100644 index 000000000..da759ba68 --- /dev/null +++ b/app/views/order_items/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing order_item + += render 'form' + += link_to 'Show', @order_item +\| += link_to 'Back', order_items_path diff --git a/app/views/order_items/index.html.haml b/app/views/order_items/index.html.haml new file mode 100644 index 000000000..6b63af7f9 --- /dev/null +++ b/app/views/order_items/index.html.haml @@ -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 diff --git a/app/views/order_items/new.html.haml b/app/views/order_items/new.html.haml new file mode 100644 index 000000000..2ba254512 --- /dev/null +++ b/app/views/order_items/new.html.haml @@ -0,0 +1,5 @@ +%h1 New order_item + += render 'form' + += link_to 'Back', order_items_path diff --git a/app/views/order_items/show.html.haml b/app/views/order_items/show.html.haml new file mode 100644 index 000000000..16551615a --- /dev/null +++ b/app/views/order_items/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 0ed446792..93d1a0c4a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ Growstuff::Application.routes.draw do + resources :order_items + + devise_for :members, :controllers => { :registrations => "registrations" } resources :authentications diff --git a/db/migrate/20130515033842_create_order_items.rb b/db/migrate/20130515033842_create_order_items.rb new file mode 100644 index 000000000..68b496dda --- /dev/null +++ b/db/migrate/20130515033842_create_order_items.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 090be1b27..56bbe7036 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/controllers/order_items_controller_spec.rb b/spec/controllers/order_items_controller_spec.rb new file mode 100644 index 000000000..74cd6d2ad --- /dev/null +++ b/spec/controllers/order_items_controller_spec.rb @@ -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 diff --git a/spec/factories/order_items.rb b/spec/factories/order_items.rb new file mode 100644 index 000000000..d99c324a5 --- /dev/null +++ b/spec/factories/order_items.rb @@ -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 diff --git a/spec/helpers/order_items_helper_spec.rb b/spec/helpers/order_items_helper_spec.rb new file mode 100644 index 000000000..5c4f6ae82 --- /dev/null +++ b/spec/helpers/order_items_helper_spec.rb @@ -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 diff --git a/spec/models/order_item_spec.rb b/spec/models/order_item_spec.rb new file mode 100644 index 000000000..959ea5fc0 --- /dev/null +++ b/spec/models/order_item_spec.rb @@ -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 diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 6a0cc6103..57a878701 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -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 diff --git a/spec/requests/order_items_spec.rb b/spec/requests/order_items_spec.rb new file mode 100644 index 000000000..dcf665e2b --- /dev/null +++ b/spec/requests/order_items_spec.rb @@ -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 diff --git a/spec/routing/order_items_routing_spec.rb b/spec/routing/order_items_routing_spec.rb new file mode 100644 index 000000000..1993db17d --- /dev/null +++ b/spec/routing/order_items_routing_spec.rb @@ -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 diff --git a/spec/views/order_items/edit.html.haml_spec.rb b/spec/views/order_items/edit.html.haml_spec.rb new file mode 100644 index 000000000..49dbc8005 --- /dev/null +++ b/spec/views/order_items/edit.html.haml_spec.rb @@ -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 diff --git a/spec/views/order_items/index.html.haml_spec.rb b/spec/views/order_items/index.html.haml_spec.rb new file mode 100644 index 000000000..0b4bdfb3d --- /dev/null +++ b/spec/views/order_items/index.html.haml_spec.rb @@ -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 diff --git a/spec/views/order_items/new.html.haml_spec.rb b/spec/views/order_items/new.html.haml_spec.rb new file mode 100644 index 000000000..70ed7cfa1 --- /dev/null +++ b/spec/views/order_items/new.html.haml_spec.rb @@ -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 diff --git a/spec/views/order_items/show.html.haml_spec.rb b/spec/views/order_items/show.html.haml_spec.rb new file mode 100644 index 000000000..8a445309a --- /dev/null +++ b/spec/views/order_items/show.html.haml_spec.rb @@ -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
" 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