Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented some expert's recommendation #21

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem 'simplecov', '~> 0.22.0'
gem 'faker', '~> 3.1'
gem "devise"
gem 'turbolinks'
gem 'simple_form'

group :development, :test do
gem 'rspec-rails', '~> 6.0', '>= 6.0.1'
Expand Down
12 changes: 11 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.10-x64-mingw32)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pg (1.4.5)
pg (1.4.5-x64-mingw32)
public_suffix (5.0.1)
puma (5.6.5)
Expand Down Expand Up @@ -205,6 +208,9 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
simple_form (5.2.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -222,6 +228,8 @@ GEM
railties (>= 6.0.0)
tailwindcss-rails (2.0.21-x64-mingw32)
railties (>= 6.0.0)
tailwindcss-rails (2.0.21-x86_64-linux)
railties (>= 6.0.0)
thor (1.2.1)
timeout (0.3.1)
turbo-rails (1.3.2)
Expand Down Expand Up @@ -256,6 +264,7 @@ GEM

PLATFORMS
x64-mingw32
x86_64-linux

DEPENDENCIES
bootsnap
Expand All @@ -271,6 +280,7 @@ DEPENDENCIES
rails (~> 7.0.4)
rspec-rails (~> 6.0, >= 6.0.1)
selenium-webdriver
simple_form
simplecov (~> 0.22.0)
sprockets-rails
stimulus-rails
Expand All @@ -285,4 +295,4 @@ RUBY VERSION
ruby 3.0.3p157

BUNDLED WITH
2.3.26
2.4.6
4 changes: 2 additions & 2 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch
web: rails server -p 3000
css: rails tailwindcss:watch
Binary file not shown.
Binary file added app/assets/images/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
1 change: 0 additions & 1 deletion app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
17 changes: 4 additions & 13 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

/*

@layer components {
.btn-primary {
@apply py-2 px-4 bg-blue-200;
}
}

*/
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "./components/buttons.css";
@import "tailwindcss/utilities";
13 changes: 13 additions & 0 deletions app/assets/stylesheets/components/buttons.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@layer components {
.btn-gradient {
@apply text-gray-900 bg-gradient-to-r from-teal-200 to-lime-200 hover:bg-gradient-to-l hover:from-teal-200 hover:to-lime-200 focus:ring-4 focus:outline-none focus:ring-lime-200 dark:focus:ring-teal-700 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2;
}

.btn-green {
@apply text-white bg-gradient-to-r from-green-400 via-green-500 to-green-600 hover:bg-gradient-to-br focus:ring-4 focus:outline-none focus:ring-green-300 dark:focus:ring-green-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2;
}

.btn-blue {
@apply bg-cyan-500 text-white active:bg-cyan-600 font-bold uppercase text-xs px-4 py-2 rounded shadow hover:shadow-md outline-none focus:outline-none mr-1 mb-1 ease-linear transition-all duration-150;
}
}
12 changes: 0 additions & 12 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
class ApplicationController < ActionController::Base
before_action :initialize_session
before_action :load_cart

private

def initialize_session
session[:cart] ||= []
end

def load_cart
@cart = Product.find(session[:cart])
end
end
20 changes: 18 additions & 2 deletions app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
class CartsController < ApplicationController
before_action :authenticate_user!

def show
@cart = resource
end

def create
end

def destroy
end


private

def resource
if user_signed_in?
Product.where(id: current_user.cart.cart_products.pluck(:product_id))
elsif session[:product_id]
Product.where(id: session[:product_id])
end
end
end
3 changes: 0 additions & 3 deletions app/controllers/carts_products_controller.rb

This file was deleted.

24 changes: 17 additions & 7 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
class CategoriesController < ApplicationController
def index
@categories = Category.all
end

def show
@category = Category.find(params[:id])
end
def index
@categories = collection
end

def show
@category = resource
end

private

def collection
Category.ordered
end

def resource
collection.find(params[:id])
end
end
3 changes: 0 additions & 3 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
class HomeController < ApplicationController
def index
render
end
end
65 changes: 35 additions & 30 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
class OrdersController < ApplicationController
before_action :set_quote, only: [:show, :edit, :update, :destroy]

def index
@orders = Order.all
end

def show
@order = Order.find(params[:id])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ресурс?)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

