From 9caca45fd8ece2d0e27abd667ac6beacfac55e0c Mon Sep 17 00:00:00 2001 From: Daniel Alves + Diego Araújo + Guilherme Rojas Date: Tue, 26 Mar 2013 18:00:40 -0300 Subject: [PATCH] [Mezuro] New validations for readings. --- plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb | 14 +++++++++++++- plugins/mezuro/public/javascripts/validations.js | 20 ++++++++++++++++++++ plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb | 10 ++++++++++ plugins/mezuro/views/mezuro_plugin_reading/_form.html.erb | 2 ++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb b/plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb index bda74c6..2f21085 100644 --- a/plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb +++ b/plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb @@ -4,6 +4,10 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController def new @reading_group_content = profile.articles.find(params[:id]) + + readings = Kalibro::Reading.readings_of @reading_group_content.reading_group_id + @parser="|*|" + @labels_and_grades = readings.map {|reading| "#{reading.label}#{@parser}#{reading.grade}#{@parser}"} end def save @@ -20,6 +24,15 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController def edit @reading_group_content = profile.articles.find(params[:id]) @reading = Kalibro::Reading.find params[:reading_id] + + readings = Kalibro::Reading.readings_of @reading_group_content.reading_group_id + readings = readings.select {|reading| (reading.id != @reading.id)} + @parser="|*|" + @labels_and_grades = readings.map do |reading| + if(reading.id != @reading.id) + "#{reading.label}#{@parser}#{reading.grade}#{@parser}" + end + end end def destroy @@ -32,5 +45,4 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController redirect_to_error_page reading.errors[0].message end end - end diff --git a/plugins/mezuro/public/javascripts/validations.js b/plugins/mezuro/public/javascripts/validations.js index adeee43..cde27d3 100644 --- a/plugins/mezuro/public/javascripts/validations.js +++ b/plugins/mezuro/public/javascripts/validations.js @@ -24,10 +24,30 @@ function validate_new_reading() { var name = jQuery('#reading_label').val(); var grade = jQuery('#reading_grade').val(); var color = jQuery('#reading_color').val(); + if (is_null(name) || is_null(grade) || is_null(color)){ alert("Please fill all fields marked with (*)."); return false; } + + var parser = jQuery('#labels_and_grades').attr('data-parser'); + var labels_and_grades = jQuery('#labels_and_grades').attr('data-list').split(parser); + for (var id = 0; id < labels_and_grades.length; id = id + 2) { + if (labels_and_grades[id] == name) { + alert("This label already exists! Please, choose another one."); + return false; + } + + if (labels_and_grades[id+1] == grade || labels_and_grades[id+1] == grade + ".0") { + alert("This grade already exists! Please, choose another one."); + return false; + } + } + + if (!color.match(/^[a-fA-F0-9]{6}$/)) { + alert("This is not a valid color."); + return false; + } return true; } diff --git a/plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb b/plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb index 55b342e..aef6748 100644 --- a/plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb +++ b/plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb @@ -21,7 +21,11 @@ class MezuroPluginReadingControllerTest < ActionController::TestCase end should 'set variables to create a new reading' do + parser = "|*|" + Kalibro::Reading.expects(:readings_of).with(@content.reading_group_id).returns([@reading]) get :new, :profile => @profile.identifier, :id => @content.id + assert_equal parser, assigns(:parser) + assert_equal ["#{@reading.label}#{parser}#{@reading.grade}#{parser}"], assigns(:labels_and_grades) assert_equal @content.id, assigns(:reading_group_content).id assert_response :success end @@ -44,10 +48,16 @@ class MezuroPluginReadingControllerTest < ActionController::TestCase end should 'set variables to edit a reading' do + parser = "|*|" + another_reading = ReadingFixtures.reading + another_reading.id = 10 + Kalibro::Reading.expects(:readings_of).with(@content.reading_group_id).returns([@reading, another_reading]) Kalibro::Reading.expects(:find).with(@reading.id.to_s).returns(@reading) get :edit, :profile => @profile.identifier, :id => @content.id, :reading_id => @reading.id assert_equal @content.id, assigns(:reading_group_content).id assert_equal @reading, assigns(:reading) + assert_equal parser, assigns(:parser) + assert_equal ["#{another_reading.label}#{parser}#{another_reading.grade}#{parser}"], assigns(:labels_and_grades) assert_response :success end diff --git a/plugins/mezuro/views/mezuro_plugin_reading/_form.html.erb b/plugins/mezuro/views/mezuro_plugin_reading/_form.html.erb index 15bf8d9..9840b0b 100644 --- a/plugins/mezuro/views/mezuro_plugin_reading/_form.html.erb +++ b/plugins/mezuro/views/mezuro_plugin_reading/_form.html.erb @@ -8,4 +8,6 @@ <%= required labelled_form_field _('Color:'), colorpicker_field(:reading, :color) %>Click in the field to change Color +
+

<%= f.submit "Save" %>

-- libgit2 0.21.2