From 5f33e87dbd3cc444c40fbacc6497492778f9e67e Mon Sep 17 00:00:00 2001 From: bmv root Date: Fri, 5 May 2017 15:30:11 -0400 Subject: [PATCH 1/2] Add grade form validation --- .../app/assets/javascripts/application.js | 1 + .../app/assets/javascripts/grades.coffee | 3 - Pearlception/app/assets/javascripts/grades.js | 64 +++++++++++++++++++ .../app/controllers/grades_controller.rb | 3 +- Pearlception/app/models/grade.rb | 7 ++ Pearlception/app/views/grades/new.html.erb | 4 +- 6 files changed, 76 insertions(+), 6 deletions(-) delete mode 100644 Pearlception/app/assets/javascripts/grades.coffee create mode 100644 Pearlception/app/assets/javascripts/grades.js diff --git a/Pearlception/app/assets/javascripts/application.js b/Pearlception/app/assets/javascripts/application.js index 1f319ca..365b38d 100644 --- a/Pearlception/app/assets/javascripts/application.js +++ b/Pearlception/app/assets/javascripts/application.js @@ -16,4 +16,5 @@ //= require bootstrap/bootstrap-rails-tooltip //= require bootstrap/bootstrap-rails-popover //= require Chart +//= require grades //= require jquery-ui diff --git a/Pearlception/app/assets/javascripts/grades.coffee b/Pearlception/app/assets/javascripts/grades.coffee deleted file mode 100644 index 24f83d1..0000000 --- a/Pearlception/app/assets/javascripts/grades.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/Pearlception/app/assets/javascripts/grades.js b/Pearlception/app/assets/javascripts/grades.js new file mode 100644 index 0000000..e0b5388 --- /dev/null +++ b/Pearlception/app/assets/javascripts/grades.js @@ -0,0 +1,64 @@ +$().ready(function(){ + console.log("Ready") + document.getElementById('grade-form').onsubmit = function(){ + var form_elements = getGradeFormElements() + if(elementsAreEmpty(form_elements)){ + alert("There is an empty field") + return false + } + if(!shortNameIsValid(form_elements.Short_name)){ + alert("Short name must only contain alphanumeric characters and be <= 5 characters") + return false + } + if(!measurementsAreNumbers(form_elements)){ + alert("An entered width,height,or length is not valid.(must be of form 1,1.1,0.1)") + return false + } + return true + } +}) + +function getGradeFormElements(){ + var form = document.getElementById('grade-form') + return { + Full_name: form["grade[Full_name]"].value, + Short_name: form["grade[Short_name]"].value, + Width_max: form["grade[Width_max]"].value, + Width_min: form["grade[Width_min]"].value, + Height_max: form["grade[Height_max]"].value, + Height_min: form["grade[Height_min]"].value, + Length_max: form["grade[Length_max]"].value, + Length_min: form["grade[Length_min]"].value + } +} + +function elementsAreEmpty(elements){ + for(key in elements){ + if(elements[key] == ""){ + return true + } + } + return false +} + +function shortNameIsValid(shortName){ + var shortNameRegexp = new RegExp(/[a-zA-Z0-9]{1,5}/) + if(shortName.match(shortNameRegexp) == null || shortName.length > 5){ + return false + } + return true +} + +function measurementsAreNumbers(elements){ + var regexp = new RegExp(/[0-9]+(\.[0-9]+){0,1}/) + if(elements.Height_max.match(regexp) == null || elements.Height_min.match(regexp) == null){ + return false + } + if(elements.Width_max.match(regexp) == null || elements.Width_min.match(regexp) == null){ + return false + } + if(elements.Length_max.match(regexp) == null || elements.Length_min.match(regexp) == null){ + return false + } + return true +} diff --git a/Pearlception/app/controllers/grades_controller.rb b/Pearlception/app/controllers/grades_controller.rb index 5026ce9..8155218 100644 --- a/Pearlception/app/controllers/grades_controller.rb +++ b/Pearlception/app/controllers/grades_controller.rb @@ -14,7 +14,8 @@ class GradesController < ApplicationController end def create - Grade.create(grades_params) + grade = Grade.new(grades_params) + grade.save redirect_to '/grades' end diff --git a/Pearlception/app/models/grade.rb b/Pearlception/app/models/grade.rb index 734eff4..7f52207 100644 --- a/Pearlception/app/models/grade.rb +++ b/Pearlception/app/models/grade.rb @@ -1,3 +1,10 @@ class Grade < ApplicationRecord self.primary_key = 'Short_name' + + def initialize(params) + super + self.Volume_max = (self.Height_max.to_f + self.Width_max.to_f + self.Length_max.to_f).to_s + self.Volume_min = (self.Height_min.to_f + self.Width_min.to_f + self.Length_min.to_f).to_s + end + end diff --git a/Pearlception/app/views/grades/new.html.erb b/Pearlception/app/views/grades/new.html.erb index a57d277..44638e0 100644 --- a/Pearlception/app/views/grades/new.html.erb +++ b/Pearlception/app/views/grades/new.html.erb @@ -62,14 +62,14 @@
-
+ <%=csrf_meta_tag%>
- +
From e273a69c22b52c8c4e84926a8ab3dbb16a4c0ff6 Mon Sep 17 00:00:00 2001 From: bmv root Date: Thu, 11 May 2017 12:59:04 -0400 Subject: [PATCH 2/2] Fixed links to homepage' --- Pearlception/app/views/dashboard/index.html.erb | 4 ++-- Pearlception/app/views/grades/index.html.erb | 4 ++-- Pearlception/app/views/statistics/index.html.erb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Pearlception/app/views/dashboard/index.html.erb b/Pearlception/app/views/dashboard/index.html.erb index 87791de..c0b9180 100644 --- a/Pearlception/app/views/dashboard/index.html.erb +++ b/Pearlception/app/views/dashboard/index.html.erb @@ -10,7 +10,7 @@ - Pearlception + Pearlception