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

Codeclimate finale #24

Merged
merged 4 commits into from
Nov 22, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions app/assets/stylesheets/professionPicker.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
$('#general_info_industry').on('change', function(e) {
const options = {
'Creators': ["Brand Owner", "Designer", "Other Creator"],
'Services': ["Model", "Photographer", "Sales", "Marketing", "Retail", "Visual", "Content Creator", "Blogger", "Influencer", "Forecasting", "Finances", "Other Services"],
'Makers': ["Manufacturing", "Materials", "Other Makers"]
};

const creator_array=["Brand Owner", "Designer", "Other Creator"]
const service_array=["Model", "Photographer", "Sales", "Marketing", "Retail", "Visual", "Content Creator", "Blogger", "Influencer", "Forecasting", "Finances", "Other Services"]
const maker_array=["Manufacturing", "Materials", "Other Makers"]
var values = $(this).val()
console.log("Four friends")
$("#general_info_job_name option").hide() //hide all options
//$('#job_name').selectpicker('deselectAll') //if want to remove all selcted optn
if (values =='Creators') {
$('#general_info_job_name').empty();
$('#general_info_job_name').append('<option>' +'Select One'+ '</option>');
for (var i = 0; i < 3; i++) {
//$("#profession option[value="' + creator_array[i] + '"]").show()
$('#general_info_job_name').append('<option>' +creator_array[i]+ '</option>');
}
}else if (values =='Services') {
$('#general_info_job_name').empty();
$('#general_info_job_name').append('<option>' +'Select One'+ '</option>');
for (var i = 0; i < 12; i++) {
//$("#profession option[value="' + creator_array[i] + '"]").show()
$('#general_info_job_name').append('<option>' +service_array[i]+ '</option>');
}
}else if (values =='Makers') {
$('#general_info_job_name').empty();
$('#general_info_job_name').append('<option>' +'Select One'+ '</option>');
for (var i = 0; i < 3; i++) {
//$("#profession option[value="' + creator_array[i] + '"]").show()
$('#general_info_job_name').append('<option>' +maker_array[i]+ '</option>');
}
const selectedValue = $(this).val();
const jobDropdown = $('#general_info_job_name');

// Clear and reset the dropdown
jobDropdown.empty();
if (options[selectedValue]) {
jobDropdown.append('<option>Select One</option>'); // Default option
options[selectedValue].forEach(option => {
jobDropdown.append(`<option>${option}</option>`);
});
} else {
// If no valid selection, show a default option or hide all
jobDropdown.append('<option>Select a valid category</option>');
}
else {
// dont show anything
$("#general_info_job_name option").hide()
//$("#general_info_job_name option").show() //show all options
}
//$('#job_name').selectpicker('refresh'); //refresh selctpicker
});
});
223 changes: 110 additions & 113 deletions app/controllers/admin/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,147 +5,144 @@ class AdminController < ApplicationController


def landing
#GeneralInfo.load_Job_File #No longer needed- Job file is loaded in initializer
@hasPermission = false
print('Current USer Key is: ')
print(session[:current_user_key])
if (session[:current_user_key] != nil && GeneralInfo.exists?(:userKey => session[:current_user_key]))
general_info = GeneralInfo.find_by(userKey: session[:current_user_key])
@hasPermission = general_info.is_admin
end
@hasPermission = check_admin_permission
if(@hasPermission == false)
redirect_to "/login_info/login"
end

end

def create

@hasPermission = false
if (session[:current_user_key] != nil && GeneralInfo.exists?(:userKey => session[:current_user_key]))
general_info = GeneralInfo.find_by(userKey: session[:current_user_key])
@hasPermission = general_info.is_admin
end
# if(@hasPermission == false)
# redirect_to "/login_info/login"
# end

@hasPermission = check_admin_permission
@potentialJob = params[:job_name].to_s


if(@potentialJob != nil && @potentialJob != "")

