Skip to content

Commit

Permalink
fixed the n+1 issue and installed rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrobert committed Jun 24, 2020
1 parent 5d72ec4 commit f7a8a47
Show file tree
Hide file tree
Showing 34 changed files with 440 additions and 241 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
10 changes: 6 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ gem 'jbuilder', '~> 2.7'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
# Font-awesome gem
gem "font-awesome-rails"
gem 'font-awesome-rails'
# Devise log in and sign up
gem 'devise'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'rspec-rails', '~> 4.0.0'
end

group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem "bullet"
end

group :test do
Expand All @@ -55,4 +57,4 @@ group :test do
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ GEM
bootsnap (1.4.6)
msgpack (~> 1.0)
builder (3.2.4)
bullet (6.1.0)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
capybara (3.32.2)
addressable
Expand All @@ -81,6 +84,7 @@ GEM
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diff-lcs (1.4.2)
erubi (1.9.0)
ffi (1.13.1)
font-awesome-rails (4.7.0.5)
Expand Down Expand Up @@ -153,6 +157,23 @@ GEM
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-rails (4.0.1)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.9)
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.9.3)
rubyzip (2.3.0)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
Expand Down Expand Up @@ -187,6 +208,7 @@ GEM
turbolinks-source (5.2.0)
tzinfo (1.2.7)
thread_safe (~> 0.1)
uniform_notifier (1.13.0)
warden (1.2.8)
rack (>= 2.0.6)
web-console (4.0.3)
Expand Down Expand Up @@ -214,6 +236,7 @@ PLATFORMS

DEPENDENCIES
bootsnap (>= 1.4.2)
bullet
byebug
capybara (>= 2.15)
devise
Expand All @@ -222,6 +245,7 @@ DEPENDENCIES
listen (~> 3.2)
puma (~> 4.1)
rails (~> 6.0.3, >= 6.0.3.1)
rspec-rails (~> 4.0.0)
sass-rails (>= 6)
selenium-webdriver
spring
Expand Down
Binary file added app/assets/images/header image fill.jpg
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/menu bar.jpg
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/validator error.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :configure_permitted_parameters, if: :devise_controller?

protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
end
75 changes: 37 additions & 38 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
class GroupsController < ApplicationController
before_action :authenticate_user!

def index
@groups = Group.all
end
before_action :authenticate_user!

def new
@group = Group.new
end
def index
@groups = Group.all
end

def show
@group = current_user.groups.find(params[:id])
@transaction = @group.transactions
@total = @transaction.sum(:amount)
end
def new
@group = Group.new
end

def create
@group = current_user.groups.new(fields_arr)
def show
@group = current_user.groups.find(params[:id])
@transaction = @group.transactions
@total = @transaction.sum(:amount)
end

if @group.save
def create
@group = current_user.groups.new(fields_arr)

redirect_to groups_path
else
render :new
if @group.save

end
end
redirect_to groups_path
else
render :new

def edit
@group = current_user.groups.find(params[:id])
end
end

def update
@group = current_user.groups.find(params[:id])
if @group.update(fields_arr)
def edit
@group = current_user.groups.find(params[:id])
end

def update
@group = current_user.groups.find(params[:id])
if @group.update(fields_arr)

redirect_to groups_path, notice: 'Group was successfully updated.'
else
render :edit
end
redirect_to groups_path, notice: 'Group was successfully updated.'
else
render :edit
end
end

# DELETE /groups/1.json
# DELETE /groups/1.json
def destroy
@group = current_user.groups.find(params[:id])
@group.destroy
redirect_back(fallback_location: root_path)

redirect_back(fallback_location: root_path)
end

private
private

def fields_arr
params.require(:group).permit(:name, :icon)
end
def fields_arr
params.require(:group).permit(:name, :icon)
end
end
27 changes: 13 additions & 14 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
class StaticPagesController < ApplicationController
before_action :authenticate_user!

def home
end
before_action :authenticate_user!

def external
user = User.find_by(id: current_user.id)
@external_user_transactions = user.transactions.where(group_id: nil).order(created_at: :desc)
@external_total = @external_user_transactions.sum(:amount)
end

def friends
@users = User.where.not(id: current_user.id)
@expenses = Transaction.where.not(user_id: current_user.id).all
end
def home; end

def external
user = User.find_by(id: current_user.id)
@external_user_transactions = user.transactions.where(group_id: nil).order(created_at: :desc)
@external_total = @external_user_transactions.sum(:amount)
end

def friends
@users = User.where.not(id: current_user.id)
@expenses = Transaction.where.not(user_id: current_user.id).all
end
end
105 changes: 48 additions & 57 deletions app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,82 +1,73 @@
class TransactionsController < ApplicationController
before_action :authenticate_user!

def index
@transaction = current_user.transactions.all
@other_transaction = @transaction.where.not(group_id: nil)
@other_transaction_sorted = @other_transaction.sort_by(&:created_at).reverse

@total = @other_transaction.sum(:amount)

end
before_action :authenticate_user!

def new
@transaction = Transaction.new
end
def index
@transaction = current_user.transactions.all
@other_transaction = @transaction.where.not(group_id: nil)
@other_transaction_sorted = @other_transaction.includes([:group]).sort_by(&:created_at).reverse

def create
@total = @other_transaction.sum(:amount)
end

@transaction = current_user.transactions.new(transaction_params)
def new
@transaction = Transaction.new
end

if @transaction.save

if @transaction.group_id.nil?
def create
@transaction = current_user.transactions.new(transaction_params)

redirect_to external_path, notice: 'Transaction expense was successfully created.'
if @transaction.save

else
if @transaction.group_id.nil?

redirect_to transactions_path, notice: 'Transaction was successfully created.'
redirect_to external_path, notice: 'Transaction expense was successfully created.'

else

end
redirect_to transactions_path, notice: 'Transaction was successfully created.'

else
end

render :new
else

end
end
render :new

def show
@transaction = Transaction.find(params[:id])
end
end

def edit
def show
@transaction = Transaction.find(params[:id])
end

@transaction = current_user.transactions.find(params[:id])
end
def edit
@transaction = current_user.transactions.find(params[:id])
end

def update
@transaction = current_user.transactions.find(params[:id])
if @transaction.update(transaction_params)

if @transaction.group_id.nil?

redirect_to external_path, notice: 'external transaction was successfully updated.'
else
redirect_to transactions_path, notice: 'transaction was successfully updated.'
end


def update
@transaction = current_user.transactions.find(params[:id])
if @transaction.update(transaction_params)

if @transaction.group_id.nil?

redirect_to external_path, notice: 'external transaction was successfully updated.'
else
render :edit
redirect_to transactions_path, notice: 'transaction was successfully updated.'
end
end

def destroy

else
render :edit
end
end

def destroy
@transaction = current_user.transactions.find(params[:id])
@transaction.destroy
redirect_back(fallback_location: root_path)
redirect_back(fallback_location: root_path)
end


end

private
private

def transaction_params
params.require(:transaction).permit(:name, :amount, :group_id)
end

def transaction_params
params.require(:transaction).permit(:name, :amount, :group_id)
end
end
Loading

0 comments on commit f7a8a47

Please sign in to comment.