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

Refactoring #13

Merged
merged 11 commits into from Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -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'
Expand Down
25 changes: 21 additions & 4 deletions app/admin/authors.rb
@@ -1,17 +1,34 @@
ActiveAdmin.register Author do
permit_params :first_name, :last_name, :biography
actions :all, except: [:show, :destroy]
actions :all

index do
selectable_column
id_column
column :first_name
column :last_name
actions defaults: true do |author|
link_to t('.delete'), author_path(author), method: :delete, data: { confirm: "#{t('.confirm_part1')} #{author.books.count} #{t('.confirm_part2')}" }
actions defaults: false do |author|
item t('.edit'), edit_admin_author_path(author), class: 'member_link'
item t('.delete'), admin_author_path(author), method: :delete, data: { confirm: "#{t('.confirm_part1')} #{author.books.count} #{t('.confirm_part2')}" }, 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

controller do
def destroy
Author.find(params[:id]).discard
flash[:notice] = 'Author has been deleted.'
redirect_to admin_authors_path
end
end
end
10 changes: 5 additions & 5 deletions app/admin/orders.rb
Expand Up @@ -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
22 changes: 22 additions & 0 deletions app/assets/javascripts/address.js
Expand Up @@ -7,3 +7,25 @@ $(document).ready(function() {
}
});
});

$(document).ready(function() {
$(".submit-address").submit(function(e) {
if ($('[name="billing"]').prop("checked")) {
$("#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()
);
}
});
});
9 changes: 9 additions & 0 deletions 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);
}
});
});
11 changes: 0 additions & 11 deletions 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
36 changes: 2 additions & 34 deletions 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 = t('.newest_first')
@books = BooksSorting.new.sort(params[:sort], params[:direction]).page(params[:page]).per(12)
@active_sorting = params[:active_sorting] || t('.newest_first')
books_count
popular_categories
end
Expand All @@ -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 = t('.newest_first')
books_count
popular_categories
render 'index'
end

def price_low_to_high
@books = Book.order(:price).page(params[:page]).per(12)
books_count
popular_categories
@active_sorting = t('.index.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 = t('.index.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 = t('.index.price_high_to_low')
render 'index'
end

private

def books_count
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/carts_controller.rb
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/categories_controller.rb
@@ -1,7 +1,24 @@
class CategoriesController < ApplicationController
def show
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'
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
7 changes: 2 additions & 5 deletions 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
Expand Down Expand Up @@ -54,7 +55,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
Expand All @@ -77,10 +78,6 @@ def empty_session
session.delete(:order_id)
end

def find_coupon
session[:coupon_id].nil? ? nil : Coupon.find(session[:coupon_id])
end

def login_customer
redirect_to login_path unless customer_signed_in?
end
Expand Down
6 changes: 2 additions & 4 deletions 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
@books = BestSellersBooks.call
end
end
28 changes: 5 additions & 23 deletions 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
Expand All @@ -11,26 +11,8 @@ def create_order
end

def index
@orders = current_customer.orders
@orders_sorting = t('orders.index.all')
end

def in_progress
@orders = current_customer.orders.in_progress
@orders_sorting = t('orders.index.in_progress')
render 'index'
end

def in_delivery
@orders = current_customer.orders.in_delivery
@active_sorting = t('orders.index.in_delivery')
render 'index'
end

def canceled
@orders = current_customer.orders.canceled
@active_sorting = t('orders.index.canceled')
render 'index'
@orders_sorting = params[:sorting]
Copy link

Choose a reason for hiding this comment

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

Can be extracted to service like OrdersFilter

@orders = OrdersFilter.call(current_customer, @orders_sorting)
end

private
Expand All @@ -47,7 +29,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
Expand Down
10 changes: 4 additions & 6 deletions 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] = t('quick_registrations.login.invalid_email')
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
8 changes: 5 additions & 3 deletions app/controllers/settings_controller.rb
@@ -1,13 +1,15 @@
class SettingsController < ApplicationController
wrap_parameters :customer

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
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
7 changes: 7 additions & 0 deletions 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
1 change: 1 addition & 0 deletions app/models/book.rb
Expand Up @@ -12,6 +12,7 @@ class Book < ApplicationRecord
validates :price, presence: true, numericality: true
validates :inventory, presence: true, numericality: { only_integer: true }

scope :latest, -> { kept.order(created_at: :asc).last(3) }
scope :kept, -> { undiscarded.joins(:author).merge(Author.kept) }

def normalize_price
Expand Down
2 changes: 1 addition & 1 deletion app/models/review.rb
Expand Up @@ -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!#$%&'*+-\/=?^_`{|}~]/

Expand Down
11 changes: 11 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions app/services/books_sorting.rb
@@ -0,0 +1,9 @@
class BooksSorting
def initialize(books = Book.all)
@books = books
end

def sort(sort = 'created_at', direction = 'asc')
@books = @books.order("#{sort} #{direction}")
end
end