Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added order items

  • Loading branch information...
commit 8c7ab93f8398a7fac940509ecb8a5b3f8df6e85f 1 parent 56b3c79
@filiptepper authored
View
31 app/controllers/items_controller.rb
@@ -0,0 +1,31 @@
+class ItemsController < ApplicationController
+ before_filter :authenticate_user!
+ before_filter :find_order
+
+ def new
+ @item = @order.items.build
+ end
+
+ def create
+ @item = @order.items.build(item_params)
+ @item.user = current_user
+
+ if @item.save
+ redirect_to orders_path(@order), notice: "Can't wait!"
+ else
+ render action: :new
+ end
+ end
+
+
+ private
+
+
+ def find_order
+ @order = Order.find(params[:order_id])
+ end
+
+ def item_params
+ params.require(:item).permit(:name)
+ end
+end
View
4 app/controllers/orders_controller.rb
@@ -20,6 +20,10 @@ def create
end
end
+ def show
+ @order = Order.find(params[:id])
+ end
+
private
View
4 app/models/item.rb
@@ -0,0 +1,4 @@
+class Item < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :order
+end
View
9 app/models/order.rb
@@ -1,8 +1,17 @@
class Order < ActiveRecord::Base
belongs_to :user
+ has_many :items
scope :current, -> {
where(Arel::Table.new(:orders)[:created_at].gt(Date.today - 1)).
where(Arel::Table.new(:orders)[:created_at].lt(Date.today + 1))
}
+
+ def has_item_for?(user)
+ self.item_for(user).present?
+ end
+
+ def item_for(user)
+ self.items.find_by(:user_id => user.id)
+ end
end
View
3  app/models/user.rb
@@ -1,4 +1,7 @@
class User < ActiveRecord::Base
include Gravtastic
gravtastic
+
+ has_many :orders
+ has_many :items, through: :orders
end
View
19 app/views/items/new.html.erb
@@ -0,0 +1,19 @@
+<%= form_for [@order, @item], html: { class: 'form-horizontal well' } do |f| %>
+ <fieldset>
+ <legend>
+ I want to order at
+ <span class="label-important"><%= @order.place %></span>!
+ </legend>
+
+ <div class="control-group">
+ <%= f.label :name, class: 'control-label' %>
+ <div class="controls">
+ <%= f.text_field :name %>
+ </div>
+ </div>
+
+ <div class="form-actions">
+ <%= f.submit 'Order!', class: 'btn btn-primary' %>
+ </div>
+ </fieldset>
+<% end %>
View
14 app/views/orders/index.html.erb
@@ -11,7 +11,17 @@
<tbody>
<% @orders.each do |order| -%>
<tr>
- <td><%= order.place %></td>
+ <td>
+ <%= link_to order.place, order_path(order) %>
+ <br>
+ <% if order.has_item_for?(current_user) -%>
+ You're having
+ <span class="label-success"><%= order.item_for(current_user).name %></span>.
+ <% else -%>
+ You didn't order anything.
+ <%= link_to "Place", new_order_item_path(order) %> your order now!
+ <% end -%>
+ </td>
<td>
<%= order.user.username %>
</td>
@@ -20,7 +30,7 @@
</tbody>
</table>
-Want something else? Go ahead an <%= link_to "order", new_order_path %>!
+Want something else? Go ahead and <%= link_to "order", new_order_path %>!
<% else -%>
There's nothing on the menu today. :(
Why don't you <%= link_to "order something", new_order_path %>?
View
6 app/views/orders/new.html.erb
@@ -1,16 +1,16 @@
-<%= form_for @order, html: { class: "form-horizontal well" } do |f| %>
+<%= form_for @order, html: { class: 'form-horizontal well' } do |f| %>
<fieldset>
<legend>I want to order!</legend>
<div class="control-group">
- <%= f.label :place, class: "control-label" %>
+ <%= f.label :place, class: 'control-label' %>
<div class="controls">
<%= f.text_field :place %>
</div>
</div>
<div class="form-actions">
- <%= f.submit "Order!", class: "btn btn-primary" %>
+ <%= f.submit 'Order!', class: 'btn btn-primary' %>
</div>
</fieldset>
<% end %>
View
31 app/views/orders/show.html.erb
@@ -0,0 +1,31 @@
+<h1>
+ <span class="label-success"><%= @order.place %></span> order details
+</h1>
+
+<% if @order.items.any? -%>
+<table class="table">
+ <thead>
+ <tr>
+ <th>What?</th>
+ <th>Who?</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @order.items.each do |item| -%>
+ <tr>
+ <td>
+ <%= item.name %>
+ </td>
+ <td>
+ <%= item.user.username %>
+ </td>
+ </tr>
+ <% end -%>
+ </tbody>
+</table>
+
+<% else -%>
+No orders here. :(
+<% end -%>
+
+Show me <%= link_to 'all orders', orders_path %>.
View
4 config/routes.rb
@@ -1,6 +1,8 @@
Lunchzilla::Application.routes.draw do
resource :sessions, only: [:destroy]
- resources :orders, only: [:index, :new, :create]
+ resources :orders, only: [:index, :new, :create, :show] do
+ resources :items, only: [:new, :create]
+ end
get '/auth/:provider/callback', to: 'sessions#create'
root to: 'home#show'
View
10 db/migrate/20121101151633_create_items.rb
@@ -0,0 +1,10 @@
+class CreateItems < ActiveRecord::Migration
+ def change
+ create_table :items do |t|
+ t.references :user, index: true
+ t.references :order, index: true
+ t.string :name
+ t.timestamps
+ end
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20121031224905) do
+ActiveRecord::Schema.define(version: 20121101151633) do
+
+ create_table "items", force: true do |t|
+ t.integer "user_id"
+ t.integer "order_id"
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "items", ["order_id"], name: "index_items_on_order_id"
+ add_index "items", ["user_id"], name: "index_items_on_user_id"
create_table "orders", force: true do |t|
t.integer "user_id"
Please sign in to comment.
Something went wrong with that request. Please try again.