if @potentialJob.present?
@potentialJob = @potentialJob.parameterize(separator: '_').upcase_first
if(GeneralInfo.check_Job?(@potentialJob) == false)
GeneralInfo.create_Job(@potentialJob)
init = @potentialJob.constantize.new
flash.now[:notice] = @potentialJob.titleize + " has been created.\nCurrent jobs are : " + GeneralInfo.see_Jobs.join(",")
else
flash.now[:notice] = params[:job_name].titleize + " already exists.\nCurrent jobs are : " + GeneralInfo.see_Jobs.join(",")
end
elsif(params[:job_name] == "")
if GeneralInfo.check_Job?(@potentialJob) == false
GeneralInfo.create_Job(@potentialJob)
flash.now[:notice] = "#{@potentialJob.titleize} has been created.\nCurrent jobs are : #{GeneralInfo.see_Jobs.join(",")}"
else
flash.now[:notice] = "#{params[:job_name].titleize} already exists.\nCurrent jobs are : #{GeneralInfo.see_Jobs.join(",")}"
end
elsif @potentialJob.blank?
flash.now[:notice] = "Please enter a non-empty value."
end

# else
# Probably fetching page


end

def edit

@hasPermission = false
if (session[:current_user_key] != nil && GeneralInfo.exists?(:userKey => session[:current_user_key]))
general_info = GeneralInfo.find_by(userKey: session[:current_user_key])
@hasPermission = general_info.is_admin
@hasPermission = check_admin_permission
if @hasPermission == false
redirect_to "/login_info/login"
end
if(@hasPermission == false)
redirect_to "/login_info/login"
@jobs = GeneralInfo.see_Jobs.map(&:titleize)

if params[:job_name].present? && params[:attr_action].present? && params[:attr_name].present?
@job = params[:job_name].parameterize(separator: '_').upcase_first
@job_Obj = @job.constantize
@action = params[:attr_action]
@attr = params[:attr_name]

if GeneralInfo.check_Job?(@job)
handle_attribute_action
else
flash.now[:notice] = "Job #{@job.titleize} not found."
end
else
flash.now[:notice] = "Error: One or more empty fields"
end
end

@jobs = Array.new

GeneralInfo.see_Jobs.each do |i|
@jobs.push i.titleize
def delete
@hasPermission = check_admin_permission
if @hasPermission == false
redirect_to "/login_info/login"
end

if(params[:job_name] != nil && params[:attr_action] != nil && params[:attr_name] != nil)
@job = params[:job_name].parameterize(separator: '_').upcase_first
@job_Obj = params[:job_name].parameterize(separator: '_').upcase_first.constantize
@action = params[:attr_action]
@attr = params[:attr_name]

if(GeneralInfo.check_Job?(@job))
if(@action == 'Add' && @job_Obj.view_Attr().include?(@attr) == false)
@job_Obj.add_Attr(@attr)
flash.now[:notice] = "Attribute " + @attr + " added to " + @job.to_s + "---" + @job + "\'s current attributes are " + @job_Obj.view_Attr.inspect
x = @job_Obj.view_Attr().find_index(@attr)
GeneralInfo.find_each do |user|
if(user[:job_name] == @job_Obj.name)
newAttr = user[:job_attr]
newAttr[x] = "Default"
user.update_attribute(:job_attr, newAttr)
end
end
elsif(@action == 'Remove' && @job_Obj.view_Attr().include?(@attr))
origLoc = @job_Obj.view_Attr().find_index(@attr)
@job_Obj.delete_Attr(@attr)
flash.now[:notice] = "Attribute " + @attr + " removed from " + @job + "---" + @job + "\'s current attributes are " + @job_Obj.view_Attr.inspect
attrLength = @job_Obj.view_Attr().length
GeneralInfo.find_each do |user|
if(user[:job_name] == @job_Obj.name)
x = origLoc
newAttr = user[:job_attr]
while (x < attrLength)
newAttr[x] = newAttr[x + 1]
x += 1
end
newAttr.delete(attrLength)
user.update_attribute(:job_attr, newAttr)
end
end
else
flash.now[:notice] = "Attribute " + @attr + " already in " + @job.to_s + "---" + @job + "\'s current attributes are " + @job_Obj.view_Attr.inspect
end

