diff --git a/Gemfile b/Gemfile index 24a30f0..cdf3cd0 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index f6c82fe..30b7ae0 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) @@ -116,6 +119,8 @@ PLATFORMS DEPENDENCIES coffee-rails (~> 4.0.0) + haml + hirb jbuilder (~> 1.2) jquery-rails pry diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 88e2941..dc07508 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -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 diff --git a/app/controllers/menu_items_controller.rb b/app/controllers/menu_items_controller.rb index b124cb4..698c822 100644 --- a/app/controllers/menu_items_controller.rb +++ b/app/controllers/menu_items_controller.rb @@ -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 diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index f8367ed..33174dc 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -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 diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index 6ba37a8..e618420 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -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 diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 5f5d28d..8092aa1 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -1,4 +1,4 @@ class Recipe < ActiveRecord::Base - belongs_to :menu_item - belongs_to :ingredient + belongs_to :menu_item + belongs_to :ingredient end diff --git a/app/views/menu_items/_form.html.erb b/app/views/menu_items/_form.html.erb index d17a090..e7a2b0b 100644 --- a/app/views/menu_items/_form.html.erb +++ b/app/views/menu_items/_form.html.erb @@ -10,7 +10,14 @@ <% 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 %>
<%= f.submit %>
diff --git a/app/views/menu_items/_ingredient.form.html.erb b/app/views/menu_items/_ingredient.form.html.erb new file mode 100644 index 0000000..70f19f2 --- /dev/null +++ b/app/views/menu_items/_ingredient.form.html.erb @@ -0,0 +1,18 @@ +<%= form_for(@ingredient) do |f| %> + <% if @ingredient.errors.any? %> +
+

<%= pluralize(@ingredient.errors.count, "error") %> prohibited this ingredient from being saved:

+ + +
+ <% end %> + + <%= f.text_field :name, :placeholder => "Ingredient" %> +
+ <%= f.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/menu_items/index.html.erb b/app/views/menu_items/index.html.erb index b1db6a9..8f7a133 100644 --- a/app/views/menu_items/index.html.erb +++ b/app/views/menu_items/index.html.erb @@ -3,8 +3,11 @@ - - + + + + + @@ -12,7 +15,11 @@ <% @menu_items.each do |menu_item| %> - + + + + <% end %> diff --git a/app/views/menu_items/show.html.erb b/app/views/menu_items/show.html.erb index 1a2fd70..18ca523 100644 --- a/app/views/menu_items/show.html.erb +++ b/app/views/menu_items/show.html.erb @@ -1,4 +1,11 @@

<%= notice %>

+

<%= @menu_item.name.titleize %>

+

Price:<%= number_to_currency @menu_item.price %>

+

Ingredients:

