diff --git a/Gemfile b/Gemfile index 24a30f0..153362f 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,10 @@ group :doc do gem 'sdoc', require: false end +gem 'hirb' +gem 'haml' +gem 'simple_form' + # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index f6c82fe..6708381 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -86,6 +89,9 @@ GEM sdoc (0.3.20) json (>= 1.1.3) rdoc (~> 3.10) + simple_form (3.0.0) + actionpack (>= 4.0.0, < 4.1) + activemodel (>= 4.0.0, < 4.1) slop (3.4.6) sprockets (2.10.0) hike (~> 1.2) @@ -116,12 +122,15 @@ PLATFORMS DEPENDENCIES coffee-rails (~> 4.0.0) + haml + hirb jbuilder (~> 1.2) jquery-rails pry rails (= 4.0.0) sass-rails (~> 4.0.0) sdoc + simple_form sqlite3 turbolinks uglifier (>= 1.3.0) diff --git a/app/controllers/menu_items_controller.rb b/app/controllers/menu_items_controller.rb index b124cb4..684f87a 100644 --- a/app/controllers/menu_items_controller.rb +++ b/app/controllers/menu_items_controller.rb @@ -5,11 +5,15 @@ class MenuItemsController < ApplicationController # GET /menu_items.json def index @menu_items = MenuItem.all + @menu_item = MenuItem.new end # GET /menu_items/1 # GET /menu_items/1.json def show + if params[:ingredient] != nil + @menu_item.ingredients.create(ingredient_params) + end end # GET /menu_items/new @@ -71,4 +75,8 @@ def set_menu_item def menu_item_params params.require(:menu_item).permit(:name) end + + def ingredient_params + params[:ingredient].permit(:name) + end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb new file mode 100644 index 0000000..e4ce65f --- /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/menu_item.rb b/app/models/menu_item.rb index 8440100..f93f667 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/app/views/menu_items/index.html.erb b/app/views/menu_items/index.html.erb index b1db6a9..4528933 100644 --- a/app/views/menu_items/index.html.erb +++ b/app/views/menu_items/index.html.erb @@ -1,4 +1,6 @@ -
| <%= 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?' } %> | +
+
|
<%= notice %>
- -<%= link_to 'Edit', edit_menu_item_path(@menu_item) %> | -<%= link_to 'Back', menu_items_path %> diff --git a/app/views/menu_items/show.html.haml b/app/views/menu_items/show.html.haml new file mode 100644 index 0000000..da01a20 --- /dev/null +++ b/app/views/menu_items/show.html.haml @@ -0,0 +1,12 @@ +%h2= @menu_item.name +%ul + -@menu_item.ingredients.all.each do |ingredient| + %li=ingredient.name + +=simple_form_for @menu_item.ingredients.new do |f| + =f.input :name, placeholder: "new ingredient!", :input_html => {:maxlength => 30, :size => 40} + =f.button :submit + += link_to 'Edit', edit_menu_item_path(@menu_item) +| += link_to 'Back', menu_items_path diff --git a/config/routes.rb b/config/routes.rb index 8320abf..7585cd8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Makerbistro::Application.routes.draw do + resources :menu_items + + post "/menu_items/:id" => "menu_items#show", as: "ingredients" # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20131011191357_create_recipes.rb b/db/migrate/20131011191357_create_recipes.rb new file mode 100644 index 0000000..9c8e7ac --- /dev/null +++ b/db/migrate/20131011191357_create_recipes.rb @@ -0,0 +1,9 @@ +class CreateRecipes < ActiveRecord::Migration + def change + create_table :recipes do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20131011191405_create_ingredients.rb b/db/migrate/20131011191405_create_ingredients.rb new file mode 100644 index 0000000..1c22faf --- /dev/null +++ b/db/migrate/20131011191405_create_ingredients.rb @@ -0,0 +1,9 @@ +class CreateIngredients < ActiveRecord::Migration + def change + create_table :ingredients do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20131011191433_add_recipes_ref_menu_item.rb b/db/migrate/20131011191433_add_recipes_ref_menu_item.rb new file mode 100644 index 0000000..041173e --- /dev/null +++ b/db/migrate/20131011191433_add_recipes_ref_menu_item.rb @@ -0,0 +1,6 @@ +class AddRecipesRefMenuItem < ActiveRecord::Migration + def change + add_reference :recipes, :ingredient, index: true + add_reference :recipes, :menu_item, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..8c35c4c --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,39 @@ +# 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: 20131011191433) do + + create_table "ingredients", force: true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "menu_items", force: true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "recipes", force: true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "ingredient_id" + t.integer "menu_item_id" + 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/ingredients.yml b/test/fixtures/ingredients.yml new file mode 100644 index 0000000..0227c60 --- /dev/null +++ b/test/fixtures/ingredients.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/fixtures/recipes.yml b/test/fixtures/recipes.yml new file mode 100644 index 0000000..0227c60 --- /dev/null +++ b/test/fixtures/recipes.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/models/ingredient_test.rb b/test/models/ingredient_test.rb new file mode 100644 index 0000000..511fcf8 --- /dev/null +++ b/test/models/ingredient_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class IngredientTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end 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