diff --git a/Gemfile b/Gemfile
index 4a47f3d..c5301f9 100644
--- a/Gemfile
+++ b/Gemfile
@@ -6,6 +6,8 @@ gem 'pg', '~> 0.18.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
+gem 'puma'
+
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
@@ -27,6 +29,7 @@ group :development, :test do
gem 'guard', '~> 2.14.0'
gem 'guard-rspec', require: false
gem 'rspec-rails', '~> 3.5.0'
+ gem 'rails-controller-testing'
gem 'factory_girl_rails', '~> 4.7.0'
gem 'database_cleaner', '~> 1.5.3'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
diff --git a/Gemfile.lock b/Gemfile.lock
index a9d7abd..3c1d53c 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -126,6 +126,7 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
+ puma (3.6.0)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
@@ -141,6 +142,10 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0)
sprockets-rails (>= 2.0.0)
+ rails-controller-testing (1.0.1)
+ actionpack (~> 5.x)
+ actionview (~> 5.x)
+ activesupport (~> 5.x)
rails-dom-testing (2.0.1)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6.0)
@@ -240,7 +245,9 @@ DEPENDENCIES
jquery-rails (~> 4.1.1)
kaminari (~> 0.17.0)
pg (~> 0.18.4)
+ puma
rails (= 5.0.0)
+ rails-controller-testing
rspec-rails (~> 3.5.0)
sass-rails (~> 5.0)
sqlite3
diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb
new file mode 100644
index 0000000..cc90341
--- /dev/null
+++ b/app/controllers/notes_controller.rb
@@ -0,0 +1,82 @@
+class NotesController < ApplicationController
+ before_action :set_note, only: [:show, :edit, :update, :destroy]
+ before_action :ensure_valid_user
+
+ # GET /notes
+ # GET /notes.json
+ def index
+ @notes = Note.for_user(current_user)
+ end
+
+ # GET /notes/1
+ # GET /notes/1.json
+ def show
+ ensure_owner(@note)
+ end
+
+ # GET /notes/new
+ def new
+ @note = Note.new
+ end
+
+ # GET /notes/1/edit
+ def edit
+ ensure_owner(@note)
+ end
+
+ # POST /notes
+ # POST /notes.json
+ def create
+ @note = Note.new(note_params)
+ @note.user = current_user
+
+ respond_to do |format|
+ if @note.save
+ format.html { redirect_to notes_path, notice: 'Note was successfully created.' }
+ format.json { render :show, status: :created, location: @note }
+ else
+ format.html { render :new }
+ format.json { render json: @note.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PATCH/PUT /notes/1
+ # PATCH/PUT /notes/1.json
+ def update
+ ensure_owner(@note) do
+ respond_to do |format|
+ if @note.update(note_params)
+ format.html { redirect_to notes_path, notice: 'Note was successfully updated.' }
+ format.json { render :show, status: :ok, location: @note }
+ else
+ format.html { render :edit }
+ format.json { render json: @note.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+ end
+
+ # DELETE /notes/1
+ # DELETE /notes/1.json
+ def destroy
+ ensure_owner(@note) do
+ @note.destroy
+ respond_to do |format|
+ format.html { redirect_to notes_url, notice: 'Note was successfully destroyed.' }
+ format.json { head :no_content }
+ end
+ end
+ end
+
+ private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_note
+ @note = Note.find(params[:id])
+ end
+
+ # Never trust parameters from the scary internet, only allow the white list through.
+ def note_params
+ params.require(:note).permit(:user_id, :content)
+ end
+end
diff --git a/app/decorators/note_decorator.rb b/app/decorators/note_decorator.rb
new file mode 100644
index 0000000..9283e9f
--- /dev/null
+++ b/app/decorators/note_decorator.rb
@@ -0,0 +1,12 @@
+class NoteDecorator < BaseDecorator
+
+ def date
+ v = super
+ if v && v.respond_to?(:strftime)
+ v.strftime("%Y-%m-%d")
+ else
+ v
+ end
+ end
+
+end
\ No newline at end of file
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index aadfaaa..373df8b 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -26,6 +26,10 @@ module ApplicationHelper
nav_item('About', about_path, 'home')
]
+ if current_user
+ nav << nav_item('Notes', notes_path, 'notes')
+ end
+
if current_user && current_user.admin?
nav << nav_item('Admin', admin_users_path, 'admin/users')
end
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
new file mode 100644
index 0000000..10a4cba
--- /dev/null
+++ b/app/models/application_record.rb
@@ -0,0 +1,3 @@
+class ApplicationRecord < ActiveRecord::Base
+ self.abstract_class = true
+end
diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb
index 217ec9c..e22b520 100644
--- a/app/models/ingredient.rb
+++ b/app/models/ingredient.rb
@@ -1,4 +1,4 @@
-class Ingredient < ActiveRecord::Base
+class Ingredient < ApplicationRecord
include TokenizedLike
belongs_to :user
diff --git a/app/models/ingredient_unit.rb b/app/models/ingredient_unit.rb
index 0a92278..6691667 100644
--- a/app/models/ingredient_unit.rb
+++ b/app/models/ingredient_unit.rb
@@ -1,4 +1,4 @@
-class IngredientUnit < ActiveRecord::Base
+class IngredientUnit < ApplicationRecord
belongs_to :ingredient, inverse_of: :ingredient_units
validates :name, presence: true
diff --git a/app/models/log.rb b/app/models/log.rb
index 938c291..21adfdf 100644
--- a/app/models/log.rb
+++ b/app/models/log.rb
@@ -1,4 +1,4 @@
-class Log < ActiveRecord::Base
+class Log < ApplicationRecord
belongs_to :recipe
belongs_to :source_recipe, class_name: 'Recipe'
diff --git a/app/models/note.rb b/app/models/note.rb
new file mode 100644
index 0000000..4cc2eeb
--- /dev/null
+++ b/app/models/note.rb
@@ -0,0 +1,10 @@
+class Note < ApplicationRecord
+
+ belongs_to :user
+
+ scope :for_user, ->(user) { where(user_id: user) }
+
+ validates :user_id, presence: true
+ validates :content, presence: true
+
+end
diff --git a/app/models/recipe.rb b/app/models/recipe.rb
index 2409903..be39818 100644
--- a/app/models/recipe.rb
+++ b/app/models/recipe.rb
@@ -1,4 +1,4 @@
-class Recipe < ActiveRecord::Base
+class Recipe < ApplicationRecord
has_many :recipe_ingredients, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
diff --git a/app/models/recipe_ingredient.rb b/app/models/recipe_ingredient.rb
index 8397dfa..4b3731b 100644
--- a/app/models/recipe_ingredient.rb
+++ b/app/models/recipe_ingredient.rb
@@ -1,4 +1,4 @@
-class RecipeIngredient < ActiveRecord::Base
+class RecipeIngredient < ApplicationRecord
belongs_to :ingredient
belongs_to :recipe, inverse_of: :recipe_ingredients
diff --git a/app/models/recipe_step.rb b/app/models/recipe_step.rb
index 30064ed..3b4b937 100644
--- a/app/models/recipe_step.rb
+++ b/app/models/recipe_step.rb
@@ -1,4 +1,4 @@
-class RecipeStep < ActiveRecord::Base
+class RecipeStep < ApplicationRecord
belongs_to :recipe, inverse_of: :recipe_steps
diff --git a/app/models/usda_food.rb b/app/models/usda_food.rb
index 5bfb420..8e24f9b 100644
--- a/app/models/usda_food.rb
+++ b/app/models/usda_food.rb
@@ -1,4 +1,4 @@
-class UsdaFood < ActiveRecord::Base
+class UsdaFood < ApplicationRecord
include TokenizedLike
has_many :usda_food_weights
diff --git a/app/models/usda_food_weight.rb b/app/models/usda_food_weight.rb
index 8ef4b0a..63bacc7 100644
--- a/app/models/usda_food_weight.rb
+++ b/app/models/usda_food_weight.rb
@@ -1,4 +1,4 @@
-class UsdaFoodWeight < ActiveRecord::Base
+class UsdaFoodWeight < ApplicationRecord
belongs_to :usda_food
diff --git a/app/models/user.rb b/app/models/user.rb
index 7544b5d..ad586c1 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,4 +1,4 @@
-class User < ActiveRecord::Base
+class User < ApplicationRecord
has_many :recipes, dependent: :nullify
has_many :ingredients, dependent: :nullify
diff --git a/app/views/notes/_form.html.erb b/app/views/notes/_form.html.erb
new file mode 100644
index 0000000..fbb7551
--- /dev/null
+++ b/app/views/notes/_form.html.erb
@@ -0,0 +1,13 @@
+<%= form_for(note) do |f| %>
+ <%= render partial: 'shared/error_list', locals: {model: @note} %>
+
+
+ <%= f.label :content, 'Note', class: 'control-label' %>
+ <%= f.text_field :content, class: 'form-control' %>
+
+
+
+ <%= f.submit class: 'btn btn-primary' %>
+ <%= link_to 'Back', notes_path, class: 'btn btn-default' %>
+
+<% end %>
diff --git a/app/views/notes/_note.json.jbuilder b/app/views/notes/_note.json.jbuilder
new file mode 100644
index 0000000..d5842d6
--- /dev/null
+++ b/app/views/notes/_note.json.jbuilder
@@ -0,0 +1,2 @@
+json.extract! note, :id, :content, :created_at
+json.url note_url(note, format: :json)
\ No newline at end of file
diff --git a/app/views/notes/edit.html.erb b/app/views/notes/edit.html.erb
new file mode 100644
index 0000000..e20934a
--- /dev/null
+++ b/app/views/notes/edit.html.erb
@@ -0,0 +1,11 @@
+
+
+
+
+ <%= render 'form', note: @note %>
+
+
+
+
diff --git a/app/views/notes/index.html.erb b/app/views/notes/index.html.erb
new file mode 100644
index 0000000..12f72a4
--- /dev/null
+++ b/app/views/notes/index.html.erb
@@ -0,0 +1,47 @@
+
+
+
+
+ <% if @notes.empty? %>
+
No Notes
+ <% else %>
+
+ <%= link_to 'New Note', new_note_path, class: 'btn btn-default' %>
+
+
+
+
+
+
+ Note |
+ Date |
+ |
+
+
+
+
+ <% decorate(@notes, NoteDecorator).each do |note| %>
+
+ <%= note.content %> |
+ <%= note.created_at %> |
+
+ <%= link_to note, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-xs btn-danger' do %>
+
+ <% end %>
+ |
+
+ <% end %>
+
+
+ <% end %>
+
+
+
+ <%= link_to 'New Note', new_note_path, class: 'btn btn-default' %>
+
+
+
+
+
diff --git a/app/views/notes/index.json.jbuilder b/app/views/notes/index.json.jbuilder
new file mode 100644
index 0000000..c121fb7
--- /dev/null
+++ b/app/views/notes/index.json.jbuilder
@@ -0,0 +1 @@
+json.array! @notes, partial: 'notes/note', as: :note
\ No newline at end of file
diff --git a/app/views/notes/new.html.erb b/app/views/notes/new.html.erb
new file mode 100644
index 0000000..ee95407
--- /dev/null
+++ b/app/views/notes/new.html.erb
@@ -0,0 +1,11 @@
+
+
+
+
+ <%= render 'form', note: @note %>
+
+
+
+
diff --git a/app/views/notes/show.html.erb b/app/views/notes/show.html.erb
new file mode 100644
index 0000000..b711b58
--- /dev/null
+++ b/app/views/notes/show.html.erb
@@ -0,0 +1,15 @@
+
+
+
+
+
+ <%= @note.content %>
+
+
+ <%= link_to 'Back', notes_path, class: 'btn btn-default' %>
+
+
+
+
diff --git a/app/views/notes/show.json.jbuilder b/app/views/notes/show.json.jbuilder
new file mode 100644
index 0000000..800c347
--- /dev/null
+++ b/app/views/notes/show.json.jbuilder
@@ -0,0 +1 @@
+json.partial! "notes/note", note: @note
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index 8495a40..2747b49 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
+ resources :notes
resources :recipes do
resources :logs, only: [:new, :create]
end
diff --git a/db/migrate/20160112214203_create_ingredients.rb b/db/migrate/20160112214203_create_ingredients.rb
index f4de0c9..ba57bc7 100644
--- a/db/migrate/20160112214203_create_ingredients.rb
+++ b/db/migrate/20160112214203_create_ingredients.rb
@@ -1,4 +1,4 @@
-class CreateIngredients < ActiveRecord::Migration
+class CreateIngredients < ActiveRecord::Migration[4.2]
def change
create_table :ingredients do |t|
diff --git a/db/migrate/20160112214213_create_recipes.rb b/db/migrate/20160112214213_create_recipes.rb
index cea8b27..c92665f 100644
--- a/db/migrate/20160112214213_create_recipes.rb
+++ b/db/migrate/20160112214213_create_recipes.rb
@@ -1,4 +1,4 @@
-class CreateRecipes < ActiveRecord::Migration
+class CreateRecipes < ActiveRecord::Migration[4.2]
def change
create_table :recipes do |t|
diff --git a/db/migrate/20160112225805_create_recipe_ingredients.rb b/db/migrate/20160112225805_create_recipe_ingredients.rb
index 7e7cb95..b7e66bf 100644
--- a/db/migrate/20160112225805_create_recipe_ingredients.rb
+++ b/db/migrate/20160112225805_create_recipe_ingredients.rb
@@ -1,4 +1,4 @@
-class CreateRecipeIngredients < ActiveRecord::Migration
+class CreateRecipeIngredients < ActiveRecord::Migration[4.2]
def change
create_table :recipe_ingredients do |t|
diff --git a/db/migrate/20160113002615_create_recipe_steps.rb b/db/migrate/20160113002615_create_recipe_steps.rb
index 8d22e73..46cdd95 100644
--- a/db/migrate/20160113002615_create_recipe_steps.rb
+++ b/db/migrate/20160113002615_create_recipe_steps.rb
@@ -1,4 +1,4 @@
-class CreateRecipeSteps < ActiveRecord::Migration
+class CreateRecipeSteps < ActiveRecord::Migration[4.2]
def change
create_table :recipe_steps do |t|
diff --git a/db/migrate/20160119012704_add_deleted_to_recipes.rb b/db/migrate/20160119012704_add_deleted_to_recipes.rb
index 5243099..2ce5460 100644
--- a/db/migrate/20160119012704_add_deleted_to_recipes.rb
+++ b/db/migrate/20160119012704_add_deleted_to_recipes.rb
@@ -1,4 +1,4 @@
-class AddDeletedToRecipes < ActiveRecord::Migration
+class AddDeletedToRecipes < ActiveRecord::Migration[4.2]
def change
add_column :recipes, :deleted, :boolean
end
diff --git a/db/migrate/20160119212055_create_users.rb b/db/migrate/20160119212055_create_users.rb
index 4294cbf..b91af38 100644
--- a/db/migrate/20160119212055_create_users.rb
+++ b/db/migrate/20160119212055_create_users.rb
@@ -1,4 +1,4 @@
-class CreateUsers < ActiveRecord::Migration
+class CreateUsers < ActiveRecord::Migration[4.2]
def change
create_table :users do |t|
t.string :username
diff --git a/db/migrate/20160124212254_create_usda_food.rb b/db/migrate/20160124212254_create_usda_food.rb
index c8fc3fa..566cd7c 100644
--- a/db/migrate/20160124212254_create_usda_food.rb
+++ b/db/migrate/20160124212254_create_usda_food.rb
@@ -1,4 +1,4 @@
-class CreateUsdaFood < ActiveRecord::Migration
+class CreateUsdaFood < ActiveRecord::Migration[4.2]
def change
create_table :usda_foods do |t|
diff --git a/db/migrate/20160124222409_add_ndbn_to_ingredients.rb b/db/migrate/20160124222409_add_ndbn_to_ingredients.rb
index 135584a..4347d7c 100644
--- a/db/migrate/20160124222409_add_ndbn_to_ingredients.rb
+++ b/db/migrate/20160124222409_add_ndbn_to_ingredients.rb
@@ -1,4 +1,4 @@
-class AddNdbnToIngredients < ActiveRecord::Migration
+class AddNdbnToIngredients < ActiveRecord::Migration[4.2]
def change
change_table :ingredients do |t|
t.string :ndbn, limit: 5, index: true
diff --git a/db/migrate/20160124231837_add_fields_to_ingredient.rb b/db/migrate/20160124231837_add_fields_to_ingredient.rb
index afd63ee..89f44b2 100644
--- a/db/migrate/20160124231837_add_fields_to_ingredient.rb
+++ b/db/migrate/20160124231837_add_fields_to_ingredient.rb
@@ -1,4 +1,4 @@
-class AddFieldsToIngredient < ActiveRecord::Migration
+class AddFieldsToIngredient < ActiveRecord::Migration[4.2]
def change
change_table :ingredients do |t|
t.decimal :water, precision: 10, scale: 2
diff --git a/db/migrate/20160130231838_change_ingredients.rb b/db/migrate/20160130231838_change_ingredients.rb
index 00aa4b4..584afca 100644
--- a/db/migrate/20160130231838_change_ingredients.rb
+++ b/db/migrate/20160130231838_change_ingredients.rb
@@ -1,4 +1,4 @@
-class ChangeIngredients < ActiveRecord::Migration
+class ChangeIngredients < ActiveRecord::Migration[4.2]
def change
change_table :recipe_ingredients do |t|
t.remove :custom_density
diff --git a/db/migrate/20160303180854_add_user_id_to_ingredient.rb b/db/migrate/20160303180854_add_user_id_to_ingredient.rb
index 34c775f..29d1f5f 100644
--- a/db/migrate/20160303180854_add_user_id_to_ingredient.rb
+++ b/db/migrate/20160303180854_add_user_id_to_ingredient.rb
@@ -1,4 +1,4 @@
-class AddUserIdToIngredient < ActiveRecord::Migration
+class AddUserIdToIngredient < ActiveRecord::Migration[4.2]
def change
add_column :ingredients, :user_id, :integer
end
diff --git a/db/migrate/20160309182253_create_usda_food_weights.rb b/db/migrate/20160309182253_create_usda_food_weights.rb
index f069a1d..38f7a3b 100644
--- a/db/migrate/20160309182253_create_usda_food_weights.rb
+++ b/db/migrate/20160309182253_create_usda_food_weights.rb
@@ -1,4 +1,4 @@
-class CreateUsdaFoodWeights < ActiveRecord::Migration
+class CreateUsdaFoodWeights < ActiveRecord::Migration[4.2]
def change
create_table :usda_food_weights do |t|
t.integer :usda_food_id, index: true, null: false
diff --git a/db/migrate/20160311000203_change_amount_column.rb b/db/migrate/20160311000203_change_amount_column.rb
index 605f694..8ebdf0b 100644
--- a/db/migrate/20160311000203_change_amount_column.rb
+++ b/db/migrate/20160311000203_change_amount_column.rb
@@ -1,4 +1,4 @@
-class ChangeAmountColumn < ActiveRecord::Migration
+class ChangeAmountColumn < ActiveRecord::Migration[4.2]
def change
change_column :usda_food_weights, :amount, :decimal, precision: 7, scale: 3
end
diff --git a/db/migrate/20160403205734_change_yields_col.rb b/db/migrate/20160403205734_change_yields_col.rb
index 58809fe..e5ba1d7 100644
--- a/db/migrate/20160403205734_change_yields_col.rb
+++ b/db/migrate/20160403205734_change_yields_col.rb
@@ -1,4 +1,4 @@
-class ChangeYieldsCol < ActiveRecord::Migration
+class ChangeYieldsCol < ActiveRecord::Migration[4.2]
def change
change_column :recipes, :yields, :string
end
diff --git a/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb b/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb
index 812513d..00c30c9 100644
--- a/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb
+++ b/db/migrate/20160622171944_add_nutrients_to_usda_foods.rb
@@ -1,4 +1,4 @@
-class AddNutrientsToUsdaFoods < ActiveRecord::Migration
+class AddNutrientsToUsdaFoods < ActiveRecord::Migration[4.2]
def change
change_table 'usda_foods' do |t|
t.integer :calcium
diff --git a/db/migrate/20160622180838_add_nutrients_to_ingredients.rb b/db/migrate/20160622180838_add_nutrients_to_ingredients.rb
index 79595f2..ad2bdb1 100644
--- a/db/migrate/20160622180838_add_nutrients_to_ingredients.rb
+++ b/db/migrate/20160622180838_add_nutrients_to_ingredients.rb
@@ -1,4 +1,4 @@
-class AddNutrientsToIngredients < ActiveRecord::Migration
+class AddNutrientsToIngredients < ActiveRecord::Migration[4.2]
def change
change_table 'ingredients' do |t|
t.integer :calcium
diff --git a/db/migrate/20160705181132_add_ingredient_units.rb b/db/migrate/20160705181132_add_ingredient_units.rb
index 43a66b6..6d991bb 100644
--- a/db/migrate/20160705181132_add_ingredient_units.rb
+++ b/db/migrate/20160705181132_add_ingredient_units.rb
@@ -1,4 +1,4 @@
-class AddIngredientUnits < ActiveRecord::Migration
+class AddIngredientUnits < ActiveRecord::Migration[4.2]
def change
create_table :ingredient_units do |t|
t.integer :ingredient_id, null: false, index: true
diff --git a/db/migrate/20160707011314_create_logs.rb b/db/migrate/20160707011314_create_logs.rb
index a789ef3..bc56a7b 100644
--- a/db/migrate/20160707011314_create_logs.rb
+++ b/db/migrate/20160707011314_create_logs.rb
@@ -1,4 +1,4 @@
-class CreateLogs < ActiveRecord::Migration
+class CreateLogs < ActiveRecord::Migration[4.2]
def change
create_table :logs do |t|
t.integer :user_id
diff --git a/db/migrate/20161014161544_create_notes.rb b/db/migrate/20161014161544_create_notes.rb
new file mode 100644
index 0000000..8fd2390
--- /dev/null
+++ b/db/migrate/20161014161544_create_notes.rb
@@ -0,0 +1,10 @@
+class CreateNotes < ActiveRecord::Migration[5.0]
+ def change
+ create_table :notes do |t|
+ t.integer :user_id, null: false, index: true
+ t.text :content, null: false
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2c274ce..c577f83 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160928212209) do
+ActiveRecord::Schema.define(version: 20161014161544) do
create_table "ingredient_units", force: :cascade do |t|
t.integer "ingredient_id", null: false
@@ -65,6 +65,14 @@ ActiveRecord::Schema.define(version: 20160928212209) do
t.text "notes"
end
+ create_table "notes", force: :cascade do |t|
+ t.integer "user_id", null: false
+ t.text "content", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["user_id"], name: "index_notes_on_user_id"
+ end
+
create_table "recipe_ingredients", force: :cascade do |t|
t.integer "ingredient_id"
t.integer "recipe_id"
diff --git a/spec/controllers/notes_controller_spec.rb b/spec/controllers/notes_controller_spec.rb
new file mode 100644
index 0000000..439ab84
--- /dev/null
+++ b/spec/controllers/notes_controller_spec.rb
@@ -0,0 +1,154 @@
+require 'rails_helper'
+
+RSpec.describe NotesController, type: :controller do
+
+ render_views
+
+ let(:user) {
+ create(:user)
+ }
+
+ let(:valid_attributes) {
+ {content: 'text'}
+ }
+
+ let(:invalid_attributes) {
+ {content: ''}
+ }
+
+ let(:valid_session) { {user_id: user.id} }
+
+ describe "GET #index" do
+ it "assigns all user notes as @notes" do
+ note = create(:note, user: user)
+ create(:note)
+ get :index, params: {}, session: valid_session
+ expect(assigns(:notes)).to eq([note])
+ end
+ end
+
+ describe "GET #show" do
+ it "assigns the requested note as @note" do
+ note = create(:note, user: user)
+ get :show, params: {id: note.to_param}, session: valid_session
+ expect(assigns(:note)).to eq(note)
+ end
+ end
+
+ describe "GET #new" do
+ it "assigns a new note as @note" do
+ get :new, params: {}, session: valid_session
+ expect(assigns(:note)).to be_a_new(Note)
+ end
+ end
+
+ describe "GET #edit" do
+ it "assigns the requested note as @note" do
+ note = create(:note, user: user)
+ get :edit, params: {id: note.to_param}, session: valid_session
+ expect(assigns(:note)).to eq(note)
+ end
+
+ it 'redirects if note is not owned' do
+ note = create(:note)
+ get :edit, params: {id: note.to_param}, session: valid_session
+ expect(response).to redirect_to(root_path)
+ end
+ end
+
+ describe "POST #create" do
+ context "with valid params" do
+ it "creates a new Note" do
+ expect {
+ post :create, params: {note: valid_attributes}, session: valid_session
+ }.to change(Note, :count).by(1)
+ end
+
+ it "assigns a newly created note as @note" do
+ post :create, params: {note: valid_attributes}, session: valid_session
+ expect(assigns(:note)).to be_a(Note)
+ expect(assigns(:note)).to be_persisted
+ end
+
+ it "redirects to the created note" do
+ post :create, params: {note: valid_attributes}, session: valid_session
+ expect(response).to redirect_to(Note.last)
+ end
+ end
+
+ context "with invalid params" do
+ it "assigns a newly created but unsaved note as @note" do
+ post :create, params: {note: invalid_attributes}, session: valid_session
+ expect(assigns(:note)).to be_a_new(Note)
+ end
+
+ it "re-renders the 'new' template" do
+ post :create, params: {note: invalid_attributes}, session: valid_session
+ expect(response).to render_template("new")
+ end
+ end
+ end
+
+ describe "PUT #update" do
+ context "with valid params" do
+ let(:new_attributes) {
+ {content: 'new stuff'}
+ }
+
+ it "updates the requested note" do
+ note = create(:note, user: user)
+ put :update, params: {id: note.to_param, note: new_attributes}, session: valid_session
+ note.reload
+ expect(note.content).to eq 'new stuff'
+ end
+
+ it "assigns the requested note as @note" do
+ note = create(:note, user: user)
+ put :update, params: {id: note.to_param, note: valid_attributes}, session: valid_session
+ expect(assigns(:note)).to eq(note)
+ end
+
+ it "redirects to the note" do
+ note = create(:note, user: user)
+ put :update, params: {id: note.to_param, note: valid_attributes}, session: valid_session
+ expect(response).to redirect_to(note)
+ end
+
+ it 'redirects if note is not owned' do
+ note = create(:note)
+ put :update, params: {id: note.to_param, note: valid_attributes}, session: valid_session
+ expect(response).to redirect_to(root_path)
+ end
+ end
+
+ context "with invalid params" do
+ it "assigns the note as @note" do
+ note = create(:note, user: user)
+ put :update, params: {id: note.to_param, note: invalid_attributes}, session: valid_session
+ expect(assigns(:note)).to eq(note)
+ end
+
+ it "re-renders the 'edit' template" do
+ note = create(:note, user: user)
+ put :update, params: {id: note.to_param, note: invalid_attributes}, session: valid_session
+ expect(response).to render_template("edit")
+ end
+ end
+ end
+
+ describe "DELETE #destroy" do
+ it "destroys the requested note" do
+ note = create(:note, user: user)
+ expect {
+ delete :destroy, params: {id: note.to_param}, session: valid_session
+ }.to change(Note, :count).by(-1)
+ end
+
+ it "redirects to the notes list" do
+ note = create(:note, user: user)
+ delete :destroy, params: {id: note.to_param}, session: valid_session
+ expect(response).to redirect_to(notes_url)
+ end
+ end
+
+end
diff --git a/spec/factories/notes.rb b/spec/factories/notes.rb
new file mode 100644
index 0000000..f771187
--- /dev/null
+++ b/spec/factories/notes.rb
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :note do
+ user
+ content "MyText"
+ end
+end
diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb
new file mode 100644
index 0000000..d37fb85
--- /dev/null
+++ b/spec/models/note_spec.rb
@@ -0,0 +1,4 @@
+require 'rails_helper'
+
+RSpec.describe Note, type: :model do
+end