і для чого у тебе екшин едіта та апдейта замовлення?) на тій же розетці ти маєш таку можливість?)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

end

def new
@order = Order.new
@order = Order.create(user_id: current_user.id, status: "created", ordered_at: DateTime.current)
current_user.cart.products.each do |item|
@order.products << item
current_user.cart[:id] = nil
end
# address = Address.new(address_params)
# address.save
# @order_detail = OrderDetail.new(order_detail_params)
# @order_detail.save
@order.save
end

def create
@order = Order.new(order_params)

if @order.save
redirect_to orders_path, notice: "Order was successfully created."
else
render :new

current_user.cart.cart_products.each do |item|
@order.cart_products << item
item.cart_id = nil
end
end

def edit
@order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
redirect_to root_path
end

def update
if @order.update(order_params)
redirect_to orders_path, notice: "Order was successfully updated."
else
render :edit
respond_to do |format|
if @order.update(order_params)
format.html { redirect_to @order, notice: 'Website was successfully updated.' }
format.json { render :show, status: :ok, location: @order }
else
format.html { render :edit }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end

def destroy
@order.destroy
redirect_to orders_path, notice: "Order was successfully destroyed."
end

private
def order_params
params.require(:order).permit()
end

def set_quote
@order = Order.find(params[:id])
end
# def order_detail_params
# params.require(:order_detail).permit(:first_name, :last_name, :email, address_id: address.id, order_id: @order.id)
# end

def order_params
params.require(:order).permit(:status, :ordered_at)
end
# def address_params
# params.require(:address).permit(:street, :city, :coutry, :comment, user_id: current_user.id)
# end
end
53 changes: 14 additions & 39 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,37 @@
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]

def index
@products = Product.all
end

def show
end

def new
@product = Product.new
end

def create
@product = Product.new(quote_params)

if @product.save
redirect_to products_path, notice: "Product was successfully created."
if params[:id]
@products = collection.where(category_id: params[:id])
else
render :new
@products = collection
end
end

def edit
end

def update
if @product.update(product_params)
redirect_to products_path, notice: "Product was successfully updated."
else
render :edit
end
end

def destroy
@product.destroy
redirect_to products_path, notice: "Product was successfully destroyed."
def show
@product = resource
end

def add_to_cart
id = params[:id].to_i
session[:cart] << id unless session[:cart].include?(id)
if session[:product_ids].nil?
session[:product_ids] = []
end

session[:product_ids] << params[:id]
redirect_to products_path
end

def remove_from_cart
id = params[:id].to_i
session[:cart].delete(id)
redirect_to products_path
end

private

def set_product
@product = Product.find(params[:id])
def collection
Product.ordered
end

def product_params
params.require(:product).permit(:name, :price, :description, :position, :category_id)
def resource
collection.find(params[:id])
end
end
10 changes: 9 additions & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ class Users::RegistrationsController < Devise::RegistrationsController
# POST /resource
def create
super
Cart.create(user_id: current_user.id)
current_cart = Cart.create(user_id: current_user.id)
if !session[:product_ids].nil? && !session[:product_ids].empty?
session[:product_ids].each do |product_id|
current_cart_products = CartProduct.create(cart_id: current_cart.id, product_id: product_id)
end
end
# session[:product_id].each do |product_id|
# CartProduct.create(cart_id: current_cart.id, product_id: product_id)
# end
end

# GET /resource/edit
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ class Users::SessionsController < Devise::SessionsController
# end

# POST /resource/sign_in
# def create
# super
# end
def create
super
session[:product_ids].each do |product_id|
current_cart_products = CartProduct.create(cart_id: current_user.cart.id, product_id: product_id)
end
end

# DELETE /resource/sign_out
# def destroy
# cart_products = Product.where(id: current_user.cart.cart_products.pluck(:product_id))
# super
# session[:product_id] = cart_products
# end

# protected
Expand Down
2 changes: 0 additions & 2 deletions app/helpers/application_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/carts_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/carts_products_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/helpers/categories_helper.rb

This file was deleted.

Loading