From f930382ffe75edc8d23c1492c2daae269dc925e8 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Wed, 10 Jul 2019 15:01:20 +0300 Subject: [PATCH 1/9] add books sort service --- Gemfile | 1 - app/admin/authors.rb | 15 +++++--- app/controllers/authors_controller.rb | 6 ---- app/controllers/books_controller.rb | 50 +++++++++++++-------------- app/controllers/home_controller.rb | 6 ++-- app/models/book.rb | 2 ++ app/services/best_sellers_books.rb | 11 ++++++ app/services/books_sorting.rb | 10 ++++++ app/views/books/index.html.haml | 8 ++--- config/routes.rb | 3 ++ 10 files changed, 68 insertions(+), 44 deletions(-) create mode 100644 app/services/best_sellers_books.rb create mode 100644 app/services/books_sorting.rb diff --git a/Gemfile b/Gemfile index d101787..ff160fe 100644 --- a/Gemfile +++ b/Gemfile @@ -47,7 +47,6 @@ gem 'travis' gem 'bootstrap-sass', '~> 3.4.1' gem 'sassc-rails', '>= 2.1.0' gem 'compass-rails', github: 'Compass/compass-rails' -gem 'omniauth-facebook' gem 'devise' gem 'cancancan' gem 'carrierwave', '~> 1.0' diff --git a/app/admin/authors.rb b/app/admin/authors.rb index c645026..218edea 100644 --- a/app/admin/authors.rb +++ b/app/admin/authors.rb @@ -1,18 +1,25 @@ ActiveAdmin.register Author do permit_params :first_name, :last_name, :biography - actions :all, except: [:show, :destroy] + actions :all, except: [:destroy, :show, :new] index do - selectable_column id_column column :first_name column :last_name column :biography - actions defaults: true do |author| - link_to 'Delete', author_path(author), method: :delete, data: { confirm: "Are you sure you want to delete this author? They are associated with #{author.books.count} books." } + actions defaults: false do |author| + link_to 'Delete', admin_author_path(author), method: :delete, data: { confirm: "Are you sure you want to delete this author? They are associated with #{author.books.count} books." } end end filter :first_name filter :last_name + + controller do + def destroy + Author.find(params[:id]).destroy + flash[:notice] = 'Author has been deleted.' + redirect_to admin_authors_path + end + end end diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb index 6512ebd..f959500 100644 --- a/app/controllers/authors_controller.rb +++ b/app/controllers/authors_controller.rb @@ -1,10 +1,4 @@ class AuthorsController < ApplicationController - def destroy - Author.find(params[:id]).destroy - flash[:notice] = 'Author has been deleted.' - redirect_to admin_authors_path - end - private def author_params diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 414639b..4676361 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -1,7 +1,7 @@ class BooksController < ApplicationController def index - @books = Book.order('created_at').page(params[:page]).per(12) - @active_sorting = 'Newest first' + @books = BooksSorting.new.sort(params[:sort], params[:direction]).page(params[:page]).per(12) + @active_sorting = params[:active_sorting] books_count popular_categories end @@ -20,29 +20,29 @@ def category render 'index' end - def price_low_to_high - @books = Book.order(:price).page(params[:page]).per(12) - books_count - popular_categories - @active_sorting = 'Price: Low to high' - render 'index' - end - - def popular_first - @books = Book.order(inventory: :desc).page(params[:page]).per(12) - books_count - popular_categories - @active_sorting = 'Popular first' - render 'index' - end - - def price_high_to_low - @books = Book.order(price: :desc).page(params[:page]).per(12) - books_count - popular_categories - @active_sorting = 'Price: High to low' - render 'index' - end + # def price_low_to_high + # @books = BooksSorting.new.sort(:price).page(params[:page]).per(12) + # books_count + # popular_categories + # @active_sorting = 'Price: Low to high' + # render 'index' + # end + + # def popular_first + # @books = BooksSorting.new.sort(params[:sort], params[:direction]).page(params[:page]).per(12) + # books_count + # popular_categories + # @active_sorting = 'Popular first' + # render 'index' + # end + + # def price_high_to_low + # @books = BooksSorting.new.sort(price: :desc).page(params[:page]).per(12) + # books_count + # popular_categories + # @active_sorting = 'Price: High to low' + # render 'index' + # end private diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 99a3fbd..2434a32 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,8 +1,6 @@ class HomeController < ApplicationController def index - @latest_books = Book.order('created_at').last(3) - @bestsellers = Category.all.select { |category| category.books.count.positive? }.map do |category| - category.books.max { |book| book.reviews.count } - end[0...4] + @latest_books = Book.latest + @bestsellers = BestSellersBooks.call end end diff --git a/app/models/book.rb b/app/models/book.rb index f95721a..fc30ec5 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -10,6 +10,8 @@ class Book < ApplicationRecord validates :price, presence: true, numericality: true validates :inventory, presence: true, numericality: { only_integer: true } + scope :latest, -> { order(created_at: :asc).last(3) } + def normalize_price self.price = price * 100 end diff --git a/app/services/best_sellers_books.rb b/app/services/best_sellers_books.rb new file mode 100644 index 0000000..07a119e --- /dev/null +++ b/app/services/best_sellers_books.rb @@ -0,0 +1,11 @@ +class BestSellersBooks + def self.call + new.call + end + + def call + Category.all.select { |category| category.books.count.positive? }.map do |category| + category.books.max { |book| book.reviews.count } + end[0...4] + end +end diff --git a/app/services/books_sorting.rb b/app/services/books_sorting.rb new file mode 100644 index 0000000..80efb12 --- /dev/null +++ b/app/services/books_sorting.rb @@ -0,0 +1,10 @@ +class BooksSorting + def initialize + @books = Book.all + end + + def sort(sort = 'created_at', direction = 'asc') + @books = Book.order("#{sort} #{direction}") + end + +end diff --git a/app/views/books/index.html.haml b/app/views/books/index.html.haml index ce6bed2..838503e 100644 --- a/app/views/books/index.html.haml +++ b/app/views/books/index.html.haml @@ -16,13 +16,13 @@ %i.fa.fa-angle-down.dropdown-icon.pull-right %ul.dropdown-menu %li - = link_to 'Newest first', books_path + = link_to 'Newest first', books_path(sort: 'created_at', direction: 'asc', active_sorting: 'Newest First') %li - = link_to 'Popular first', books_popular_first_path + = link_to 'Popular first', books_path(sort: 'inventory', direction: 'asc', active_sorting: 'Popular first') %li - = link_to 'Price: Low to high', books_price_low_to_high_path + = link_to 'Price: Low to high', books_path(sort: 'price', direction: 'asc', active_sorting: 'Price: Low to high') %li - = link_to 'Price: High to low', books_price_high_to_low_path + = link_to 'Price: High to low', books_path(sort: 'price', direction: 'desc', active_sorting: 'Price: High to low') %ul.list-inline.pt-10.mb-25.mr-240 %li.mr-35 %a.filter-link{href: "/books"} diff --git a/config/routes.rb b/config/routes.rb index f6ee566..f55da9e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,6 +28,9 @@ resources :checkout resources :orders resources :settings + resources :categories do + resources :books, only: [:index, :show] + end devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html From 2c0a6cd3eab0079da7dc1b4834641fe117a5ced3 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Wed, 10 Jul 2019 20:23:02 +0300 Subject: [PATCH 2/9] fixed routes and tests --- app/admin/authors.rb | 14 ++++++- app/controllers/books_controller.rb | 34 +-------------- app/controllers/carts_controller.rb | 2 +- app/controllers/categories_controller.rb | 15 +++++++ app/controllers/checkout_controller.rb | 4 +- app/controllers/orders_controller.rb | 38 +++++++---------- .../quick_registrations_controller.rb | 10 ++--- app/controllers/reviews_controller.rb | 2 +- app/views/books/index.html.haml | 4 +- app/views/books/show.html.haml | 2 +- app/views/carts/cart.haml | 2 +- app/views/home/index.html.haml | 6 +-- app/views/orders/index.haml | 8 ++-- .../{login.haml => index.haml} | 2 +- app/views/shared/_header.haml | 2 +- config/routes.rb | 41 ++++++++----------- spec/controllers/books_controller_spec.rb | 24 +++-------- spec/controllers/carts_controller_spec.rb | 16 ++++---- spec/views/books/index_spec.rb | 8 ++-- spec/views/carts/cart_spec.rb | 24 +++++------ 20 files changed, 110 insertions(+), 148 deletions(-) rename app/views/quick_registrations/{login.haml => index.haml} (94%) diff --git a/app/admin/authors.rb b/app/admin/authors.rb index 218edea..e08dc4c 100644 --- a/app/admin/authors.rb +++ b/app/admin/authors.rb @@ -1,6 +1,6 @@ ActiveAdmin.register Author do permit_params :first_name, :last_name, :biography - actions :all, except: [:destroy, :show, :new] + actions :all index do id_column @@ -8,10 +8,20 @@ column :last_name column :biography actions defaults: false do |author| - link_to 'Delete', admin_author_path(author), method: :delete, data: { confirm: "Are you sure you want to delete this author? They are associated with #{author.books.count} books." } + item 'Edit', edit_admin_author_path(author), class: 'member_link' + item 'Delete', admin_author_path(author), method: :delete, data: { confirm: "Are you sure you want to delete this author? They are associated with #{author.books.count} books." }, class: 'member_link' end end + form do |f| + f.inputs do + f.input :first_name + f.input :last_name + f.input :biography + end + f.actions + end + filter :first_name filter :last_name diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 4676361..3ec8f1b 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -1,7 +1,7 @@ class BooksController < ApplicationController def index @books = BooksSorting.new.sort(params[:sort], params[:direction]).page(params[:page]).per(12) - @active_sorting = params[:active_sorting] + @active_sorting = params[:active_sorting] || 'Newest first' books_count popular_categories end @@ -12,38 +12,6 @@ def show @quantity = 1 end - def category - @books = Category.find(params[:id]).books.order('created_at').page(params[:page]).per(12) - @active_sorting = 'Newest first' - books_count - popular_categories - render 'index' - end - - # def price_low_to_high - # @books = BooksSorting.new.sort(:price).page(params[:page]).per(12) - # books_count - # popular_categories - # @active_sorting = 'Price: Low to high' - # render 'index' - # end - - # def popular_first - # @books = BooksSorting.new.sort(params[:sort], params[:direction]).page(params[:page]).per(12) - # books_count - # popular_categories - # @active_sorting = 'Popular first' - # render 'index' - # end - - # def price_high_to_low - # @books = BooksSorting.new.sort(price: :desc).page(params[:page]).per(12) - # books_count - # popular_categories - # @active_sorting = 'Price: High to low' - # render 'index' - # end - private def books_count diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb index 8749d6c..259ea43 100644 --- a/app/controllers/carts_controller.rb +++ b/app/controllers/carts_controller.rb @@ -20,7 +20,7 @@ def add_book(book_id, new_quantity) end end - def show_cart + def index session.delete(:order_id) @coupon = get_coupon_discount(session[:coupon_id]) || 0 @cart_details = CartDetails.new(@cart, @coupon) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 9fa6741..5840ec2 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -1,7 +1,22 @@ class CategoriesController < ApplicationController + def show + @books = Category.find(params[:id]).books.order('created_at').page(params[:page]).per(12) + books_count + popular_categories + render 'books/index' + end + private def category_params params.require(:category).permit(:id, :title, :text) end + + def books_count + @all_books = Book.all.count + end + + def popular_categories + @popular_categories = Category.all.sort { |b, a| a.books.count <=> b.books.count }[0...5] + end end diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index dc2369b..c41fc0e 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -54,7 +54,7 @@ def prepopulate_addresses def current_order if session[:order_id].nil? - redirect_to cart_path + redirect_to carts_path else @order = Order.find(session[:order_id]) end @@ -77,7 +77,7 @@ def empty_session session.delete(:order_id) end - def find_coupon + def find_coupons session[:coupon_id].nil? ? nil : Coupon.find(session[:coupon_id]) end diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index c724f42..25e2a48 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -1,7 +1,7 @@ class OrdersController < ApplicationController - before_action :login_customer, only: :create_order + before_action :login_customer, only: :create - def create_order + def create if session[:order_id].nil? create_order_with_items else @@ -11,26 +11,18 @@ def create_order end def index - @orders = current_customer.orders - @orders_sorting = 'All' - end - - def in_progress - @orders = current_customer.orders.in_progress - @orders_sorting = 'In Progress' - render 'index' - end - - def in_delivery - @orders = current_customer.orders.in_delivery - @active_sorting = 'In Delivery' - render 'index' - end - - def canceled - @orders = current_customer.orders.canceled - @active_sorting = 'Canceled' - render 'index' + @orders_sorting = params[:sorting] + case @orders_sorting + when 'In Progress' + @orders = current_customer.orders.in_progress + when 'In Delivery' + @orders = current_customer.orders.in_delivery + when 'Canceled' + @orders = current_customer.orders.canceled + else + @orders = current_customer.orders + @orders_sorting = 'All' + end end private @@ -47,7 +39,7 @@ def create_item(book_id, quantity) end def login_customer - redirect_to login_path unless customer_signed_in? + redirect_to quick_registrations_path unless customer_signed_in? end def create_order_with_items diff --git a/app/controllers/quick_registrations_controller.rb b/app/controllers/quick_registrations_controller.rb index fb175b8..577ac28 100644 --- a/app/controllers/quick_registrations_controller.rb +++ b/app/controllers/quick_registrations_controller.rb @@ -1,26 +1,24 @@ class QuickRegistrationsController < ApplicationController VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i - def show_lazy_login + def index @customer = Customer.new - render 'login' end - def lazy_sign_up + def create if email_valid?(params[:customer][:email]) generated_password = Devise.friendly_token.first(8) customer = Customer.new(email: params[:customer][:email], password: generated_password) customer.save(validate: false) sign_in(:customer, customer) - redirect_to cart_path + redirect_to carts_path customer.send_reset_password_instructions else flash[:notice] = 'Your email is invalid' - redirect_to login_path end end def email_valid?(email) - email.match(VALID_EMAIL_REGEX) + !Customer.find_by(email: email).present? && email.match(VALID_EMAIL_REGEX) end end diff --git a/app/controllers/reviews_controller.rb b/app/controllers/reviews_controller.rb index c634fbf..bb4c936 100644 --- a/app/controllers/reviews_controller.rb +++ b/app/controllers/reviews_controller.rb @@ -1,5 +1,5 @@ class ReviewsController < ApplicationController - before_action :authenticate_customer!, only: :create + before_action :authenticate_customer!, only: :new def create @review = Review.new(review_params) diff --git a/app/views/books/index.html.haml b/app/views/books/index.html.haml index 838503e..30cb3ba 100644 --- a/app/views/books/index.html.haml +++ b/app/views/books/index.html.haml @@ -30,7 +30,7 @@ %span.badge.general-badge= @all_books - @popular_categories.each do |category| %li.mr-35.filter-link - = link_to(category.title, books_category_path(id: category.id), class: 'filter-link') + = link_to(category.title, category_path(category), method: :get, class: 'filter-link') %span.badge.general-badge= category.books.count - paginate(@books) @@ -46,7 +46,7 @@ .general-thumb-link-wrap %a.thumb-hover-link.show-book{href: "/books/#{book.id}"} %i.fa.fa-eye.thumb-icon{'aria-hidden': "true"} - = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), books_add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') + = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') .general-thumb-info %p.title= book.title %p.font-16.in-gold-500= Money.new(book.price, 'USD').format diff --git a/app/views/books/show.html.haml b/app/views/books/show.html.haml index 92f2515..a0651fd 100644 --- a/app/views/books/show.html.haml +++ b/app/views/books/show.html.haml @@ -14,7 +14,7 @@ .product-gallery .mb-20 %img.img-responsive{src: @book.cover_url} - %form.col-sm-6.row{action: books_add_to_cart_path, method: 'post'} + %form.col-sm-6.row{action: add_to_cart_path, method: 'post'} %h1.mt-res-0= @book.title %p.lead.small= "#{@book.author.first_name} #{@book.author.last_name}" .row diff --git a/app/views/carts/cart.haml b/app/views/carts/cart.haml index 59e70d9..3823982 100644 --- a/app/views/carts/cart.haml +++ b/app/views/carts/cart.haml @@ -77,4 +77,4 @@ %td %strong.font-18= @cart_details.total %button.btn.btn-default.mb-20.visible-xs-inline-block Checkout - = link_to('Checkout', create_order_path, class: 'btn btn-default mb-20 hidden-xs center-block check-center') + = link_to('Checkout', orders_path, method: :post, class: 'btn btn-default mb-20 hidden-xs center-block check-center') diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 82f7256..151c4a7 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -20,8 +20,8 @@ %p.lead= truncate(book.description, :length =>100) unless book.description.nil? %p.lead.small= "#{book.author.first_name} #{book.author.last_name}" .general-align - = link_to('Buy Now', books_add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'btn btn-default') - + = link_to('Buy Now', add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'btn btn-default') + %a.left.carousel-control{href: "#slider", role: "button", 'data-slide': "prev"} %span.icon-prev{'aria-hidden': "true"} %span.sr-only Previous @@ -47,7 +47,7 @@ .general-thumb-link-wrap %a.thumb-hover-link.show-book{href: "/books/#{book.id}"} %i.fa.fa-eye.thumb-icon{'aria-hidden': "true"} - = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), books_add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') + = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') .mb-35 .clearfix diff --git a/app/views/orders/index.haml b/app/views/orders/index.haml index 5f3dc0c..bdff60d 100644 --- a/app/views/orders/index.haml +++ b/app/views/orders/index.haml @@ -7,13 +7,13 @@ %i.fa.fa-angle-down.dropdown-icon.pull-right %ul.dropdown-menu %li - %a{href: "/orders"} All + = link_to 'All', orders_path(sorting: 'All') %li - %a{href: "/orders/in_progress"} In Progress + = link_to 'In Progress', orders_path(sorting: 'In Progress') %li - %a{href: "/orders/in_delivery"} In Delivery + = link_to 'In Delivery', orders_path(sorting: 'In Delivery') %li - %a{href: "/orders/canceled"} Canceled + = link_to 'Canceled', orders_path(sorting: 'Canceled') %li - if @orders.empty? %h3.mt-60 diff --git a/app/views/quick_registrations/login.haml b/app/views/quick_registrations/index.haml similarity index 94% rename from app/views/quick_registrations/login.haml rename to app/views/quick_registrations/index.haml index ad6401f..eed6305 100644 --- a/app/views/quick_registrations/login.haml +++ b/app/views/quick_registrations/index.haml @@ -32,7 +32,7 @@ .mb-40 %p.mb-5 Quick Register %p.general-info-text.font-12.mb-20 You’ll be able to create password later - = form_for @customer, url: login_path, method: 'post' do |f| + = form_for @customer, url: quick_registrations_path, method: 'post' do |f| .form-group.mb-30 = f.label :email, class: 'control-label input-label' = f.text_field :email, class: 'form-control' diff --git a/app/views/shared/_header.haml b/app/views/shared/_header.haml index b6fb990..082654f 100644 --- a/app/views/shared/_header.haml +++ b/app/views/shared/_header.haml @@ -10,7 +10,7 @@ %a.shop-link.pull-right.visible-xs{href: "#"} %span.shop-icon %span.shop-quantity= @cart.count - %a.shop-link.pull-right.hidden-xs{href: "/cart"} + %a.shop-link.pull-right.hidden-xs{href: "/carts"} %span.shop-icon %span.shop-quantity= @cart.count #navbar.navbar-collapse.collapse.pl-30{'aria-expanded': "false"} diff --git a/config/routes.rb b/config/routes.rb index f55da9e..b5f07e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,36 +1,27 @@ Rails.application.routes.draw do root to: 'home#index' devise_for :customers, controllers: { omniauth_callbacks: 'omniauth_callbacks' } - get '/books/popular_first' => 'books#popular_first' - get '/books/price_low_to_high' => 'books#price_low_to_high' - get '/books/price_high_to_low' => 'books#price_high_to_low' - get '/books/:id/increase_quantity' => 'books#increase_quantity' - post 'books/add_to_cart' => 'carts#add_to_cart' - get 'cart' => 'carts#show_cart' + post 'add_to_cart' => 'carts#add_to_cart' get 'cart/remove_item' => 'carts#remove_item' get 'cart/increase_quantity' => 'carts#increase_quantity' get 'cart/decrease_quantity' => 'carts#decrease_quantity' post 'cart/apply_coupon' => 'carts#apply_coupon' - post 'orders' => 'orders#create' - get 'books/category' => 'books#category' - get 'settings' => 'settings#index' - post 'login' => 'quick_registrations#lazy_sign_up' - get 'login' => 'quick_registrations#show_lazy_login' - get 'orders/in_progress' => 'orders#in_progress' - get 'orders/in_delivery' => 'orders#in_delivery' - get 'orders/canceled' => 'orders#canceled' - get 'create_order' => 'orders#create_order' - put 'settings' => 'settings#update' - put 'settings' => 'settings#update_email' - resources :reviews - resources :books - resources :authors - resources :checkout - resources :orders - resources :settings - resources :categories do - resources :books, only: [:index, :show] + resources :reviews, only: :create + resources :carts, only: :index + resources :orders, only: [:index, :create] + resources :quick_registrations, only: [:index, :create] + resources :checkout, only: [:show, :update] + resources :settings, only: [:index, :update, :update_email] + resources :categories, only: :show do + resources :books, shallow: true do + end end + resources :books, only: :index do + collection do + get :index + end + end + devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html diff --git a/spec/controllers/books_controller_spec.rb b/spec/controllers/books_controller_spec.rb index b437ac4..f1e88fb 100644 --- a/spec/controllers/books_controller_spec.rb +++ b/spec/controllers/books_controller_spec.rb @@ -8,18 +8,6 @@ get :index expect(response).to render_template :index end - it 'renders :index template for price_low_to_high' do - get :price_low_to_high - expect(response).to render_template :index - end - it 'renders :index template for popular_first' do - get :popular_first - expect(response).to render_template :index - end - it 'renders :index template for price_high_to_low' do - get :price_high_to_low - expect(response).to render_template :index - end end context 'assigns @active_sorting' do @@ -30,15 +18,15 @@ expect(assigns(:active_sorting)).to eq('Newest first') end it 'is Price: Low to high' do - get :price_low_to_high + get :index, params: { sort: 'price', direction: 'asc', active_sorting: 'Price: Low to high' } expect(assigns(:active_sorting)).to eq('Price: Low to high') end it 'is Popular first' do - get :popular_first + get :index, params: { sort: 'inventory', direction: 'asc', active_sorting: 'Popular first' } expect(assigns(:active_sorting)).to eq('Popular first') end it 'is Price: High to low' do - get :price_high_to_low + get :index, params: { sort: 'price', direction: 'desc', active_sorting: 'Price: High to low' } expect(assigns(:active_sorting)).to eq('Price: High to low') end end @@ -56,17 +44,17 @@ end it 'shows books in order by price low to high' do - get :price_low_to_high + get :index, params: { sort: 'price', direction: 'asc', active_sorting: 'Price: Low to high' } expect(assigns(:books)).to match_array(Book.order(:price)) end it 'shows books in order by price high to low' do - get :price_high_to_low + get :index, params: { sort: 'inventory', direction: 'asc', active_sorting: 'Popular first' } expect(assigns(:books)).to match_array(Book.order(price: :desc)) end it 'shows books in order by popularity' do - get :popular_first + get :index, params: { sort: 'price', direction: 'desc', active_sorting: 'Price: High to low' } expect(assigns(:books)).to match_array(Book.order(inventory: :desc)) end end diff --git a/spec/controllers/carts_controller_spec.rb b/spec/controllers/carts_controller_spec.rb index ca0ebc9..9f251f8 100644 --- a/spec/controllers/carts_controller_spec.rb +++ b/spec/controllers/carts_controller_spec.rb @@ -7,20 +7,20 @@ context 'empty cart' do it 'render cart' do - get :show_cart + get :index expect(response).to render_template :cart end it 'does not have items' do - get :show_cart + get :index expect(assigns(:cart)).to match_array([]) end it 'does not have coupon' do - get :show_cart + get :index expect(assigns(:coupon)).to eq(0) end it 'has cart details' do - get :show_cart + get :index expect(assigns(:cart_details)).to_not be_nil end end @@ -137,7 +137,7 @@ it 'redirects back' do request.env['HTTP_REFERER'] = '/cart' - get :show_cart + get :index post :increase_quantity, params: { book_id: book.id }, session: { cart: cart } expect(response).to redirect_to('/cart') end @@ -162,7 +162,7 @@ it 'redirects back' do request.env['HTTP_REFERER'] = '/cart' - get :show_cart + get :index cart = [{ book_id: book.id, title: book.title, price: book.price, url: book.cover_url, quantity: 2 }] post :decrease_quantity, params: { book_id: book.id }, session: { cart: cart } expect(response).to redirect_to('/cart') @@ -199,7 +199,7 @@ it 'renders cart' do request.env['HTTP_REFERER'] = '/cart' - get :show_cart + get :index post :apply_coupon, params: { coupon: coupon.name }, session: { cart: cart } expect(response).to redirect_to('/cart') end @@ -236,7 +236,7 @@ it 'redirects back' do request.env['HTTP_REFERER'] = '/cart' - get :show_cart + get :index post :apply_coupon, params: { coupon: 'not existed' }, session: { cart: cart } expect(response).to redirect_to('/cart') end diff --git a/spec/views/books/index_spec.rb b/spec/views/books/index_spec.rb index 3a07f89..48fc206 100644 --- a/spec/views/books/index_spec.rb +++ b/spec/views/books/index_spec.rb @@ -25,10 +25,10 @@ it 'has options for sorting' do visit '/books' - expect(page).to have_selector(:link, 'Newest first', href: '/books') - expect(page).to have_selector(:link, 'Popular first', href: '/books/popular_first') - expect(page).to have_selector(:link, 'Price: Low to high', href: '/books/price_low_to_high') - expect(page).to have_selector(:link, 'Price: High to low', href: '/books/price_high_to_low') + expect(page).to have_selector(:link, 'Newest first') + expect(page).to have_selector(:link, 'Popular first') + expect(page).to have_selector(:link, 'Price: Low to high') + expect(page).to have_selector(:link, 'Price: High to low') end context 'view more' do diff --git a/spec/views/carts/cart_spec.rb b/spec/views/carts/cart_spec.rb index 88ce728..acd05d0 100644 --- a/spec/views/carts/cart_spec.rb +++ b/spec/views/carts/cart_spec.rb @@ -3,7 +3,7 @@ describe 'carts', type: :feature do context 'empty cart' do it 'does not have added books on session start' do - visit '/cart' + visit '/carts' expect(page).to have_selector('.shop-quantity', text: '0') expect(page).to have_content('You don`t have added books. Go and find something in Catalog!') end @@ -18,21 +18,21 @@ it 'from home page' do visit '/' page.find('.add-to-cart').click - visit '/cart' + visit '/carts' expect(page).to have_content(book.title) end it 'from books page' do visit '/books' page.find('.add-to-cart').click - visit '/cart' + visit '/carts' expect(page).to have_content(book.title) end it 'from book show page' do visit "/books/#{book.id}" page.find_button(text: 'Add to Cart').click - visit '/cart' + visit '/carts' expect(page).to have_content(book.title) end @@ -40,7 +40,7 @@ visit "/books/#{book.id}" page.find('.quantity-input').set('3') page.find_button(text: 'Add to Cart').click - visit '/cart' + visit '/carts' expect(page).to have_content(book.title) expect(page).to have_selector(".quantity-input[value='3']") end @@ -55,7 +55,7 @@ end it 'increase' do - visit '/cart' + visit '/carts' page.find('.increase').click expect(page).to have_selector(".quantity-input[value='2']") end @@ -63,13 +63,13 @@ it 'decrease' do visit '/books' page.find('.add-to-cart').click - visit '/cart' + visit '/carts' page.find('.decrease').click expect(page).to have_selector(".quantity-input[value='1']") end it 'cannot decrease from 1' do - visit '/cart' + visit '/carts' expect(page).to have_no_selector('.decrease') expect(page).to have_selector(".quantity-input[value='1']") end @@ -84,7 +84,7 @@ end it 'clicks remove' do - visit '/cart' + visit '/carts' page.find('.general-cart-close').click expect(page).to have_no_content(book.title) expect(page).to have_selector('.shop-quantity', text: '0') @@ -102,12 +102,12 @@ end it 'coupon form is shown' do - visit '/cart' + visit '/carts' expect(page).to have_selector('.general-input-group') end it 'valid coupon' do - visit '/cart' + visit '/carts' page.find('.coupon-input').set(coupon.name) page.find_button(text: 'Apply Coupon').click expect(page).to have_no_selector('.general-input-group') @@ -115,7 +115,7 @@ end it 'not valid coupon' do - visit '/cart' + visit '/carts' page.find('.coupon-input').set('not valid') page.find_button(text: 'Apply Coupon').click expect(page).to have_selector('.general-input-group') From e3444793cd96ca1518d515ffff9dc9ed3b9639d8 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Wed, 10 Jul 2019 21:19:22 +0300 Subject: [PATCH 3/9] fix authors after merge --- app/controllers/authors_controller.rb | 11 ----------- app/services/confirm_order.rb | 2 +- app/views/checkout/delivery.haml | 2 +- config/locales/en.yml | 1 + 4 files changed, 3 insertions(+), 13 deletions(-) diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb index 8ac0365..fea2418 100644 --- a/app/controllers/authors_controller.rb +++ b/app/controllers/authors_controller.rb @@ -1,13 +1,2 @@ class AuthorsController < ApplicationController - def destroy - Author.find(params[:id]).discard - flash[:notice] = t('.admin.messages.author_deleted') - redirect_to admin_authors_path - end - - private - - def author_params - params.require(:author).permit(:first_name, :last_name, :biography) - end end diff --git a/app/services/confirm_order.rb b/app/services/confirm_order.rb index 24ffe76..cf0782c 100644 --- a/app/services/confirm_order.rb +++ b/app/services/confirm_order.rb @@ -17,7 +17,7 @@ def confirm if order_has_all_attributes? order.start_processing! else - order.errors.add(:base, 'You missed to enter some info!') + order.errors.add(:base, t('common.error_missed_info')) end end end diff --git a/app/views/checkout/delivery.haml b/app/views/checkout/delivery.haml index 959ef7d..bf8498c 100644 --- a/app/views/checkout/delivery.haml +++ b/app/views/checkout/delivery.haml @@ -47,4 +47,4 @@ %strong.font-18= t('order.order_total') %td %strong.font-18= @cart_details.total - = submit_tag t('common.save_and_continue'),class: 'btn btn-default center-block mb-20' + = submit_tag t('common.save_and_continue'), class: 'btn btn-default center-block mb-20' diff --git a/config/locales/en.yml b/config/locales/en.yml index f21e0e5..456804a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -35,6 +35,7 @@ en: common: save_and_continue: "Save and continue" back_to_store: "Back to Store" + error_missed_info: "You missed to enter some info!" order: order_summary: "Order Summary" item_total: "Item Total:" From d4c85445706309cd018586a5e8a7b414e76d6ee1 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Thu, 11 Jul 2019 08:45:59 +0300 Subject: [PATCH 4/9] add sorting by review on pupolar books, add missed translations to reviews --- app/admin/orders.rb | 10 +++++----- app/models/review.rb | 2 +- app/views/books/index.html.haml | 2 +- config/locales/en.yml | 10 ++++++++++ ...90711053738_add_counter_cache_to_book_on_reviews.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20190711053738_add_counter_cache_to_book_on_reviews.rb diff --git a/app/admin/orders.rb b/app/admin/orders.rb index 6de3cb6..48569e7 100644 --- a/app/admin/orders.rb +++ b/app/admin/orders.rb @@ -62,23 +62,23 @@ if resource.start_delivery! redirect_to admin_order_path(resource), notice: t('.in_delivery') else - redirect_to admin_order_path(resource), alert: 'Sorry, not all requirements were met for delivering' + redirect_to admin_order_path(resource), alert: t('.failed_start_delivery') end end member_action :finish_delivery, method: :post do if resource.finish_delivery! - redirect_to admin_order_path(resource), notice: 'Order has been delivered!' + redirect_to admin_order_path(resource), notice: t('.delivered') else - redirect_to admin_order_path(resource), alert: 'Sorry, not all requirements were met to finish delivering' + redirect_to admin_order_path(resource), alert: t('.delivery_failed') end end member_action :cancel, method: :post do if resource.cancel! - redirect_to admin_order_path(resource), notice: 'Order has been canceled!' + redirect_to admin_order_path(resource), notice: t('.canceled') else - redirect_to admin_order_path(resource), alert: 'Sorry, not all requirements were met for canceling' + redirect_to admin_order_path(resource), alert: t('.cancel_failed') end end end diff --git a/app/models/review.rb b/app/models/review.rb index cb958b4..fdeba72 100644 --- a/app/models/review.rb +++ b/app/models/review.rb @@ -19,7 +19,7 @@ class Review < ApplicationRecord end end - belongs_to :book + belongs_to :book, counter_cache: true belongs_to :customer VALID_STRING_REGEX = /[a-zA-Z0-9!#$%&'*+-\/=?^_`{|}~]/ diff --git a/app/views/books/index.html.haml b/app/views/books/index.html.haml index a76e13a..050922e 100644 --- a/app/views/books/index.html.haml +++ b/app/views/books/index.html.haml @@ -18,7 +18,7 @@ %li = link_to 'Newest first', books_path(sort: 'created_at', direction: 'asc', active_sorting: t('.newest_first')) %li - = link_to 'Popular first', books_path(sort: 'inventory', direction: 'asc', active_sorting: t('.popular_first')) + = link_to 'Popular first', books_path(sort: 'reviews_count', direction: 'desc', active_sorting: t('.popular_first')) %li = link_to 'Price: Low to high', books_path(sort: 'price', direction: 'asc', active_sorting: t('.price_low_to_high')) %li diff --git a/config/locales/en.yml b/config/locales/en.yml index 456804a..1e811fc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -241,6 +241,16 @@ en: reject: notice: "Review has been rejected!" alert: "Sorry, not all requirements were met for rejecting" + orders: + start_delivery: + in_delivery: "Order is in delivery now!" + failed_start_delivery: "Sorry, not all requirements were met for delivering" + finish_delivery: + delivered: "Order has been delivered!" + delivery_failed: 'Sorry, not all requirements were met to finish delivering' + cancel: + canceled: "Order has been canceled!" + failed_cancel: "Sorry, not all requirements were met for canceling" devise: failure: customer: diff --git a/db/migrate/20190711053738_add_counter_cache_to_book_on_reviews.rb b/db/migrate/20190711053738_add_counter_cache_to_book_on_reviews.rb new file mode 100644 index 0000000..f001200 --- /dev/null +++ b/db/migrate/20190711053738_add_counter_cache_to_book_on_reviews.rb @@ -0,0 +1,5 @@ +class AddCounterCacheToBookOnReviews < ActiveRecord::Migration[5.2] + def change + add_column :books, :reviews_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 6ef38ae..caf670f 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: 2019_07_08_115252) do +ActiveRecord::Schema.define(version: 2019_07_11_053738) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -78,6 +78,7 @@ t.bigint "category_id" t.string "cover" t.datetime "discarded_at" + t.integer "reviews_count", default: 0 t.index ["author_id"], name: "index_books_on_author_id" t.index ["category_id"], name: "index_books_on_category_id" t.index ["discarded_at"], name: "index_books_on_discarded_at" From 02ea86fbe735ec418cdeebb89d64c974d4ef1e9c Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Thu, 11 Jul 2019 08:55:07 +0300 Subject: [PATCH 5/9] remove useless methods --- app/controllers/checkout_controller.rb | 4 ---- app/controllers/reviews_controller.rb | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index ef3277b..1f342a3 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -77,10 +77,6 @@ def empty_session session.delete(:order_id) end - def find_coupons - session[:coupon_id].nil? ? nil : Coupon.find(session[:coupon_id]) - end - def login_customer redirect_to login_path unless customer_signed_in? end diff --git a/app/controllers/reviews_controller.rb b/app/controllers/reviews_controller.rb index d559093..4c9285e 100644 --- a/app/controllers/reviews_controller.rb +++ b/app/controllers/reviews_controller.rb @@ -1,5 +1,5 @@ class ReviewsController < ApplicationController - before_action :authenticate_customer!, only: :new + before_action :authenticate_customer!, only: :create def create @review = Review.new(review_params) From b08da64cd19c6264286e51bebc30c0574e066384 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Thu, 11 Jul 2019 14:18:05 +0300 Subject: [PATCH 6/9] add partial for books --- app/controllers/categories_controller.rb | 4 +- app/controllers/home_controller.rb | 2 +- app/controllers/orders_controller.rb | 12 +---- app/controllers/settings_controller.rb | 2 +- app/helpers/books_helper.rb | 7 +++ app/services/books_sorting.rb | 7 ++- app/services/orders_filter.rb | 18 +++++++ app/views/books/_books_collection.haml | 16 ++++++ app/views/books/_sort_drop_down.haml | 13 +++++ app/views/books/index.html.haml | 32 ++--------- app/views/checkout/address.haml | 69 +++++------------------- app/views/home/index.html.haml | 17 +----- app/views/orders/index.haml | 10 ++-- app/views/settings/index.haml | 6 +-- app/views/shared/_address.haml | 42 +++++++++++++++ config/routes.rb | 6 +-- 16 files changed, 132 insertions(+), 131 deletions(-) create mode 100644 app/services/orders_filter.rb create mode 100644 app/views/books/_books_collection.haml create mode 100644 app/views/books/_sort_drop_down.haml create mode 100644 app/views/shared/_address.haml diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 5840ec2..a272491 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -1,6 +1,8 @@ class CategoriesController < ApplicationController def show - @books = Category.find(params[:id]).books.order('created_at').page(params[:page]).per(12) + category_books = Category.find(params[:id]).books + @books = BooksSorting.new(category_books).sort(params[:sort], params[:direction]).page(params[:page]).per(12) + @active_sorting = params[:active_sorting] || t('books.index.newest_first') books_count popular_categories render 'books/index' diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 2434a32..49f623b 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,6 +1,6 @@ class HomeController < ApplicationController def index @latest_books = Book.latest - @bestsellers = BestSellersBooks.call + @books = BestSellersBooks.call end end diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 48b8eff..f79e008 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -12,17 +12,7 @@ def create def index @orders_sorting = params[:sorting] - case @orders_sorting - when t('orders.index.in_progress') - @orders = current_customer.orders.in_progress - when t('orders.index.in_delivery') - @orders = current_customer.orders.in_delivery - when t('orders.index.canceled') - @orders = current_customer.orders.canceled - else - @orders = current_customer.orders - @orders_sorting = t('orders.index.all') - end + @orders = OrdersFilter.call(current_customer, @orders_sorting) end private diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index 56089d8..58cc2bf 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -8,6 +8,6 @@ def update end def customer_params - params.require(:customer).permit(:email, :password, :first_name, :last_name, { shipping_address_attributes: [:address_line, :country, :city, :zip, :phone] }, { billing_address_attributes: [:address_line, :country, :city, :zip, :phone] }) + params.permit(:email, :password, :first_name, :last_name, { shipping_address_attributes: [:address_line, :country, :city, :zip, :phone] }, { billing_address_attributes: [:address_line, :country, :city, :zip, :phone] }) end end diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb index 4b9311e..acb02ea 100644 --- a/app/helpers/books_helper.rb +++ b/app/helpers/books_helper.rb @@ -1,2 +1,9 @@ module BooksHelper + def dropdown_with_links + if current_page?('/books') + render partial: 'books/sort_drop_down', locals: { active_controller: 'books', action: :index } + else + render partial: 'books/sort_drop_down', locals: { active_controller: 'categories', action: :show } + end + end end diff --git a/app/services/books_sorting.rb b/app/services/books_sorting.rb index 80efb12..b3d67fc 100644 --- a/app/services/books_sorting.rb +++ b/app/services/books_sorting.rb @@ -1,10 +1,9 @@ class BooksSorting - def initialize - @books = Book.all + def initialize(books = Book.all) + @books = books end def sort(sort = 'created_at', direction = 'asc') - @books = Book.order("#{sort} #{direction}") + @books = @books.order("#{sort} #{direction}") end - end diff --git a/app/services/orders_filter.rb b/app/services/orders_filter.rb new file mode 100644 index 0000000..92c7e63 --- /dev/null +++ b/app/services/orders_filter.rb @@ -0,0 +1,18 @@ +class OrdersFilter + def self.call(current_customer, sorting) + new.call(current_customer, sorting) + end + + def call(current_customer, sorting) + @orders = case sorting + when I18n.t('orders.index.in_progress') + current_customer.orders.in_progress + when I18n.t('orders.index.in_delivery') + current_customer.orders.in_delivery + when I18n.t('orders.index.canceled') + current_customer.orders.canceled + else + current_customer.orders + end + end +end diff --git a/app/views/books/_books_collection.haml b/app/views/books/_books_collection.haml new file mode 100644 index 0000000..1fbe5ca --- /dev/null +++ b/app/views/books/_books_collection.haml @@ -0,0 +1,16 @@ +- if book + .col-xs-6.col-sm-3 + .general-thumb-wrap + .thumbnail.general-thumbnail + %img.img-shadow.general-thumbnail-img{src: book.cover_url, alt: book.title} + .thumb-hover + .general-thumb-link-wrap + %a.thumb-hover-link.show-book{href: "/books/#{book.id}"} + %i.fa.fa-eye.thumb-icon{'aria-hidden': "true"} + = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') + .general-thumb-info + %p.title= book.title + %p.font-16.in-gold-500= Money.new(book.price, 'USD').format + %span.divider-sm + %p.lead.small= "#{book.author.first_name} #{book.author.last_name}" + diff --git a/app/views/books/_sort_drop_down.haml b/app/views/books/_sort_drop_down.haml new file mode 100644 index 0000000..7611732 --- /dev/null +++ b/app/views/books/_sort_drop_down.haml @@ -0,0 +1,13 @@ +.dropdown.width-240 + %a.dropdown-toggle.lead.small{href: "#", 'data-toggle': "dropdown", role: "button", 'aria-haspopup': "true", 'aria-expanded': "false"} + = @active_sorting || t('.newest_first') + %i.fa.fa-angle-down.dropdown-icon.pull-right + %ul.dropdown-menu + %li + = link_to 'Newest first', controller: active_controller, action: action, params: {sort: 'created_at', direction: 'asc', active_sorting: t('books.index.newest_first')} + %li + = link_to 'Popular first', controller: active_controller, action: action, params: {sort: 'reviews_count', direction: 'desc', active_sorting: t('books.index.popular_first')} + %li + = link_to 'Price: Low to high', controller: active_controller, action: action, params: {sort: 'price', direction: 'asc', active_sorting: t('books.index.price_low_to_high')} + %li + = link_to 'Price: High to low', controller: active_controller, action: action, params: {sort: 'price', direction: 'desc', active_sorting: t('books.index.price_high_to_low')} diff --git a/app/views/books/index.html.haml b/app/views/books/index.html.haml index 050922e..0285185 100644 --- a/app/views/books/index.html.haml +++ b/app/views/books/index.html.haml @@ -10,19 +10,7 @@ %h1.general-title-margin Сatalog .hidden-xs.clearfix .dropdowns.pull-right - .dropdown.width-240 - %a.dropdown-toggle.lead.small{href: "#", 'data-toggle': "dropdown", role: "button", 'aria-haspopup': "true", 'aria-expanded': "false"} - = @active_sorting || t('.newest_first') - %i.fa.fa-angle-down.dropdown-icon.pull-right - %ul.dropdown-menu - %li - = link_to 'Newest first', books_path(sort: 'created_at', direction: 'asc', active_sorting: t('.newest_first')) - %li - = link_to 'Popular first', books_path(sort: 'reviews_count', direction: 'desc', active_sorting: t('.popular_first')) - %li - = link_to 'Price: Low to high', books_path(sort: 'price', direction: 'asc', active_sorting: t('.price_low_to_high')) - %li - = link_to 'Price: High to low', books_path(sort: 'price', direction: 'desc', active_sorting: t('.price_high_to_low')) + = dropdown_with_links %ul.list-inline.pt-10.mb-25.mr-240 %li.mr-35 %a.filter-link{href: "/books"} @@ -36,23 +24,9 @@ - paginate(@books) - @books.in_groups_of(4) do |group| .row - - group.each do |book| - - if book - .col-xs-6.col-sm-3 - .general-thumb-wrap - .thumbnail.general-thumbnail - %img.img-shadow.general-thumbnail-img{src: book.cover_url, alt: book.title} - .thumb-hover - .general-thumb-link-wrap - %a.thumb-hover-link.show-book{href: "/books/#{book.id}"} - %i.fa.fa-eye.thumb-icon{'aria-hidden': "true"} - = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') - .general-thumb-info - %p.title= book.title - %p.font-16.in-gold-500= Money.new(book.price, 'USD').format - %span.divider-sm - %p.lead.small= "#{book.author.first_name} #{book.author.last_name}" + = render partial: 'books/books_collection', collection: group, as: :book .text-center.mb-20 - unless @books.last_page? = link_to_next_page(@books, t('.view_more'), class: 'btn btn-primary view-more') + diff --git a/app/views/checkout/address.haml b/app/views/checkout/address.haml index df07f1d..d064930 100644 --- a/app/views/checkout/address.haml +++ b/app/views/checkout/address.haml @@ -16,61 +16,20 @@ %span.checkbox-icon %i.fa.fa-check %span.checkbox-text= t('address.use_shipping_address') - = form_for @order, url: wizard_path, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| - = fields_for :shipping_address_attributes, f.object.shipping_address do |ship_f| - .row - .col-md-5.mb-40 - .visible-xs.visible-sm - %h3.general-subtitle= t('address.shipping_address') - %p.general-info-text= t('address.all_fields_required') - .form-group - = ship_f.label t('address.address_line'), class: 'control-label input-label' - = ship_f.text_field :address_line, class: 'form-control' - .form-group - = ship_f.label t('address.country'), class: 'control-label input-label' - = ship_f.text_field :country, class: 'form-control' - .form-group - = ship_f.label t('address.city'), class: 'control-label input-label' - = ship_f.text_field :city, class: 'form-control' - .form-group - = ship_f.label t('address.zip'), class: 'control-label input-label' - = ship_f.text_field :zip, class: 'form-control' - .form-group - = ship_f.label t('address.phone'), class: 'control-label input-label' - = ship_f.text_field :phone, class: 'form-control' - .col-md-5.col-md-offset-1.mb-60.billing-section - .visible-xs.visible-sm - %h3.general-subtitle Billing Address - = fields_for :billing_address_attributes, f.object.billing_address do |bill_f| - .form-group - = bill_f.label t('address.address_line'), class: 'control-label input-label' - = bill_f.text_field :address_line, class: 'form-control' - .form-group - = bill_f.label t('address.country'), class: 'control-label input-label' - = bill_f.text_field :country, class: 'form-control' - .form-group - = bill_f.label t('address.city'), class: 'control-label input-label' - = bill_f.text_field :city, class: 'form-control' - .form-group - = bill_f.label t('address.zip'), class: 'control-label input-label' - = bill_f.text_field :zip, class: 'form-control' - .form-group - = bill_f.label t('address.phone'), class: 'control-label input-label' - = bill_f.text_field :phone, class: 'form-control' - .general-text-align.mb-60 - %p.in-gold-500.font-18= t('order.order_summary') - %table.general-summary-table - %tr - %td - %p.font-16= t('order.item_total') - %td - %p.font-16= @cart_details.subtotal - %tr - %td - %p.font-16= t('order.order_total') - %td - %p.font-16= @cart_details.total - = f.submit t('common.save_and_continue'), class: 'btn btn-default center-block mb-20 submit-address' + = render partial: 'shared/address', locals: {resource: @order} + .general-text-align.mb-60 + %p.in-gold-500.font-18= t('order.order_summary') + %table.general-summary-table + %tr + %td + %p.font-16= t('order.item_total') + %td + %p.font-16= @cart_details.subtotal + %tr + %td + %p.font-16= t('order.order_total') + %td + %p.font-16= @cart_details.total -# Billing address handling :javascript diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 53056d8..92da186 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -36,20 +36,5 @@ #bestsellers.mb-35 %h3= t('.best_sellers') .row - - @bestsellers.each do |book| - .col-sm-6.col-md-3 - .general-thumb-wrap - .thumbnail.general-thumbnail - %img.img-shadow.general-thumbnail-img{src: book.cover_url, alt: book.title} - .thumb-hover - .general-thumb-link-wrap - %a.thumb-hover-link.show-book{href: "/books/#{book.id}"} - %i.fa.fa-eye.thumb-icon{'aria-hidden': "true"} - = link_to((fa_icon 'shopping-cart', class: 'thumb-icon'), add_to_cart_path(id: book.id, quantity: 1), method: 'post', class: 'thumb-hover-link add-to-cart') + = render partial: 'books/books_collection', collection: @books, as: :book - .mb-35 - .clearfix - %p.pull-right.font-16.in-gold-500= Money.new(book.price).format - %p.title.mr-55= book.title - %span.divider-sm - %p.lead.small= "#{book.author.first_name} #{book.author.last_name}" diff --git a/app/views/orders/index.haml b/app/views/orders/index.haml index d9fe584..8635014 100644 --- a/app/views/orders/index.haml +++ b/app/views/orders/index.haml @@ -3,17 +3,17 @@ %p.lead.small.mb-10.visible-xs= t('.sort_by') .dropdowns.dropdown.general-order-dropdown %a.dropdown-toggle.lead.small{href: "#", 'data-toggle': "dropdown", role: "button", 'aria-haspopup': "true", 'aria-expanded': "false"} - = @orders_sorting || "All" + = @orders_sorting || t('.all') %i.fa.fa-angle-down.dropdown-icon.pull-right %ul.dropdown-menu %li - = link_to 'All', orders_path(sorting: t('.all')) + = link_to t('.all'), orders_path(sorting: t('.all')) %li - = link_to 'In Progress', orders_path(sorting: t('.in_progress')) + = link_to t('.in_progress'), orders_path(sorting: t('.in_progress')) %li - = link_to 'In Delivery', orders_path(sorting: t('.in_delivery')) + = link_to t('.in_delivery'), orders_path(sorting: t('.in_delivery')) %li - = link_to 'Canceled', orders_path(sorting: t('.canceled')) + = link_to t('.canceled'), orders_path(sorting: t('.canceled')) %li - if @orders.empty? %h3.mt-60 diff --git a/app/views/settings/index.haml b/app/views/settings/index.haml index 8cfc8a8..b122e28 100644 --- a/app/views/settings/index.haml +++ b/app/views/settings/index.haml @@ -31,7 +31,7 @@ %i.fa.fa-check %span.checkbox-text= t('address.use_shipping_address') = form_for current_customer, url: settings_path, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| - = fields_for :'customer[shipping_address_attributes]', f.object.shipping_address do |ship_f| + = fields_for :shipping_address_attributes, f.object.shipping_address do |ship_f| .row .col-md-5.mb-40 .visible-xs.visible-sm @@ -55,7 +55,7 @@ .col-md-5.col-md-offset-1.mb-60.billing-section .visible-xs.visible-sm %h3.general-subtitle= t('address.billing_address') - = fields_for :'customer[billing_address_attributes]', f.object.billing_address do |bill_f| + = fields_for :billing_address_attributes, f.object.billing_address do |bill_f| .form-group = bill_f.label t('address.address_line'), class: 'control-label input-label' = bill_f.text_field :address_line, class: 'form-control' @@ -100,7 +100,7 @@ = f.submit t('.update_password'), class: 'btn btn-default mb-20' .col-sm-12 %p.in-gold-500.font-18.mb-25= t('.remove_account') - = form_for current_customer, url: customer_path(current_customer.id), method: :delete do |f| + = form_for current_customer, url: customer_registration_path(current_customer.id), method: :delete do |f| .general-settings-btn = f.submit t('.remove_account'), class: 'btn btn-default mb-20 remove-customer disabled' .form-group.checkbox.general-settings-checkbox diff --git a/app/views/shared/_address.haml b/app/views/shared/_address.haml new file mode 100644 index 0000000..fe490ae --- /dev/null +++ b/app/views/shared/_address.haml @@ -0,0 +1,42 @@ += form_for resource, url: wizard_path, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| + = fields_for :shipping_address_attributes, f.object.shipping_address do |ship_f| + .row + .col-md-5.mb-40 + .visible-xs.visible-sm + %h3.general-subtitle= t('address.shipping_address') + %p.general-info-text= t('address.all_fields_required') + .form-group + = ship_f.label t('address.address_line'), class: 'control-label input-label' + = ship_f.text_field :address_line, class: 'form-control' + .form-group + = ship_f.label t('address.country'), class: 'control-label input-label' + = ship_f.text_field :country, class: 'form-control' + .form-group + = ship_f.label t('address.city'), class: 'control-label input-label' + = ship_f.text_field :city, class: 'form-control' + .form-group + = ship_f.label t('address.zip'), class: 'control-label input-label' + = ship_f.text_field :zip, class: 'form-control' + .form-group + = ship_f.label t('address.phone'), class: 'control-label input-label' + = ship_f.text_field :phone, class: 'form-control' + .col-md-5.col-md-offset-1.mb-40.billing-section + .visible-xs.visible-sm + %h3.general-subtitle Billing Address + = fields_for :billing_address_attributes, f.object.billing_address do |bill_f| + .form-group + = bill_f.label t('address.address_line'), class: 'control-label input-label' + = bill_f.text_field :address_line, class: 'form-control' + .form-group + = bill_f.label t('address.country'), class: 'control-label input-label' + = bill_f.text_field :country, class: 'form-control' + .form-group + = bill_f.label t('address.city'), class: 'control-label input-label' + = bill_f.text_field :city, class: 'form-control' + .form-group + = bill_f.label t('address.zip'), class: 'control-label input-label' + = bill_f.text_field :zip, class: 'form-control' + .form-group + = bill_f.label t('address.phone'), class: 'control-label input-label' + = bill_f.text_field :phone, class: 'form-control' + = f.submit t('common.save_and_continue'), class: 'btn btn-default center-block mb-20 submit-address' diff --git a/config/routes.rb b/config/routes.rb index ab15e4e..a70c28c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,11 +19,7 @@ resources :books, shallow: true do end end - resources :books, only: :index do - collection do - get :index - end - end + resources :books, only: :index devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) From 4c8c6afbcae39c8352de0f73e17b0928ce847bd8 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Thu, 11 Jul 2019 15:25:41 +0300 Subject: [PATCH 7/9] add partial for address --- app/controllers/checkout_controller.rb | 2 + app/controllers/settings_controller.rb | 2 + app/views/checkout/address.haml | 2 +- app/views/settings/index.haml | 54 +++----------------------- app/views/shared/_address.haml | 2 +- config/routes.rb | 3 +- 6 files changed, 14 insertions(+), 51 deletions(-) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 1f342a3..3f52afe 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -1,5 +1,6 @@ class CheckoutController < ApplicationController include Wicked::Wizard + wrap_parameters :order, include: [:shipping_address_attributes, :billing_address_attributes] before_action :current_order, :login_customer steps :address, :delivery, :payment, :confirmation, :complete @@ -21,6 +22,7 @@ def update cart_details case step when :address + binding.pry UpdateOrderAddress.new(@order, address_params).update when :delivery UpdateOrderDelivery.new(@order, delivery_params).update diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index 58cc2bf..f8ab74a 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -1,4 +1,6 @@ class SettingsController < ApplicationController + wrap_parameters :customer + def update if current_customer.update(customer_params) flash[:notice] = t('.settings.index.updated') diff --git a/app/views/checkout/address.haml b/app/views/checkout/address.haml index d064930..4398932 100644 --- a/app/views/checkout/address.haml +++ b/app/views/checkout/address.haml @@ -16,7 +16,7 @@ %span.checkbox-icon %i.fa.fa-check %span.checkbox-text= t('address.use_shipping_address') - = render partial: 'shared/address', locals: {resource: @order} + = render partial: 'shared/address', locals: {resource: @order, url: wizard_path} .general-text-align.mb-60 %p.in-gold-500.font-18= t('order.order_summary') %table.general-summary-table diff --git a/app/views/settings/index.haml b/app/views/settings/index.haml index b122e28..2700a3a 100644 --- a/app/views/settings/index.haml +++ b/app/views/settings/index.haml @@ -29,49 +29,7 @@ %input.checkbox-input.test-class{type: "checkbox", hidden: "true", name: 'billing'} %span.checkbox-icon %i.fa.fa-check - %span.checkbox-text= t('address.use_shipping_address') - = form_for current_customer, url: settings_path, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| - = fields_for :shipping_address_attributes, f.object.shipping_address do |ship_f| - .row - .col-md-5.mb-40 - .visible-xs.visible-sm - %h3.general-subtitle= t('address.shipping_address') - %p.general-info-text= t('address.all_fields_required') - .form-group - = ship_f.label t('address.address_line'), class: 'control-label input-label' - = ship_f.text_field :address_line, class: 'form-control' - .form-group - = ship_f.label t('address.country'), class: 'control-label input-label' - = ship_f.text_field :country, class: 'form-control' - .form-group - = ship_f.label t('address.city'), class: 'control-label input-label' - = ship_f.text_field :city, class: 'form-control' - .form-group - = ship_f.label t('address.zip'), class: 'control-label input-label' - = ship_f.text_field :zip, class: 'form-control' - .form-group - = ship_f.label t('address.phone'), class: 'control-label input-label' - = ship_f.text_field :phone, class: 'form-control' - .col-md-5.col-md-offset-1.mb-60.billing-section - .visible-xs.visible-sm - %h3.general-subtitle= t('address.billing_address') - = fields_for :billing_address_attributes, f.object.billing_address do |bill_f| - .form-group - = bill_f.label t('address.address_line'), class: 'control-label input-label' - = bill_f.text_field :address_line, class: 'form-control' - .form-group - = bill_f.label t('address.country'), class: 'control-label input-label' - = bill_f.text_field :country, class: 'form-control' - .form-group - = bill_f.label t('address.city'), class: 'control-label input-label' - = bill_f.text_field :city, class: 'form-control' - .form-group - = bill_f.label t('address.zip'), class: 'control-label input-label' - = bill_f.text_field :zip, class: 'form-control' - .form-group - = bill_f.label t('address.phone'), class: 'control-label input-label' - = bill_f.text_field :phone, class: 'form-control' - = f.submit t('common.save_and_continue'), class: 'btn btn-default center-block mb-20 submit-address' + = render partial: 'shared/address', locals: {resource: current_customer, url: settings_update_path} #privacy.tab-pane.fade{role: "tabpanel"} .row.mb-60 .col-sm-5 @@ -129,11 +87,11 @@ let billingCheckbox = document.querySelector("input[name=billing]") if (billingCheckbox.checked) { - document.getElementById('customer_billing_address_attributes_address_line').value = document.getElementById('customer_shipping_address_attributes_address_line').value - document.getElementById('customer_billing_address_attributes_country').value = document.getElementById('customer_shipping_address_attributes_country').value - document.getElementById('customer_billing_address_attributes_city').value = document.getElementById('customer_shipping_address_attributes_city').value - document.getElementById('customer_billing_address_attributes_zip').value = document.getElementById('customer_shipping_address_attributes_zip').value - document.getElementById('customer_billing_address_attributes_phone').value = document.getElementById('customer_shipping_address_attributes_phone').value + document.getElementById('billing_address_attributes_address_line').value = document.getElementById('shipping_address_attributes_address_line').value + document.getElementById('billing_address_attributes_country').value = document.getElementById('shipping_address_attributes_country').value + document.getElementById('billing_address_attributes_city').value = document.getElementById('shipping_address_attributes_city').value + document.getElementById('billing_address_attributes_zip').value = document.getElementById('shipping_address_attributes_zip').value + document.getElementById('billing_address_attributes_phone').value = document.getElementById('shipping_address_attributes_phone').value } } diff --git a/app/views/shared/_address.haml b/app/views/shared/_address.haml index fe490ae..af6232f 100644 --- a/app/views/shared/_address.haml +++ b/app/views/shared/_address.haml @@ -1,4 +1,4 @@ -= form_for resource, url: wizard_path, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| += form_for resource, url: url, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| = fields_for :shipping_address_attributes, f.object.shipping_address do |ship_f| .row .col-md-5.mb-40 diff --git a/config/routes.rb b/config/routes.rb index a70c28c..f6e77df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,12 +9,13 @@ get 'cart/increase_quantity' => 'carts#increase_quantity' get 'cart/decrease_quantity' => 'carts#decrease_quantity' post 'cart/apply_coupon' => 'carts#apply_coupon' + put 'settings/update' => 'settings#update' resources :reviews, only: :create resources :carts, only: :index resources :orders, only: [:index, :create] resources :quick_registrations, only: [:index, :create] resources :checkout, only: [:show, :update] - resources :settings, only: [:index, :update, :update_email] + resources :settings, only: [:index] resources :categories, only: :show do resources :books, shallow: true do end From 03794b814773a786fe7766fa999b5ead8ae51911 Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Thu, 11 Jul 2019 16:45:49 +0300 Subject: [PATCH 8/9] add jquery for address and settings --- app/assets/javascripts/address.js | 23 +++++++++++ app/assets/javascripts/settings.js | 9 ++++ app/controllers/settings_controller.rb | 4 +- app/views/checkout/address.haml | 27 ------------ app/views/settings/index.haml | 52 ++++-------------------- app/views/shared/_address.haml | 6 +-- config/locales/en.yml | 2 + spec/controllers/home_controller_spec.rb | 4 +- 8 files changed, 50 insertions(+), 77 deletions(-) create mode 100644 app/assets/javascripts/settings.js diff --git a/app/assets/javascripts/address.js b/app/assets/javascripts/address.js index def1a24..b4d34ae 100644 --- a/app/assets/javascripts/address.js +++ b/app/assets/javascripts/address.js @@ -7,3 +7,26 @@ $(document).ready(function() { } }); }); + +$(document).ready(function() { + $(".submit-address").submit(function(e) { + if ($('[name="billing"]').prop("checked")) { + console.log(1); + $("#billing_address_attributes_address_line").val( + $("#shipping_address_attributes_address_line").val() + ); + $("#billing_address_attributes_city").val( + $("#shipping_address_attributes_city").val() + ); + $("#billing_address_attributes_country").val( + $("#shipping_address_attributes_country").val() + ); + $("#billing_address_attributes_zip").val( + $("#shipping_address_attributes_zip").val() + ); + $("#billing_address_attributes_phone").val( + $("#shipping_address_attributes_phone").val() + ); + } + }); +}); diff --git a/app/assets/javascripts/settings.js b/app/assets/javascripts/settings.js new file mode 100644 index 0000000..ac0ec75 --- /dev/null +++ b/app/assets/javascripts/settings.js @@ -0,0 +1,9 @@ +$(document).ready(function() { + $(".agree-remove").click(function(e) { + if (e.target.checked) { + $(".remove-customer").prop("disabled", false); + } else { + $(".remove-customer").prop("disabled", true); + } + }); +}); diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index f8ab74a..aa74605 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -3,10 +3,10 @@ class SettingsController < ApplicationController def update if current_customer.update(customer_params) - flash[:notice] = t('.settings.index.updated') + flash[:notice] = t('settings.index.updated') sign_in(:customer, current_customer) end - render 'index' + redirect_to settings_path end def customer_params diff --git a/app/views/checkout/address.haml b/app/views/checkout/address.haml index 4398932..6566c02 100644 --- a/app/views/checkout/address.haml +++ b/app/views/checkout/address.haml @@ -30,30 +30,3 @@ %p.font-16= t('order.order_total') %td %p.font-16= @cart_details.total - --# Billing address handling -:javascript - function hideBillingAddress() { - let billingSection = document.querySelector('.billing-section'); - let billingCheckbox = document.querySelector("input[name=billing]") - - billingCheckbox.addEventListener('click', (e) => { - - billingSection.classList.contains('hidden') ? billingSection.classList.remove('hidden') : billingSection.classList.add('hidden') - }) - } - - // hideBillingAddress(); - - function handleBillingAddress(e) { - let billingCheckbox = document.querySelector("input[name=billing]") - - if (billingCheckbox.checked) { - document.getElementById('billing_address_attributes_address_line').value = document.getElementById('shipping_address_attributes_address_line').value - document.getElementById('billing_address_attributes_country').value = document.getElementById('shipping_address_attributes_country').value - document.getElementById('billing_address_attributes_city').value = document.getElementById('shipping_address_attributes_city').value - document.getElementById('billing_address_attributes_zip').value = document.getElementById('shipping_address_attributes_zip').value - document.getElementById('billing_address_attributes_phone').value = document.getElementById('shipping_address_attributes_phone').value - } - } - diff --git a/app/views/settings/index.haml b/app/views/settings/index.haml index 2700a3a..c76102b 100644 --- a/app/views/settings/index.haml +++ b/app/views/settings/index.haml @@ -29,6 +29,7 @@ %input.checkbox-input.test-class{type: "checkbox", hidden: "true", name: 'billing'} %span.checkbox-icon %i.fa.fa-check + %span.checkbox-text= t('address.use_shipping_address') = render partial: 'shared/address', locals: {resource: current_customer, url: settings_update_path} #privacy.tab-pane.fade{role: "tabpanel"} .row.mb-60 @@ -36,9 +37,15 @@ %p.in-gold-500.font-18.mb-25= t('.email') .general-form-md = form_for current_customer, url: settings_path, method: :put do |f| - .form-group.mb-35 + .form-group.mb-30 = f.label t('.email'), class: 'control-label input-label' = f.text_field :email, class: 'form-control' + .form-group.mb-40 + = f.label t('.first_name'), class: 'control-label input-label' + = f.text_field :first_name, class: 'form-control' + .form-group.mb-40 + = f.label t('.last_name'), class: 'control-label input-label' + = f.text_field :last_name, class: 'form-control' .general-text-align = f.submit t('.update_email'), class: 'btn btn-default mb-20' .col-sm-5.col-sm-offset-1 @@ -60,51 +67,10 @@ %p.in-gold-500.font-18.mb-25= t('.remove_account') = form_for current_customer, url: customer_registration_path(current_customer.id), method: :delete do |f| .general-settings-btn - = f.submit t('.remove_account'), class: 'btn btn-default mb-20 remove-customer disabled' + = f.submit t('.remove_account'), class: 'btn btn-default mb-20 remove-customer', disabled: :true .form-group.checkbox.general-settings-checkbox %label.checkbox-label %input.checkbox-input.agree-remove{type: "checkbox", hidden: "true"} %span.checkbox-icon %i.fa.fa-check %span.checkbox-text= t('.accept_checkbox') - - --# Billing address handling -:javascript - function hideBillingAddress() { - let billingSection = document.querySelector('.billing-section'); - let billingCheckbox = document.querySelector("input[name=billing]") - - billingCheckbox.addEventListener('click', (e) => { - - billingSection.classList.contains('hidden') ? billingSection.classList.remove('hidden') : billingSection.classList.add('hidden') - }) - } - - hideBillingAddress(); - - function handleBillingAddress(e) { - let billingCheckbox = document.querySelector("input[name=billing]") - - if (billingCheckbox.checked) { - document.getElementById('billing_address_attributes_address_line').value = document.getElementById('shipping_address_attributes_address_line').value - document.getElementById('billing_address_attributes_country').value = document.getElementById('shipping_address_attributes_country').value - document.getElementById('billing_address_attributes_city').value = document.getElementById('shipping_address_attributes_city').value - document.getElementById('billing_address_attributes_zip').value = document.getElementById('shipping_address_attributes_zip').value - document.getElementById('billing_address_attributes_phone').value = document.getElementById('shipping_address_attributes_phone').value - } - } - --# Billing address handling -:javascript - function confirmRemovingCustomer() { - let removeCustomerCheckbox = document.querySelector('.agree-remove'); - let removeCustomerButton = document.querySelector(".remove-customer") - - removeCustomerCheckbox.addEventListener('click', (e) => { - - removeCustomerButton.classList.contains('disabled') ? removeCustomerButton.classList.remove('disabled') : removeCustomerButton.classList.add('disabled') - }) - } - - confirmRemovingCustomer(); diff --git a/app/views/shared/_address.haml b/app/views/shared/_address.haml index af6232f..8999da2 100644 --- a/app/views/shared/_address.haml +++ b/app/views/shared/_address.haml @@ -1,7 +1,7 @@ -= form_for resource, url: url, method: :put, html: { :onsubmit => "handleBillingAddress(event)" } do |f| += form_for resource, url: url, method: :put, html: {:class => 'submit-address' } do |f| = fields_for :shipping_address_attributes, f.object.shipping_address do |ship_f| .row - .col-md-5.mb-40 + .col-md-5.mb-40.shipping-section .visible-xs.visible-sm %h3.general-subtitle= t('address.shipping_address') %p.general-info-text= t('address.all_fields_required') @@ -22,7 +22,7 @@ = ship_f.text_field :phone, class: 'form-control' .col-md-5.col-md-offset-1.mb-40.billing-section .visible-xs.visible-sm - %h3.general-subtitle Billing Address + %h3.general-subtitle= t('address.billing_address') = fields_for :billing_address_attributes, f.object.billing_address do |bill_f| .form-group = bill_f.label t('address.address_line'), class: 'control-label input-label' diff --git a/config/locales/en.yml b/config/locales/en.yml index 1e811fc..34b967c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -202,6 +202,8 @@ en: address: "Address" privacy: "Privacy" email: "Email" + first_name: "First name" + last_name: "Last name" password: "Password" old_password: "Old Password" new_password: "New Password" diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index dd73a0a..b26abcc 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -34,7 +34,7 @@ it 'create books collection' do get :index - expect(assigns(:bestsellers)).to_not be_nil + expect(assigns(:books)).to_not be_nil end it 'include 4 most ordered books' do @@ -42,7 +42,7 @@ bestsellers = Category.all.select { |category| category.books.count.positive? }.map do |category| category.books.max { |book| book.reviews.count } end[0...4] - expect(assigns(:bestsellers)).to eql(bestsellers) + expect(assigns(:books)).to eql(bestsellers) end end end From dc1a359f2fe13c1c00c4d376209098335340495a Mon Sep 17 00:00:00 2001 From: Mary Banshee Date: Thu, 11 Jul 2019 17:15:33 +0300 Subject: [PATCH 9/9] remove logs --- app/assets/javascripts/address.js | 1 - app/controllers/checkout_controller.rb | 1 - 2 files changed, 2 deletions(-) diff --git a/app/assets/javascripts/address.js b/app/assets/javascripts/address.js index b4d34ae..a7b2153 100644 --- a/app/assets/javascripts/address.js +++ b/app/assets/javascripts/address.js @@ -11,7 +11,6 @@ $(document).ready(function() { $(document).ready(function() { $(".submit-address").submit(function(e) { if ($('[name="billing"]').prop("checked")) { - console.log(1); $("#billing_address_attributes_address_line").val( $("#shipping_address_attributes_address_line").val() ); diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 3f52afe..c272c6c 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -22,7 +22,6 @@ def update cart_details case step when :address - binding.pry UpdateOrderAddress.new(@order, address_params).update when :delivery UpdateOrderDelivery.new(@order, delivery_params).update