From ca35f7bccd6a1b7da54620763f61184a3724303f Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 15:50:57 -0500 Subject: [PATCH 1/9] add routes and run migrations to get /menu_items up and running --- config/routes.rb | 54 +----------------------------------------------- db/schema.rb | 22 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 53 deletions(-) create mode 100644 db/schema.rb diff --git a/config/routes.rb b/config/routes.rb index 8320abf..dd13419 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,56 +1,4 @@ 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' - - # 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 + resources :menu_items - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..8980e15 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,22 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20131010155724) do + + create_table "menu_items", force: true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + +end From 282e5ccf4152a18c0984a2c306fbb9ab3091d29e Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 16:05:25 -0500 Subject: [PATCH 2/9] add migration AddPriceToMenuItems --- db/migrate/20131012205824_add_price_to_menu_items.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20131012205824_add_price_to_menu_items.rb diff --git a/db/migrate/20131012205824_add_price_to_menu_items.rb b/db/migrate/20131012205824_add_price_to_menu_items.rb new file mode 100644 index 0000000..38d7853 --- /dev/null +++ b/db/migrate/20131012205824_add_price_to_menu_items.rb @@ -0,0 +1,5 @@ +class AddPriceToMenuItems < ActiveRecord::Migration + def change + add_column :menu_items, :price, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 8980e15..0428b08 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,12 +11,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20131010155724) do +ActiveRecord::Schema.define(version: 20131012205824) do create_table "menu_items", force: true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" + t.string "price" end end From 9f3ab762a882dfa1d4bee3b9e3101425d002cfeb Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 16:08:48 -0500 Subject: [PATCH 3/9] add db seeds --- db/seeds.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..4b0f194 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,7 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +MenuItem.create({name: "pasta", price: "$10"}) +MenuItem.create({name: "cheesey bread", price: "$5"}) +MenuItem.create({name: "pizza", price: "$15"}) \ No newline at end of file From a559e49a805bd65cd291d596ecb7362a0ca2388c Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 16:10:09 -0500 Subject: [PATCH 4/9] add hirb --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index 24a30f0..d487912 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,8 @@ group :doc do gem 'sdoc', require: false end +gem 'hirb' + # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' From cdee6d4170bb34a515874329beae1cf431c53820 Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 16:15:22 -0500 Subject: [PATCH 5/9] create Ingredients model; add hirb gem --- Gemfile.lock | 2 ++ app/models/ingredients.rb | 2 ++ db/migrate/20131012211224_create_ingredients.rb | 8 ++++++++ db/schema.rb | 8 +++++++- test/fixtures/ingredients.yml | 11 +++++++++++ test/models/ingredients_test.rb | 7 +++++++ 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 app/models/ingredients.rb create mode 100644 db/migrate/20131012211224_create_ingredients.rb create mode 100644 test/fixtures/ingredients.yml create mode 100644 test/models/ingredients_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index f6c82fe..22af6b1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,7 @@ GEM erubis (2.7.0) execjs (2.0.2) hike (1.2.3) + hirb (0.7.1) i18n (0.6.5) jbuilder (1.5.2) activesupport (>= 3.0.0) @@ -116,6 +117,7 @@ PLATFORMS DEPENDENCIES coffee-rails (~> 4.0.0) + hirb jbuilder (~> 1.2) jquery-rails pry diff --git a/app/models/ingredients.rb b/app/models/ingredients.rb new file mode 100644 index 0000000..8db1ed6 --- /dev/null +++ b/app/models/ingredients.rb @@ -0,0 +1,2 @@ +class Ingredients < ActiveRecord::Base +end diff --git a/db/migrate/20131012211224_create_ingredients.rb b/db/migrate/20131012211224_create_ingredients.rb new file mode 100644 index 0000000..b9ba5fb --- /dev/null +++ b/db/migrate/20131012211224_create_ingredients.rb @@ -0,0 +1,8 @@ +class CreateIngredients < ActiveRecord::Migration + def change + create_table :ingredients do |t| + t.string :food_thing + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0428b08..3d83d91 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20131012205824) do +ActiveRecord::Schema.define(version: 20131012211224) do + + create_table "ingredients", force: true do |t| + t.string "food_thing" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "menu_items", force: true do |t| t.string "name" diff --git a/test/fixtures/ingredients.yml b/test/fixtures/ingredients.yml new file mode 100644 index 0000000..c63aac0 --- /dev/null +++ b/test/fixtures/ingredients.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/ingredients_test.rb b/test/models/ingredients_test.rb new file mode 100644 index 0000000..441fe90 --- /dev/null +++ b/test/models/ingredients_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class IngredientsTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 11f9c7e837196bc083e4fdc9749f4c0753cb8f16 Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 16:25:44 -0500 Subject: [PATCH 6/9] add jointable Recipe and add associations --- app/models/ingredients.rb | 2 ++ app/models/menu_item.rb | 2 ++ app/models/recipe.rb | 4 ++++ db/migrate/20131012212122_create_recipes.rb | 10 ++++++++++ db/schema.rb | 12 +++++++++++- test/fixtures/recipes.yml | 9 +++++++++ test/models/recipe_test.rb | 7 +++++++ 7 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app/models/recipe.rb create mode 100644 db/migrate/20131012212122_create_recipes.rb create mode 100644 test/fixtures/recipes.yml create mode 100644 test/models/recipe_test.rb diff --git a/app/models/ingredients.rb b/app/models/ingredients.rb index 8db1ed6..27a4b4f 100644 --- a/app/models/ingredients.rb +++ b/app/models/ingredients.rb @@ -1,2 +1,4 @@ class Ingredients < ActiveRecord::Base + has_many :recipes + has_many :menu_items, through: => :recipes end diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index 8440100..af3b9be 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -1,2 +1,4 @@ class MenuItem < ActiveRecord::Base + has_many :recipes + has_many :ingredients, through: => :recipes end diff --git a/app/models/recipe.rb b/app/models/recipe.rb new file mode 100644 index 0000000..8092aa1 --- /dev/null +++ b/app/models/recipe.rb @@ -0,0 +1,4 @@ +class Recipe < ActiveRecord::Base + belongs_to :menu_item + belongs_to :ingredient +end diff --git a/db/migrate/20131012212122_create_recipes.rb b/db/migrate/20131012212122_create_recipes.rb new file mode 100644 index 0000000..c843e35 --- /dev/null +++ b/db/migrate/20131012212122_create_recipes.rb @@ -0,0 +1,10 @@ +class CreateRecipes < ActiveRecord::Migration + def change + create_table :recipes do |t| + t.references :menu_item, index: true + t.references :ingredient, index: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 3d83d91..e9eb532 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20131012211224) do +ActiveRecord::Schema.define(version: 20131012212122) do create_table "ingredients", force: true do |t| t.string "food_thing" @@ -26,4 +26,14 @@ t.string "price" end + create_table "recipes", force: true do |t| + t.integer "menu_item_id" + t.integer "ingredient_id" + t.datetime "created_at" + 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 diff --git a/test/fixtures/recipes.yml b/test/fixtures/recipes.yml new file mode 100644 index 0000000..7cf14a0 --- /dev/null +++ b/test/fixtures/recipes.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + menu_item_id: + ingredient_id: + +two: + menu_item_id: + ingredient_id: diff --git a/test/models/recipe_test.rb b/test/models/recipe_test.rb new file mode 100644 index 0000000..5f53f19 --- /dev/null +++ b/test/models/recipe_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class RecipeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 77ed47397732108485261000506ae829d33544f6 Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 16:32:33 -0500 Subject: [PATCH 7/9] fix syntax error in associations --- app/models/ingredients.rb | 2 +- app/models/menu_item.rb | 2 +- db/seeds.rb | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/models/ingredients.rb b/app/models/ingredients.rb index 27a4b4f..fb15b44 100644 --- a/app/models/ingredients.rb +++ b/app/models/ingredients.rb @@ -1,4 +1,4 @@ class Ingredients < ActiveRecord::Base has_many :recipes - has_many :menu_items, through: => :recipes + has_many :menu_items, :through => :recipe end diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index af3b9be..f0a7b16 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -1,4 +1,4 @@ class MenuItem < ActiveRecord::Base has_many :recipes - has_many :ingredients, through: => :recipes + has_many :ingredients, :through => :recipe end diff --git a/db/seeds.rb b/db/seeds.rb index 4b0f194..eff979a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -8,4 +8,11 @@ MenuItem.create({name: "pasta", price: "$10"}) MenuItem.create({name: "cheesey bread", price: "$5"}) -MenuItem.create({name: "pizza", price: "$15"}) \ No newline at end of file +MenuItem.create({name: "pizza", price: "$15"}) + +Ingredient.create({food_thing: "dough"}) +Ingredient.create({food_thing: "sauce"}) +Ingredient.create({food_thing: "cheese"}) +Ingredient.create({food_thing: "noodles"}) +Ingredient.create({food_thing: "meat"}) +Ingredient.create({food_thing: "garlic"}) \ No newline at end of file From 87bc9e33f7cae28bfdd620cc66274380931cfca1 Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sat, 12 Oct 2013 19:17:39 -0500 Subject: [PATCH 8/9] fix associations and create better seeds --- app/models/ingredient.rb | 4 ++++ app/models/ingredients.rb | 4 ---- app/models/menu_item.rb | 2 +- db/seeds.rb | 27 ++++++++++++++++++--------- 4 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 app/models/ingredient.rb delete mode 100644 app/models/ingredients.rb diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb new file mode 100644 index 0000000..70445cb --- /dev/null +++ b/app/models/ingredient.rb @@ -0,0 +1,4 @@ +class Ingredient < ActiveRecord::Base + has_many :recipes + has_many :menu_items, :through => :recipes +end diff --git a/app/models/ingredients.rb b/app/models/ingredients.rb deleted file mode 100644 index fb15b44..0000000 --- a/app/models/ingredients.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Ingredients < ActiveRecord::Base - has_many :recipes - has_many :menu_items, :through => :recipe -end diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index f0a7b16..37c3026 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -1,4 +1,4 @@ class MenuItem < ActiveRecord::Base has_many :recipes - has_many :ingredients, :through => :recipe + has_many :ingredients, :through => :recipes end diff --git a/db/seeds.rb b/db/seeds.rb index eff979a..cc46f86 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,13 +6,22 @@ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) -MenuItem.create({name: "pasta", price: "$10"}) -MenuItem.create({name: "cheesey bread", price: "$5"}) -MenuItem.create({name: "pizza", price: "$15"}) +pasta = MenuItem.create(name: "pasta", price: "$10") -Ingredient.create({food_thing: "dough"}) -Ingredient.create({food_thing: "sauce"}) -Ingredient.create({food_thing: "cheese"}) -Ingredient.create({food_thing: "noodles"}) -Ingredient.create({food_thing: "meat"}) -Ingredient.create({food_thing: "garlic"}) \ No newline at end of file +# .ingredients.create([food_thing: "noodles", food_thing: "sauce", food_thing: "garlic"]) +cheesebread = MenuItem.create(name: "cheesey bread", price: "$5") +pizza = MenuItem.create(name: "pizza", price: "$15") + +dough = Ingredient.create(food_thing: "dough") +sauce = Ingredient.create(food_thing: "sauce") +cheese = Ingredient.create(food_thing: "cheese") +noodles = Ingredient.create(food_thing: "noodles") +meat = Ingredient.create(food_thing: "meat") +garlic = Ingredient.create(food_thing: "garlic") + +spaghetti = pasta.recipes.create(ingredient: noodles) +spaghetti = pasta.recipes.create(ingredient: sauce) +spaghetti = pasta.recipes.create(ingredient: garlic) +# testfood = MenuItem.first.recipes.create +# testfood2 = MenuItem.last.recipes.create ingredient_id: 1 +# pasta.recipes.create(ingredient: noodles) \ No newline at end of file From 880dc82a38c4c72dffbde651593c9cf13c66bc9a Mon Sep 17 00:00:00 2001 From: nicole pank Date: Sun, 13 Oct 2013 15:59:18 -0500 Subject: [PATCH 9/9] add price to index and add add info to show.html --- app/views/menu_items/index.html.erb | 1 + app/views/menu_items/show.html.erb | 3 +++ 2 files changed, 4 insertions(+) diff --git a/app/views/menu_items/index.html.erb b/app/views/menu_items/index.html.erb index b1db6a9..c451c1c 100644 --- a/app/views/menu_items/index.html.erb +++ b/app/views/menu_items/index.html.erb @@ -13,6 +13,7 @@ <% @menu_items.each do |menu_item| %>

<%= menu_item.name %>

+

<%= menu_item.price %>

<%= link_to 'Show', menu_item %> <%= link_to 'Edit', edit_menu_item_path(menu_item) %> <%= link_to 'Destroy', menu_item, method: :delete, data: { confirm: 'Are you sure?' } %> diff --git a/app/views/menu_items/show.html.erb b/app/views/menu_items/show.html.erb index 1a2fd70..455fc8a 100644 --- a/app/views/menu_items/show.html.erb +++ b/app/views/menu_items/show.html.erb @@ -1,4 +1,7 @@

<%= notice %>

+

<%= @menu_item.name %>

+

<%= @menu_item.price %>

+ <%= link_to 'Edit', edit_menu_item_path(@menu_item) %> | <%= link_to 'Back', menu_items_path %>