else
flash.now[:notice] = "Job " + @job.titleize + " not found."
end
else
flash.now[:notice] = "Error: One or more empty fields"

@jobs = GeneralInfo.see_Jobs.map(&:titleize)

if params[:job_name].present?
@potentialJob = params[:job_name].to_s.parameterize(separator: '_').upcase_first
if GeneralInfo.check_Job?(@potentialJob)
GeneralInfo.delete_Job(@potentialJob)
flash.now[:notice] = "#{params[:job_name].titleize} has been deleted."
end
end
end

def delete
private
# Extracted method to check admin permission
def check_admin_permission
if session[:current_user_key].present? && GeneralInfo.exists?(userKey: session[:current_user_key])
general_info = GeneralInfo.find_by(userKey: session[:current_user_key])
general_info.is_admin
else
false
end
end

@hasPermission = false
if (session[:current_user_key] != nil && GeneralInfo.exists?(:userKey => session[:current_user_key]))
general_info = GeneralInfo.find_by(userKey: session[:current_user_key])
@hasPermission = general_info.is_admin
# Extracted method to handle the Add/Remove actions for attributes
def handle_attribute_action
if @action == 'Add'
add_attribute
elsif @action == 'Remove'
remove_attribute
else
flash.now[:notice] = "Attribute #{@attr} already in #{@job.titleize}---#{@job}'s current attributes are #{@job_Obj.view_Attr.inspect}"
end
if(@hasPermission == false)
redirect_to "/login_info/login"
end

# Add attribute to job
def add_attribute
if @job_Obj.view_Attr.include?(@attr)
flash.now[:notice] = "Attribute #{@attr} already in #{@job.titleize}---#{@job.titleize}'s current attributes are #{@job_Obj.view_Attr.inspect}"
else
@job_Obj.add_Attr(@attr)
update_users_job_attributes
flash.now[:notice] = "Attribute #{@attr} added to #{@job.titleize}---#{@job.titleize}'s current attributes are #{@job_Obj.view_Attr.inspect}"
end
end

@jobs = Array.new


if(params[:job_name] != nil)
@potentialJob = params[:job_name].to_s.parameterize(separator: '_').upcase_first
if(GeneralInfo.check_Job?(@potentialJob))
GeneralInfo.delete_Job(@potentialJob)
flash.now[:notice] = params[:job_name].titleize + " has been deleted."
# Remove attribute from job
def remove_attribute
if @job_Obj.view_Attr.include?(@attr)
original_location = @job_Obj.view_Attr.find_index(@attr)
@job_Obj.delete_Attr(@attr)
update_users_job_attributes_after_removal(original_location)
flash.now[:notice] = "Attribute #{@attr} removed from #{@job.titleize}---#{@job.titleize}'s current attributes are #{@job_Obj.view_Attr.inspect}"
else
flash.now[:notice] = "Attribute #{@attr} not found in #{@job.titleize}."
end
end

#GeneralInfo.delete_Job_From_File(params[:job_name])
end
#else
# Probably fetching page
# Extracted method to update job attributes for all users
def update_users_job_attributes
GeneralInfo.find_each do |user|
if user[:job_name] == @job_Obj.name
Rails.logger.debug("user.job_attr is #{user.job_attr.class}") # Log the class of job_attr
if user.job_attr.is_a?(Array)
user.job_attr += [@attr]
elsif user.job_attr.is_a?(Hash)
user.job_attr[@attr] = "Default" # Or appropriate logic for a hash
else
Rails.logger.error("Unexpected type for job_attr")
end
user.save
end
end
end

GeneralInfo.see_Jobs.each do |i|
@jobs.push i.titleize
# Extracted method to update job attributes for users after removing an attribute
def update_users_job_attributes_after_removal(original_location)
GeneralInfo.find_each do |user|
if user[:job_name] == @job_Obj.name
new_attributes = user[:job_attr]
if new_attributes.is_a?(Array)
new_attributes.delete_at(original_location)
user.update_attribute(:job_attr, new_attributes)
else
Rails.logger.error("Expected Array for job_attr, but got #{new_attributes.class}")
end
end
end

end
end
end
Loading
Loading