Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ group :doc do
gem 'sdoc', require: false
end

gem 'haml'

gem 'hirb'

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ GEM
coffee-script-source (1.6.3)
erubis (2.7.0)
execjs (2.0.2)
haml (4.0.3)
tilt
hike (1.2.3)
hirb (0.7.1)
i18n (0.6.5)
jbuilder (1.5.2)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -116,6 +119,8 @@ PLATFORMS

DEPENDENCIES
coffee-rails (~> 4.0.0)
haml
hirb
jbuilder (~> 1.2)
jquery-rails
pry
Expand Down
35 changes: 21 additions & 14 deletions app/controllers/ingredients_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
class IngredientsController < ApplicationController
def create
# @menu_item =
# @ingredient =

end

private
# Use callbacks to share common setup or constraints between actions.
# def set_menu_item
# @menu_item = MenuItem.find(params[:id])
# end
def show
@ingredients = Ingredient.all
end

def create
@ingredient = Ingredient.new(ingredient_params)

# # Never trust parameters from the scary internet, only allow the white list through.
# def menu_item_params
# params.require(:menu_item).permit(:name)
# end
respond_to do |format|
if @ingredient.save
format.html { redirect_to @ingredient, notice: 'Ingredient was successfully created.' }
format.json { render action: 'show', status: :created, location: @ingredient }
else
format.html { render action: 'new' }
format.json { render json: @ingredient.errors, status: :unprocessable_entity }
end
end
end

private

def ingredient_params
params.require(:ingredient).permit(:name)
end
end
2 changes: 2 additions & 0 deletions app/controllers/menu_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class MenuItemsController < ApplicationController
# GET /menu_items.json
def index
@menu_items = MenuItem.all
@ingredients = Ingredient.all
end

# GET /menu_items/1
# GET /menu_items/1.json
def show
@ingredients = @menu_item.ingredients
end

# GET /menu_items/new
Expand Down
4 changes: 2 additions & 2 deletions app/models/ingredient.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Ingredient < ActiveRecord::Base
has_many :recipes
has_many :menu_items, through: :recipes
has_many :recipes
has_many :menu_items, :through => :recipe
end
8 changes: 6 additions & 2 deletions app/models/menu_item.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class MenuItem < ActiveRecord::Base
has_one :recipe
has_many :ingredients, through: :recipe
has_one :recipe
has_many :ingredients, :through => :recipe

validates :name, presence: true

accepts_nested_attributes_for :ingredients
end
4 changes: 2 additions & 2 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Recipe < ActiveRecord::Base
belongs_to :menu_item
belongs_to :ingredient
belongs_to :menu_item
belongs_to :ingredient
end
9 changes: 8 additions & 1 deletion app/views/menu_items/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
</ul>
</div>
<% end %>
<%= f.text_field :name %>

<%= f.text_field :name, :placeholder => "Item Name" %>
<%= f.text_field :price, :placeholder => "Item Price" %>
<% @menu_item.ingredients.each do |ingredient| %>
<%= f.fields_for "ingredients[]", ingredient do |ingredient_form| %>
<%= ingredient_form.text_field :name %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
Expand Down
18 changes: 18 additions & 0 deletions app/views/menu_items/_ingredient.form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= form_for(@ingredient) do |f| %>
<% if @ingredient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ingredient.errors.count, "error") %> prohibited this ingredient from being saved:</h2>

<ul>
<% @ingredient.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= f.text_field :name, :placeholder => "Ingredient" %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
13 changes: 10 additions & 3 deletions app/views/menu_items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
<table>
<thead>
<tr>
<th></th>
<th></th>
<th>Item Name:</th>
<th>Item Price:</th>
<th>Ingredients:</th>
<th>Show</th>
<th>Edit</th>
<th></th>
</tr>
</thead>

<tbody>
<% @menu_items.each do |menu_item| %>
<tr>
<td><p><%= menu_item.name %></p></td>
<td><p><%= menu_item.name.titleize %></p></td>
<td><p><%= number_to_currency menu_item.price %></p></td>
<td><% @ingredients.each do |ingredient| %>
<li><%= ingredient.name %></li></td>
<% end %>
<td><%= link_to 'Show', menu_item %></td>
<td><%= link_to 'Edit', edit_menu_item_path(menu_item) %></td>
<td><%= link_to 'Destroy', menu_item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Expand Down
7 changes: 7 additions & 0 deletions app/views/menu_items/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<p id="notice"><%= notice %></p>

<h2><strong><%= @menu_item.name.titleize %></strong></h2>
<p>Price:<%= number_to_currency @menu_item.price %></p>
<h4>Ingredients:</h4>
<% @ingredients.each do |ingredient| %>
<li><%= ingredient.name %></li>
<% end %>

<%= link_to 'Edit', edit_menu_item_path(@menu_item) %> |
<%= link_to 'Back', menu_items_path %>
57 changes: 3 additions & 54 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
Makerbistro::Application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

resources :menu_items do
resources :ingredients, only: [:create]
resources :ingredients
end

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
resource :ingredients

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
root "menu_item#index"
end
2 changes: 0 additions & 2 deletions db/migrate/20131010155724_create_menu_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ class CreateMenuItems < ActiveRecord::Migration
def change
create_table :menu_items do |t|
t.string :name
t.string :price

t.timestamps
end
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20131014041455_add_price_to_menu_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPriceToMenuItems < ActiveRecord::Migration
def change
add_column :menu_items, :price, :string
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class CreateRecipes < ActiveRecord::Migration
def change
create_table :recipes do |t|
t.references :menu_item
t.references :ingredient
t.references :menu_item, index: true
t.references :ingredient, index: true

t.timestamps
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CreateIngredients < ActiveRecord::Migration
def change
create_table :ingredients do |t|
t.string :name

t.timestamps
end
end
Expand Down
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131010155915) do
ActiveRecord::Schema.define(version: 20131014042123) do

create_table "ingredients", force: true do |t|
t.string "name"
Expand All @@ -21,9 +21,9 @@

create_table "menu_items", force: true do |t|
t.string "name"
t.string "price"
t.datetime "created_at"
t.datetime "updated_at"
t.string "price"
end

create_table "recipes", force: true do |t|
Expand All @@ -33,4 +33,7 @@
t.datetime "updated_at"
end

add_index "recipes", ["ingredient_id"], name: "index_recipes_on_ingredient_id"
add_index "recipes", ["menu_item_id"], name: "index_recipes_on_menu_item_id"

end
25 changes: 25 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,28 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)


mi1 = MenuItem.create(name: "Pizza", price: "10")
mi2 = MenuItem.create(name: "Hot Dog", price: "15")
mi3 = MenuItem.create(name: "Burger", price: "12")
mi4 = MenuItem.create(name: "Salad", price: "5")

i1 = Ingredient.create(name:"Bread")
i2 = Ingredient.create(name:"Meat")
i3 = Ingredient.create(name:"Lettuce")
i4 = Ingredient.create(name:"Veggies")
i5 = Ingredient.create(name:"Dough")

Recipe.create(menu_item: mi1, ingredient: i5)
Recipe.create(menu_item: mi1, ingredient: i2)


Recipe.create(menu_item: mi2, ingredient: i1)
Recipe.create(menu_item: mi2, ingredient: i2)

Recipe.create(menu_item: mi3, ingredient: i1)
Recipe.create(menu_item: mi3, ingredient: i2)

Recipe.create(menu_item: mi4, ingredient: i3)
Recipe.create(menu_item: mi4, ingredient: i4)
Empty file removed test/controllers/.keep
Empty file.
49 changes: 0 additions & 49 deletions test/controllers/menu_items_controller_test.rb

This file was deleted.

Empty file removed test/fixtures/.keep
Empty file.
Loading