-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Filip Tepper
committed
Nov 1, 2012
1 parent
56b3c79
commit 8c7ab93
Showing
12 changed files
with
141 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ def create | |
end | ||
end | ||
|
||
def show | ||
@order = Order.find(params[:id]) | ||
end | ||
|
||
|
||
private | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters