Skip to content

Commit

Permalink
added order items
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Tepper committed Nov 1, 2012
1 parent 56b3c79 commit 8c7ab93
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 7 deletions.
31 changes: 31 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def create
end
end

def show
@order = Order.find(params[:id])
end


private

Expand Down
4 changes: 4 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :order
end
9 changes: 9 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class User < ActiveRecord::Base
include Gravtastic
gravtastic

has_many :orders
has_many :items, through: :orders
end
19 changes: 19 additions & 0 deletions app/views/items/new.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
14 changes: 12 additions & 2 deletions app/views/orders/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 %>?
Expand Down
6 changes: 3 additions & 3 deletions app/views/orders/new.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
31 changes: 31 additions & 0 deletions app/views/orders/show.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>.
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20121101151633_create_items.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8c7ab93

Please sign in to comment.