+ <% @ingredients.each do |ingredient| %> +
  • <%= ingredient.name %>
  • +<% end %> + <%= 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 f26336e..bd8790d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20131010155724_create_menu_items.rb b/db/migrate/20131010155724_create_menu_items.rb index ae058ad..8278a6c 100644 --- a/db/migrate/20131010155724_create_menu_items.rb +++ b/db/migrate/20131010155724_create_menu_items.rb @@ -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 diff --git a/db/migrate/20131014041455_add_price_to_menu_items.rb b/db/migrate/20131014041455_add_price_to_menu_items.rb new file mode 100644 index 0000000..38d7853 --- /dev/null +++ b/db/migrate/20131014041455_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/migrate/20131010155836_create_recipes.rb b/db/migrate/20131014042052_create_recipes.rb similarity index 59% rename from db/migrate/20131010155836_create_recipes.rb rename to db/migrate/20131014042052_create_recipes.rb index e2ae30c..c843e35 100644 --- a/db/migrate/20131010155836_create_recipes.rb +++ b/db/migrate/20131014042052_create_recipes.rb @@ -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 diff --git a/db/migrate/20131010155915_create_ingredients.rb b/db/migrate/20131014042123_create_ingredients.rb similarity index 95% rename from db/migrate/20131010155915_create_ingredients.rb rename to db/migrate/20131014042123_create_ingredients.rb index 21723bf..1c22faf 100644 --- a/db/migrate/20131010155915_create_ingredients.rb +++ b/db/migrate/20131014042123_create_ingredients.rb @@ -2,7 +2,7 @@ class CreateIngredients < ActiveRecord::Migration def change create_table :ingredients do |t| t.string :name - + t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb index bb072f1..89866d7 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: 20131010155915) do +ActiveRecord::Schema.define(version: 20131014042123) do create_table "ingredients", force: true do |t| t.string "name" @@ -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| @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..fd96b54 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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) \ No newline at end of file diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/controllers/menu_items_controller_test.rb b/test/controllers/menu_items_controller_test.rb deleted file mode 100644 index 4aa8ef9..0000000 --- a/test/controllers/menu_items_controller_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'test_helper' - -class MenuItemsControllerTest < ActionController::TestCase - setup do - @menu_item = menu_items(:one) - end - - test "should get index" do - get :index - assert_response :success - assert_not_nil assigns(:menu_items) - end - - test "should get new" do - get :new - assert_response :success - end - - test "should create menu_item" do - assert_difference('MenuItem.count') do - post :create, menu_item: { } - end - - assert_redirected_to menu_item_path(assigns(:menu_item)) - end - - test "should show menu_item" do - get :show, id: @menu_item - assert_response :success - end - - test "should get edit" do - get :edit, id: @menu_item - assert_response :success - end - - test "should update menu_item" do - patch :update, id: @menu_item, menu_item: { } - assert_redirected_to menu_item_path(assigns(:menu_item)) - end - - test "should destroy menu_item" do - assert_difference('MenuItem.count', -1) do - delete :destroy, id: @menu_item - end - - assert_redirected_to menu_items_path - end -end diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/ingredients.yml b/test/fixtures/ingredients.yml index 0227c60..c63aac0 100644 --- a/test/fixtures/ingredients.yml +++ b/test/fixtures/ingredients.yml @@ -1,7 +1,11 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html -one: - name: MyString - -two: - name: MyString +# 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/fixtures/menu_items.yml b/test/fixtures/menu_items.yml deleted file mode 100644 index 2c8154b..0000000 --- a/test/fixtures/menu_items.yml +++ /dev/null @@ -1,9 +0,0 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html - -one: - name: MyString - price: MyString - -two: - name: MyString - price: MyString diff --git a/test/helpers/.keep b/test/helpers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/helpers/menu_items_helper_test.rb b/test/helpers/menu_items_helper_test.rb deleted file mode 100644 index 8919dfd..0000000 --- a/test/helpers/menu_items_helper_test.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'test_helper' - -class MenuItemsHelperTest < ActionView::TestCase -end diff --git a/test/integration/.keep b/test/integration/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/models/menu_item_test.rb b/test/models/menu_item_test.rb deleted file mode 100644 index 73f476d..0000000 --- a/test/models/menu_item_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class MenuItemTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index bc7e05d..0000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,15 +0,0 @@ -ENV["RAILS_ENV"] ||= "test" -require File.expand_path('../../config/environment', __FILE__) -require 'rails/test_help' - -class ActiveSupport::TestCase - ActiveRecord::Migration.check_pending! - - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - # - # Note: You'll currently still have to declare fixtures explicitly in integration tests - # -- they do not yet inherit this setting - fixtures :all - - # Add more helper methods to be used by all tests here... -end
    Item Name:Item Price:Ingredients:ShowEdit

    <%= menu_item.name %>

    <%= menu_item.name.titleize %>

    <%= number_to_currency menu_item.price %>

    <% @ingredients.each do |ingredient| %> +
  • <%= ingredient.name %>
  • <%= 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?' } %>