-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
540 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
class FoodsController < ApplicationController | ||
before_action :authenticate_user! | ||
def index; end | ||
def index | ||
@user = current_user | ||
order_direction = params[:sort] == 'name' && session[:sort_direction] == 'asc' ? 'desc' : 'asc' | ||
session[:sort_direction] = order_direction | ||
@foods = @user.foods.order("LOWER(name) #{order_direction}") | ||
end | ||
|
||
def show; end | ||
|
||
def new; end | ||
def new | ||
@food = Food.new | ||
end | ||
|
||
def create; end | ||
def destroy | ||
@food = current_user.foods.find(params[:id]) | ||
if @food.destroy | ||
redirect_to foods_path, notice: 'Food was successfully destroyed.' | ||
else | ||
redirect_to foods_path, alert: 'Failed to destroy food.' | ||
end | ||
end | ||
|
||
def edit; end | ||
def create | ||
@food = current_user.foods.build(food_params) | ||
|
||
def destroy; end | ||
if @food.save | ||
redirect_to foods_path, notice: 'Food was successfully created.' | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
private | ||
|
||
def food_params | ||
params.require(:food).permit(:name, :quantity, :measurement_unit, :price) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,42 @@ | ||
<%= render 'devise/shared/navbar' %> | ||
|
||
<h1>Foods#index</h1> | ||
<p>Find me in app/views/foods/index.html.erb</p> | ||
<div class="container mt-5"> | ||
<h2 class="text-center"><%= @user.name %>'s Food</h2> | ||
<h2 class="text-center">Food List</h2> | ||
<% if notice %> | ||
<p class="notice"><%= notice %></p> | ||
<% end %> | ||
<% if alert %> | ||
<p class="alert"><%= alert %></p> | ||
<% end %> | ||
<div class="my-3 text-end"> | ||
<%= link_to "Add New Food", new_food_path, class: "btn btn-primary mt-3" %> | ||
</div> | ||
<table class="border table"> | ||
<thead> | ||
<tr> | ||
<th class="food-name"> | ||
Name | ||
<%= link_to '', foods_path(sort: 'name'), class: 'triangle-up' %> | ||
</th> | ||
<th>Measurement Unit</th> | ||
<th>Unit Price</th> | ||
<th>Quantity</th> | ||
<th>Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @foods.each do |food| %> | ||
<tr> | ||
<td><%= food.name %></td> | ||
<td><%= food.measurement_unit %></td> | ||
<td>$ <%= food.price %></td> | ||
<td><%= food.quantity %></td> | ||
<td> | ||
<%= button_to "Delete", food, method: :delete, class: "btn btn-danger btn-sm" %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,31 @@ | ||
<h1>Foods#new</h1> | ||
<p>Find me in app/views/foods/new.html.erb</p> | ||
<%= render 'devise/shared/navbar' %> | ||
|
||
<div class="container mt-5"> | ||
<h1 class="text-center">Add New Food</h1> | ||
|
||
<%= form_for @food, html: { class: "mt-3" } do |f| %> | ||
<div class="mb-3"> | ||
<%= f.label :name, class: "form-label" %> | ||
<%= f.text_field :name, class: "form-control" %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :quantity, class: "form-label" %> | ||
<%= f.number_field :quantity, class: "form-control" %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :measurement_unit, class: "form-label" %> | ||
<%= f.text_field :measurement_unit, class: "form-control" %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :price, class: "form-label" %> | ||
<%= f.number_field :price, class: "form-control" %> | ||
</div> | ||
|
||
<%= f.submit "Save", class: "btn btn-primary w-100" %> | ||
<% end %> | ||
|
||
<%= link_to "Back to List", foods_path, class: "btn btn-secondary mt-3 w-100" %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
# frozen_string_literal: true | ||
# This file should contain all the record creation needed to seed the database with its default values. | ||
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). | ||
# | ||
# Examples: | ||
# | ||
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) | ||
# Character.create(name: "Luke", movie: movies.first) | ||
# db/seeds.rb | ||
|
||
# Crear usuarios | ||
user1 = User.create!( | ||
name: "Ejemplo Usuario 1", | ||
email: "[email protected]", | ||
password: "password123", | ||
confirmed_at: Time.now | ||
) | ||
|
||
user2 = User.create!( | ||
name: "Ejemplo Usuario 2", | ||
email: "[email protected]", | ||
password: "password123", | ||
confirmed_at: Time.now | ||
) | ||
|
||
# Crear alimentos asociados a usuarios específicos | ||
food1 = user1.foods.create!(name: 'Apple', measurement_unit: 'piece', price: 1, quantity: 10) | ||
food2 = user1.foods.create!(name: 'Banana', measurement_unit: 'piece', price: 2, quantity: 8) | ||
|
||
food3 = user2.foods.create!(name: 'Orange', measurement_unit: 'piece', price: 1, quantity: 12) | ||
food4 = user2.foods.create!(name: 'Carrot', measurement_unit: 'piece', price: 1, quantity: 15) | ||
|
||
# Crear recetas asociadas a usuarios específicos y alimentos específicos | ||
recipe1 = user1.recipes.create!( | ||
name: 'Fruit Salad', | ||
preparation_time: 10, | ||
cooking_time: 0, | ||
description: 'Healthy fruit salad recipe.', | ||
public: true | ||
) | ||
|
||
recipe2 = user2.recipes.create!( | ||
name: 'Carrot Soup', | ||
preparation_time: 15, | ||
cooking_time: 20, | ||
description: 'Delicious carrot soup recipe.', | ||
public: true | ||
) | ||
|
||
# Asociar alimentos a recetas | ||
RecipeFood.create!(recipe: recipe1, food: food1, quantity: 2) | ||
RecipeFood.create!(recipe: recipe1, food: food2, quantity: 3) | ||
|
||
RecipeFood.create!(recipe: recipe2, food: food3, quantity: 4) | ||
RecipeFood.create!(recipe: recipe2, food: food4, quantity: 5) | ||
|
||
puts "Seed data created successfully!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe FoodsController, type: :controller do | ||
let(:user) { TestConfiguration.create_example_user } | ||
|
||
before do | ||
sign_in user | ||
end | ||
|
||
describe 'GET #index' do | ||
it 'returns a successful response' do | ||
get :index | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe 'GET #show' do | ||
it 'returns a successful response' do | ||
food = create(:food, user:) | ||
get :show, params: { id: food.id } | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe 'GET #new' do | ||
it 'returns a successful response' do | ||
get :new | ||
expect(response).to be_successful | ||
end | ||
end | ||
|
||
describe 'POST #create' do | ||
it 'creates a new food with valid params' do | ||
expect do | ||
post :create, params: { food: attributes_for(:food) } | ||
end.to change(Food, :count).by(1) | ||
expect(response).to redirect_to(foods_path) | ||
end | ||
end | ||
|
||
describe 'DELETE #destroy' do | ||
it 'destroys the food' do | ||
food = create(:food, user:) | ||
expect do | ||
delete :destroy, params: { id: food.id } | ||
end.to change(Food, :count).by(-1) | ||
expect(response).to redirect_to(foods_path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FactoryBot.define do | ||
factory :user do | ||
email { Faker::Internet.email } | ||
password { 'password' } | ||
password_confirmation { 'password' } | ||
name { Faker::Name.name } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :food do | ||
name { Faker::Food.dish } | ||
measurement_unit { Faker::Food.metric_measurement } | ||
price { Faker::Number.decimal(l_digits: 2) } | ||
quantity { Faker::Number.number(digits: 2) } | ||
user | ||
end | ||
end |
Oops, something went wrong.