backend
This commit is contained in:
parent
a2f2a05679
commit
8992a4a082
46
app/controllers/task_items_controller.rb
Normal file
46
app/controllers/task_items_controller.rb
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
class TaskItemsController < ApplicationController
|
||||||
|
|
||||||
|
before_action :ensure_valid_user
|
||||||
|
before_action :set_task_list
|
||||||
|
before_action :set_task_item, only: [:update, :destroy]
|
||||||
|
|
||||||
|
def create
|
||||||
|
@task_item = TaskItem.new(task_item_params)
|
||||||
|
@task_item.task_list = @task_list
|
||||||
|
|
||||||
|
if @task_item.save
|
||||||
|
render :show, status: :created, location: @task_item
|
||||||
|
else
|
||||||
|
render json: @task_item.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
if @task_item.update(task_item_params)
|
||||||
|
render :show, status: :ok, location: @task_item
|
||||||
|
else
|
||||||
|
render json: @task_item.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@task_item.destroy
|
||||||
|
head :no_content
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def task_item_params
|
||||||
|
params.require(:task_item).permit(:name, :quantity)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_task_list
|
||||||
|
@task_list = TaskList.find(params[:task_list_id])
|
||||||
|
ensure_owner(@task_list)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_task_item
|
||||||
|
@task_item = @task_list.task_items.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
52
app/controllers/task_lists_controller.rb
Normal file
52
app/controllers/task_lists_controller.rb
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
class TaskListsController < ApplicationController
|
||||||
|
|
||||||
|
before_action :ensure_valid_user
|
||||||
|
before_action :set_task_list, only: [:show, :update, :destroy]
|
||||||
|
|
||||||
|
def index
|
||||||
|
@task_lists = TaskList.for_user(current_user)
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
ensure_owner(@task_list)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@task_list = TaskList.new(task_list_params)
|
||||||
|
@task_list.user = current_user
|
||||||
|
|
||||||
|
if @task_list.save
|
||||||
|
render :show, status: :created, location: @task_list
|
||||||
|
else
|
||||||
|
render json: @task_list.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
ensure_owner(@task_list) do
|
||||||
|
if @task_list.update(task_list_params)
|
||||||
|
render :show, status: :ok, location: @task_list
|
||||||
|
else
|
||||||
|
render json: @task_list.errors, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
ensure_owner(@task_list) do
|
||||||
|
@task_list.destroy
|
||||||
|
head :no_content
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def task_list_params
|
||||||
|
params.require(:task_list).permit(:name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_task_list
|
||||||
|
@task_list = TaskList.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -1,7 +1,5 @@
|
|||||||
class UsersController < ApplicationController
|
class UsersController < ApplicationController
|
||||||
|
|
||||||
UserProxy = Struct.new(:user_id)
|
|
||||||
|
|
||||||
before_action :ensure_valid_user, except: [:show, :login, :verify_login, :new, :create]
|
before_action :ensure_valid_user, except: [:show, :login, :verify_login, :new, :create]
|
||||||
skip_before_action :verify_authenticity_token, only: [:verify_login]
|
skip_before_action :verify_authenticity_token, only: [:verify_login]
|
||||||
|
|
||||||
|
7
app/models/task_item.rb
Normal file
7
app/models/task_item.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
class TaskItem < ApplicationRecord
|
||||||
|
|
||||||
|
belongs_to :task_list
|
||||||
|
|
||||||
|
validates :name, presence: true
|
||||||
|
|
||||||
|
end
|
12
app/models/task_list.rb
Normal file
12
app/models/task_list.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
class TaskList < ApplicationRecord
|
||||||
|
|
||||||
|
belongs_to :user
|
||||||
|
has_many :task_items, dependent: :delete_all
|
||||||
|
|
||||||
|
validates :name,
|
||||||
|
presence: true,
|
||||||
|
uniqueness: { case_sensitive: false }
|
||||||
|
|
||||||
|
scope :for_user, -> (user) { where(user_id: user) }
|
||||||
|
|
||||||
|
end
|
@ -2,6 +2,7 @@ class User < ApplicationRecord
|
|||||||
|
|
||||||
has_many :recipes, dependent: :nullify
|
has_many :recipes, dependent: :nullify
|
||||||
has_many :ingredients, dependent: :nullify
|
has_many :ingredients, dependent: :nullify
|
||||||
|
has_many :task_lists, dependent: :destroy
|
||||||
|
|
||||||
has_secure_password
|
has_secure_password
|
||||||
|
|
||||||
|
1
app/views/task_items/_task_item.json.jbuilder
Normal file
1
app/views/task_items/_task_item.json.jbuilder
Normal file
@ -0,0 +1 @@
|
|||||||
|
json.extract! task_item, :id, :name, :quantity, :created_on, :updated_on
|
3
app/views/task_lists/_task_list.json.jbuilder
Normal file
3
app/views/task_lists/_task_list.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
json.extract! task_list, :id, :name, :created_at, :updated_at
|
||||||
|
json.task_items task_list.task_items, partial: 'task_item/task_item', as: :task_item
|
1
app/views/task_lists/index.json.jbuilder
Normal file
1
app/views/task_lists/index.json.jbuilder
Normal file
@ -0,0 +1 @@
|
|||||||
|
json.array! @task_lists, partial: 'task_lists/task_list', as: :task_list
|
1
app/views/task_lists/show.json.jbuilder
Normal file
1
app/views/task_lists/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
|||||||
|
json.partial! 'task_lists/task_list', task_list: @task_list
|
@ -33,6 +33,10 @@ Rails.application.routes.draw do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :task_lists, only: [:index, :show, :create, :update, :destroy] do
|
||||||
|
resources :task_list_items, only: [:create, :update, :destroy]
|
||||||
|
end
|
||||||
|
|
||||||
resource :user, only: [:new, :create, :edit, :update]
|
resource :user, only: [:new, :create, :edit, :update]
|
||||||
|
|
||||||
get '/login' => 'users#login', as: :login
|
get '/login' => 'users#login', as: :login
|
||||||
|
10
db/migrate/20180827214745_create_task_lists.rb
Normal file
10
db/migrate/20180827214745_create_task_lists.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class CreateTaskLists < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :task_lists do |t|
|
||||||
|
t.integer :user_id, index: true, null: false
|
||||||
|
t.string :name
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
11
db/migrate/20180827215102_create_task_items.rb
Normal file
11
db/migrate/20180827215102_create_task_items.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
class CreateTaskItems < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :task_items do |t|
|
||||||
|
t.integer :task_list_id, index: true, null: false
|
||||||
|
t.string :name
|
||||||
|
t.string :quantity
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
20
db/schema.rb
20
db/schema.rb
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2017_04_14_233856) do
|
ActiveRecord::Schema.define(version: 2018_08_27_215102) do
|
||||||
|
|
||||||
create_table "ingredient_units", force: :cascade do |t|
|
create_table "ingredient_units", force: :cascade do |t|
|
||||||
t.integer "ingredient_id", null: false
|
t.integer "ingredient_id", null: false
|
||||||
@ -83,6 +83,7 @@ ActiveRecord::Schema.define(version: 2017_04_14_233856) do
|
|||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
t.text "preparation"
|
t.text "preparation"
|
||||||
|
t.integer "recipe_as_ingredient_id"
|
||||||
t.index ["recipe_id"], name: "index_recipe_ingredients_on_recipe_id"
|
t.index ["recipe_id"], name: "index_recipe_ingredients_on_recipe_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -126,6 +127,23 @@ ActiveRecord::Schema.define(version: 2017_04_14_233856) do
|
|||||||
t.index ["lowercase_name"], name: "index_tags_on_lowercase_name", unique: true
|
t.index ["lowercase_name"], name: "index_tags_on_lowercase_name", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "task_items", force: :cascade do |t|
|
||||||
|
t.integer "task_list_id", null: false
|
||||||
|
t.string "name"
|
||||||
|
t.string "quantity"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["task_list_id"], name: "index_task_items_on_task_list_id"
|
||||||
|
end
|
||||||
|
|
||||||
|
create_table "task_lists", force: :cascade do |t|
|
||||||
|
t.integer "user_id", null: false
|
||||||
|
t.string "name"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["user_id"], name: "index_task_lists_on_user_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "usda_food_weights", force: :cascade do |t|
|
create_table "usda_food_weights", force: :cascade do |t|
|
||||||
t.integer "usda_food_id", null: false
|
t.integer "usda_food_id", null: false
|
||||||
t.decimal "amount", precision: 7, scale: 3
|
t.decimal "amount", precision: 7, scale: 3
|
||||||
|
62
spec/controllers/task_list_controller_spec.rb
Normal file
62
spec/controllers/task_list_controller_spec.rb
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe TaskListsController, type: :controller do
|
||||||
|
|
||||||
|
render_views
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
request.accept = "application/json"
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:user) {
|
||||||
|
create(:user)
|
||||||
|
}
|
||||||
|
|
||||||
|
let(:valid_session) { {user_id: user.id} }
|
||||||
|
|
||||||
|
describe 'GET #index' do
|
||||||
|
it 'assigns all user lists as @task_lists' do
|
||||||
|
l1 = create(:task_list, user: user)
|
||||||
|
l2 = create(:task_list, user: user)
|
||||||
|
create(:task_list)
|
||||||
|
|
||||||
|
get :index, params: {}, session: valid_session
|
||||||
|
|
||||||
|
expect(assigns(:task_lists)).to contain_exactly(l1, l2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET #show' do
|
||||||
|
it 'assigns @task_list' do
|
||||||
|
l = create(:task_list, user: user)
|
||||||
|
get :show, params: {id: l.id}, session: valid_session
|
||||||
|
expect(assigns(:task_list)).to eq l
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST #create' do
|
||||||
|
it 'creates a task_list' do
|
||||||
|
expect do
|
||||||
|
post :create, params: {task_list: {name: 'name'}}, session: valid_session
|
||||||
|
end.to change(TaskList, :count).by 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'PATCH #update' do
|
||||||
|
it 'updates a task_list' do
|
||||||
|
l = create(:task_list, user: user)
|
||||||
|
patch :update, params: {id: l.id, task_list: {name: 'new name'}}, session: valid_session
|
||||||
|
l.reload
|
||||||
|
expect(l.name).to eq 'new name'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'DELETE #destroy' do
|
||||||
|
it 'destroys the list' do
|
||||||
|
l = create(:task_list, user: user)
|
||||||
|
delete :destroy, params: {id: l.id}, session: valid_session
|
||||||
|
expect(TaskList.where(id: l.id).count).to eq 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
7
spec/factories/task_items.rb
Normal file
7
spec/factories/task_items.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
factory :task_item do
|
||||||
|
task_list
|
||||||
|
name "MyString"
|
||||||
|
quantity "MyString"
|
||||||
|
end
|
||||||
|
end
|
11
spec/factories/task_lists.rb
Normal file
11
spec/factories/task_lists.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
FactoryBot.define do
|
||||||
|
|
||||||
|
sequence :generic_task_list_name do |n|
|
||||||
|
"list_#{n}"
|
||||||
|
end
|
||||||
|
|
||||||
|
factory :task_list do
|
||||||
|
user
|
||||||
|
name { generate(:generic_task_list_name) }
|
||||||
|
end
|
||||||
|
end
|
5
spec/models/task_item_spec.rb
Normal file
5
spec/models/task_item_spec.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe TaskItem, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
5
spec/models/task_list_spec.rb
Normal file
5
spec/models/task_list_spec.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe TaskList, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user