Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaM2429 committed Oct 11, 2023
2 parents ec2e92e + 1919b09 commit 2cc9d02
Show file tree
Hide file tree
Showing 21 changed files with 540 additions and 23 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ gem 'devise'
gem 'bullet'
gem 'rails', '~> 7.0.8'
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem 'faker'
gem 'sprockets-rails'

# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
gem 'shoulda-matchers'

# Use the Puma web server [https://github.com/puma/puma]
gem 'puma', '~> 5.0'
Expand All @@ -26,6 +27,7 @@ gem 'importmap-rails'
gem 'turbo-rails'

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem 'factory_bot_rails'
gem 'stimulus-rails'

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
Expand Down Expand Up @@ -55,6 +57,7 @@ gem 'rspec'
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'rspec-rails', '~> 6.0.0'
end

group :development do
Expand All @@ -73,3 +76,4 @@ group :test do
gem 'capybara'
gem 'selenium-webdriver'
end
# Run against this stable release
25 changes: 23 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ GEM
warden (~> 1.2.3)
diff-lcs (1.5.0)
erubi (1.12.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (3.1.1)
i18n (>= 1.8.11, < 2)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand Down Expand Up @@ -136,7 +143,7 @@ GEM
mini_mime (1.1.5)
minitest (5.20.0)
msgpack (1.7.2)
net-imap (0.4.0)
net-imap (0.4.1)
date
net-protocol
net-pop (0.1.2)
Expand Down Expand Up @@ -214,6 +221,14 @@ GEM
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-rails (6.0.3)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.56.4)
base64 (~> 0.1.1)
Expand All @@ -235,6 +250,8 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
shoulda-matchers (5.3.0)
activesupport (>= 5.2.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -280,15 +297,19 @@ DEPENDENCIES
capybara
debug
devise
factory_bot_rails
faker
importmap-rails
jbuilder
letter_opener
pg (~> 1.1)
puma (~> 5.0)
rails (~> 7.0.8)
rspec
rspec-rails (~> 6.0.0)
rubocop (>= 1.0, < 2.0)
selenium-webdriver
shoulda-matchers
sprockets-rails
stimulus-rails
turbo-rails
Expand All @@ -299,4 +320,4 @@ RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.4.19
2.4.20
10 changes: 10 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
color: #1875cc !important;
}

.triangle-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 8px #000;
display: inline-block;
margin-left: 5px;
}

.public-btn,
.public-btn-false {
width: 60px;
Expand Down
36 changes: 31 additions & 5 deletions app/controllers/foods_controller.rb
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
3 changes: 0 additions & 3 deletions app/views/devise/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
</button>
<div class="collapse justify-content-end navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item <%= 'active' if current_page?(root_path) %>">
<%= link_to 'Users', root_path, class: 'nav-link', 'aria-current': 'page' %>
</li>
<li class="nav-item <%= 'active' if current_page?(foods_path) %>">
<%= link_to 'Foods', foods_path, class: 'nav-link' %>
</li>
Expand Down
42 changes: 40 additions & 2 deletions app/views/foods/index.html.erb
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>
33 changes: 31 additions & 2 deletions app/views/foods/new.html.erb
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>
57 changes: 49 additions & 8 deletions db/seeds.rb
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!"
50 changes: 50 additions & 0 deletions spec/controllers/foods_controller_spec.rb
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
8 changes: 8 additions & 0 deletions spec/factories/devise.rb
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
9 changes: 9 additions & 0 deletions spec/factories/food.rb
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
Loading

0 comments on commit 2cc9d02

Please sign in to comment.