Skip to content
Snippets Groups Projects
Commit 092737a5 authored by Dore, Kieran D (UG - Comp Sci & Elec Eng)'s avatar Dore, Kieran D (UG - Comp Sci & Elec Eng)
Browse files

Comented controllers

parent ee109bb3
No related branches found
No related tags found
No related merge requests found
class ContactController < ApplicationController class ContactController < ApplicationController
def contact def contact # Display the contact view
if session[:user_id].blank? if session[:user_id].blank? # If we have no session variable for the user_id, then the user has not signed in
redirect_to homepage_path redirect_to root_path
else else # Otherwise, we set up the variables relevant to the page
@username = User.find(session[:user_id]).name @username = User.find(session[:user_id]).name
@pagename = t('.page_title') @pagename = t('.page_title')
@email = User.find(session[:user_id]).email @email = User.find(session[:user_id]).email
end end
end end
def sendmessage def sendmessage # Method for sending the message
if params[:email].blank? if params[:email].blank? # If the email was blank, then stay on page and display a flash alert
flash[:alert] = t('.contact.blank_email') flash[:alert] = t('.contact.blank_email')
redirect_to contact_path redirect_to contact_path
elsif params[:message].blank? elsif params[:message].blank? # If the message is blank, then stay on the page and display a flash alert
flash[:alert] = t('.contact.blank_message') flash[:alert] = t('.contact.blank_message')
redirect_to contact_path redirect_to contact_path
elsif params[:email] =~ URI::MailTo::EMAIL_REGEXP elsif params[:email] =~ URI::MailTo::EMAIL_REGEXP # If the email is in the correct format, then we send the message and go to home page
ContactMailer.contact_email(params[:email], params[:message]).deliver_now ContactMailer.contact_email(params[:email], params[:message]).deliver_now
redirect_to homepage_path redirect_to homepage_path
else else # Otherwise stay on page and display a flash alert
flash[:alert] = t('.contact.invalid') flash[:alert] = t('.contact.invalid')
redirect_to contact_path redirect_to contact_path
end end
......
class HomeController < ApplicationController class HomeController < ApplicationController
def new_lst def new_lst # Method for creating new lists
if List.where(name: params[:name]).exists? if List.user(session[:user_id]).where(name: params[:name]).exists? # If the user already has a list of the same name, then we display a relevant message
redirect_to homepage_path alert: t('.homepage.existing', name: params[:name]) redirect_to homepage_path alert: t('.homepage.existing', name: params[:name])
else else # If there is no pre-existing list, then we create one
user = User.find(session[:user_id]) user = User.find(session[:user_id])
List.create(name: params[:name], user: user) List.create(name: params[:name], user: user)
redirect_to homepage_path redirect_to homepage_path
end end
end end
def del_lst def del_lst # Method for deleting a list
List.destroy(params[:list_id]) unless List.find(params[:list_id]).blank? List.destroy(params[:list_id]) unless List.find(params[:list_id]).blank? # If the list_id is not blank, then we delete the relevant list
redirect_to homepage_path redirect_to homepage_path
end end
def login; def login; # Method for login page view
@username = '' @username = ''
@pagename = t('.page_title') @pagename = t('.page_title')
end end
def logincheck def logincheck # Here we check if the login info is correct
if params[:email].blank? || params[:password].blank? if params[:email].blank? || params[:password].blank? # If the user left any fields blank, then we redirect back to the login page and display a relevant message
redirect_to root_path, alert: t('.login.blank') redirect_to root_path, alert: t('.login.blank')
elsif User.where(email: params[:email], password: params[:password]).exists? elsif User.where(email: params[:email], password: params[:password]).exists? # If we can find a user with a matching email and password
session[:user_id] = User.find_by(email: params[:email]).id session[:user_id] = User.find_by(email: params[:email]).id # Then we set the user_id session variable and then go to the homepage
redirect_to homepage_path redirect_to homepage_path
else else # If we cannot find a matching user, then we redirect back to the login page and display the appropriate alert
redirect_to root_path, alert: t('.login.incorrect') redirect_to root_path, alert: t('.login.incorrect')
end end
end end
def homepage def homepage # Method for displaying home page
if session[:user_id].nil? if session[:user_id].nil? # If we have no session variable for the user_id, then the user has not signed in
redirect_to root_path redirect_to root_path # Redirect back to the login page
else else # Otherwise, we go ahead set the correct variables and show the homepage view
user = User.find(session[:user_id]) user = User.find(session[:user_id])
@user_id = user.id
@username = user.name @username = user.name
@pagename = t('.page_title') @pagename = t('.page_title')
@user_id = user.id
end end
end end
def new_acc def new_acc # Method for handling creation of a new account
if User.where(email: params[:email]).exists? if params[:email].blank? || params[:name].blank? || params[:password].blank? # If the user left fields blank, then we redirect to login page with a message
redirect_to root_path, alert: t('.login.in_use')
elsif params[:email].blank? || params[:name].blank? || params[:password].blank?
redirect_to root_path, alert: t('.login.blank') redirect_to root_path, alert: t('.login.blank')
elsif params[:email] =~ URI::MailTo::EMAIL_REGEXP elsif User.where(email: params[:email]).exists? # If the email is already taken, then redirect back to login page with the appropriate message
redirect_to root_path, alert: t('.login.in_use')
elsif params[:email] =~ URI::MailTo::EMAIL_REGEXP # If the email matches the standard email regex, then we create the account, and set the session variable
session[:user_id] = User.create(email: params[:email], name: params[:name], password: params[:password]).id session[:user_id] = User.create(email: params[:email], name: params[:name], password: params[:password]).id
redirect_to homepage_path redirect_to homepage_path
else else # Otherwise the email was in an invalid faormat, so we go back to the login page and display a message
redirect_to root_path, alert: t('.login.invalid') redirect_to root_path, alert: t('.login.invalid')
end end
end end
......
class ListviewController < ApplicationController class ListviewController < ApplicationController
def change_prods def change_prods # Method for changing specific values in the product object
changes = Hash[] changes = Hash[] # Set up a hashmap to keep track of changes
(0..params[:product_id].length - 1).each do |i| (0..params[:product_id].length - 1).each do |i| # Loop through each product and keep track of the relevant changes
change = Hash[] change = Hash[]
change[:quantity] = params[:quantity][i] unless params[:quantity].nil? || params[:quantity][i].blank? || Integer(params[:quantity][i]) < 1 change[:quantity] = params[:quantity][i] unless params[:quantity].nil? || params[:quantity][i].blank? || Integer(params[:quantity][i]) < 1
change[:acquired] = params[:acquired][i] unless params[:acquired].nil? || params[:acquired][i].blank? change[:acquired] = params[:acquired][i] unless params[:acquired].nil? || params[:acquired][i].blank?
change[:name] = params[:name][i] unless params[:name].nil? || params[:name][i].blank? change[:name] = params[:name][i] unless params[:name].nil? || params[:name][i].blank?
changes[Integer(params[:product_id][i])] = change changes[Integer(params[:product_id][i])] = change # Add these changes to the original hashmap
end end
Product.update(changes.keys, changes.values) Product.update(changes.keys, changes.values) # Apply all changes from hashmap to database and then reload the page
redirect_to listview_path redirect_to listview_path
end end
def del_prod def del_prod # Method for deleting a product
Product.destroy(params[:product_id]) Product.destroy(params[:product_id]) # Destroy the product and reload page
redirect_to listview_path redirect_to listview_path
end end
def listview def listview # Method for displaying the list page
if session[:list_id].nil? if session[:user_id].nil? # If we have no session variable for the user_id, then the user has not signed in
redirect_to homepage_path redirect_to root_path
else else # Otherwise we set the relevant variables and render the listview page
session[:list_id] = params[:list_id] if params[:list_id] session[:list_id] = params[:list_id]
@list_id = session[:list_id]
@username = User.find(session[:user_id]).name @username = User.find(session[:user_id]).name
@pagename = List.find(session[:list_id]).name @pagename = List.find(session[:list_id]).name
@list_id = session[:list_id]
end end
end end
def new_prod def new_prod # Method for creating a new product
valid_quantity = (!params[:quantity].blank? && Integer(params[:quantity]) >= 1) || params[:quantity].blank? valid_quantity = (!params[:quantity].blank? && Integer(params[:quantity]) >= 1) || params[:quantity].blank?
valid_name = !params[:name].blank? valid_name = !params[:name].blank?
if valid_quantity && valid_name if valid_quantity && valid_name # Check if the quantity and name are valid, if not, then we do nothing
if Product.where(list_id: params[:list_id], name: params[:name]).exists? if Product.list(params[:list_id]).where(name: params[:name]).exists? # If a product in the list with the same name exists, just increment the quantity
prod = Product.find_by(list_id: params[:list_id], name: params[:name]) prod = Product.find_by(list_id: params[:list_id], name: params[:name])
quant = !params[:quantity].nil? && params[:quantity].number? ? Integer(params[:quantity]) : 1 quant = !params[:quantity].nil? && params[:quantity].number? ? Integer(params[:quantity]) : 1
Product.update(prod.id, quantity: [1, prod.quantity].max + quant) Product.update(prod.id, quantity: [1, prod.quantity].max + quant)
else else # Otherwise create a new product with the specified values
@prod = Product.create(name: params[:name], quantity: !params[:quantity].blank? ? params[:quantity] : 1, acquired: 0, @prod = Product.create(name: params[:name], quantity: !params[:quantity].blank? ? params[:quantity] : 1, acquired: 0,
list: List.find(params[:list_id])) list: List.find(params[:list_id]))
end end
...@@ -53,7 +53,7 @@ class ListviewController < ApplicationController ...@@ -53,7 +53,7 @@ class ListviewController < ApplicationController
end end
class String class String
def number? def number? # Method for checking if a string can parsed to a number
true if Float(self) true if Float(self)
rescue StandardError rescue StandardError
false false
......
%br %br
%br %br
- List.all.user(@user_id).each do |list| - List.user(@user_id).each do |list|
.listCon .listCon
%p{class: "text"}= list.name %p{class: "text"}= list.name
.listButtons .listButtons
= form_tag listview_path, method: 'get' do = form_tag listview_path, method: 'get' do
%input{type: "hidden", value: list.id, name: :list_id}
%input{type: "submit", value: t('.view'), class: "button1"} %input{type: "submit", value: t('.view'), class: "button1"}
= button_tag(class: "iconButton", onclick: "removeList(" + String(list.id) + ", this)") do = button_tag(class: "iconButton", onclick: "removeList(" + String(list.id) + ", this)") do
......
- Product.list(@list_id).all.each do |product| - Product.list(@list_id).each do |product|
.prodCon .prodCon
%input{type: "text", class: "inputTextAlt", value: product.name, required: true, onchange: "updateProduct(this, " + String(product.id) + ", 'name')"} %input{type: "text", class: "inputTextAlt", value: product.name, required: true, onchange: "updateProduct(this, " + String(product.id) + ", 'name')"}
%input{type: "number", class: "inputTextAlt", style: "max-width: 100px", value: product.quantity, min: 1, onchange: "updateProduct(this, " + String(product.id) + ", 'quantity')"} %input{type: "number", class: "inputTextAlt", style: "max-width: 100px", value: product.quantity, min: 1, onchange: "updateProduct(this, " + String(product.id) + ", 'quantity')"}
......
...@@ -2,7 +2,7 @@ require 'test_helper' ...@@ -2,7 +2,7 @@ require 'test_helper'
class ListviewControllerTest < ActionDispatch::IntegrationTest class ListviewControllerTest < ActionDispatch::IntegrationTest
test 'should get listview' do test 'should get listview' do
get listview_path get listview_path params: {list_id: List.first.id}
assert_response :redirect assert_response :redirect
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment