Commit 9caca45fd8ece2d0e27abd667ac6beacfac55e0c
Committed by
Paulo Meireles
1 parent
09f2a25b
Exists in
master
and in
29 other branches
[Mezuro] New validations for readings.
Showing
4 changed files
with
45 additions
and
1 deletions
Show diff stats
plugins/mezuro/controllers/myprofile/mezuro_plugin_reading_controller.rb
... | ... | @@ -4,6 +4,10 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController |
4 | 4 | |
5 | 5 | def new |
6 | 6 | @reading_group_content = profile.articles.find(params[:id]) |
7 | + | |
8 | + readings = Kalibro::Reading.readings_of @reading_group_content.reading_group_id | |
9 | + @parser="|*|" | |
10 | + @labels_and_grades = readings.map {|reading| "#{reading.label}#{@parser}#{reading.grade}#{@parser}"} | |
7 | 11 | end |
8 | 12 | |
9 | 13 | def save |
... | ... | @@ -20,6 +24,15 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController |
20 | 24 | def edit |
21 | 25 | @reading_group_content = profile.articles.find(params[:id]) |
22 | 26 | @reading = Kalibro::Reading.find params[:reading_id] |
27 | + | |
28 | + readings = Kalibro::Reading.readings_of @reading_group_content.reading_group_id | |
29 | + readings = readings.select {|reading| (reading.id != @reading.id)} | |
30 | + @parser="|*|" | |
31 | + @labels_and_grades = readings.map do |reading| | |
32 | + if(reading.id != @reading.id) | |
33 | + "#{reading.label}#{@parser}#{reading.grade}#{@parser}" | |
34 | + end | |
35 | + end | |
23 | 36 | end |
24 | 37 | |
25 | 38 | def destroy |
... | ... | @@ -32,5 +45,4 @@ class MezuroPluginReadingController < MezuroPluginMyprofileController |
32 | 45 | redirect_to_error_page reading.errors[0].message |
33 | 46 | end |
34 | 47 | end |
35 | - | |
36 | 48 | end | ... | ... |
plugins/mezuro/public/javascripts/validations.js
... | ... | @@ -24,10 +24,30 @@ function validate_new_reading() { |
24 | 24 | var name = jQuery('#reading_label').val(); |
25 | 25 | var grade = jQuery('#reading_grade').val(); |
26 | 26 | var color = jQuery('#reading_color').val(); |
27 | + | |
27 | 28 | if (is_null(name) || is_null(grade) || is_null(color)){ |
28 | 29 | alert("Please fill all fields marked with (*)."); |
29 | 30 | return false; |
30 | 31 | } |
32 | + | |
33 | + var parser = jQuery('#labels_and_grades').attr('data-parser'); | |
34 | + var labels_and_grades = jQuery('#labels_and_grades').attr('data-list').split(parser); | |
35 | + for (var id = 0; id < labels_and_grades.length; id = id + 2) { | |
36 | + if (labels_and_grades[id] == name) { | |
37 | + alert("This label already exists! Please, choose another one."); | |
38 | + return false; | |
39 | + } | |
40 | + | |
41 | + if (labels_and_grades[id+1] == grade || labels_and_grades[id+1] == grade + ".0") { | |
42 | + alert("This grade already exists! Please, choose another one."); | |
43 | + return false; | |
44 | + } | |
45 | + } | |
46 | + | |
47 | + if (!color.match(/^[a-fA-F0-9]{6}$/)) { | |
48 | + alert("This is not a valid color."); | |
49 | + return false; | |
50 | + } | |
31 | 51 | return true; |
32 | 52 | } |
33 | 53 | ... | ... |
plugins/mezuro/test/functional/myprofile/mezuro_plugin_reading_controller_test.rb
... | ... | @@ -21,7 +21,11 @@ class MezuroPluginReadingControllerTest < ActionController::TestCase |
21 | 21 | end |
22 | 22 | |
23 | 23 | should 'set variables to create a new reading' do |
24 | + parser = "|*|" | |
25 | + Kalibro::Reading.expects(:readings_of).with(@content.reading_group_id).returns([@reading]) | |
24 | 26 | get :new, :profile => @profile.identifier, :id => @content.id |
27 | + assert_equal parser, assigns(:parser) | |
28 | + assert_equal ["#{@reading.label}#{parser}#{@reading.grade}#{parser}"], assigns(:labels_and_grades) | |
25 | 29 | assert_equal @content.id, assigns(:reading_group_content).id |
26 | 30 | assert_response :success |
27 | 31 | end |
... | ... | @@ -44,10 +48,16 @@ class MezuroPluginReadingControllerTest < ActionController::TestCase |
44 | 48 | end |
45 | 49 | |
46 | 50 | should 'set variables to edit a reading' do |
51 | + parser = "|*|" | |
52 | + another_reading = ReadingFixtures.reading | |
53 | + another_reading.id = 10 | |
54 | + Kalibro::Reading.expects(:readings_of).with(@content.reading_group_id).returns([@reading, another_reading]) | |
47 | 55 | Kalibro::Reading.expects(:find).with(@reading.id.to_s).returns(@reading) |
48 | 56 | get :edit, :profile => @profile.identifier, :id => @content.id, :reading_id => @reading.id |
49 | 57 | assert_equal @content.id, assigns(:reading_group_content).id |
50 | 58 | assert_equal @reading, assigns(:reading) |
59 | + assert_equal parser, assigns(:parser) | |
60 | + assert_equal ["#{another_reading.label}#{parser}#{another_reading.grade}#{parser}"], assigns(:labels_and_grades) | |
51 | 61 | assert_response :success |
52 | 62 | end |
53 | 63 | ... | ... |
plugins/mezuro/views/mezuro_plugin_reading/_form.html.erb
... | ... | @@ -8,4 +8,6 @@ |
8 | 8 | <%= required labelled_form_field _('Color:'), |
9 | 9 | colorpicker_field(:reading, :color) %>Click in the field to change Color |
10 | 10 | |
11 | +<div id="labels_and_grades" data-list="<%= @labels_and_grades %>" data-parser="<%= @parser %>"/> | |
12 | + | |
11 | 13 | <p><%= f.submit "Save" %></p> | ... | ... |