From 2bc4649aa75b97eaea255ed7c22825c3bee4998f Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 1 Feb 2017 11:12:40 +0100 Subject: [PATCH 1/4] uses year of birth instead of date of birth --- .../officing/residence_controller.rb | 2 +- app/models/officing/residence.rb | 22 +++++++---- app/views/officing/residence/new.html.erb | 4 +- spec/factories.rb | 2 +- spec/features/officing/residence_spec.rb | 26 +++++++++---- spec/models/officing/residence_spec.rb | 38 ++++++++++++------- 6 files changed, 60 insertions(+), 34 deletions(-) diff --git a/app/controllers/officing/residence_controller.rb b/app/controllers/officing/residence_controller.rb index 768bac21f..3b7455dbc 100644 --- a/app/controllers/officing/residence_controller.rb +++ b/app/controllers/officing/residence_controller.rb @@ -16,6 +16,6 @@ class Officing::ResidenceController < Officing::BaseController private def residence_params - params.require(:residence).permit(:document_number, :document_type, :date_of_birth) + params.require(:residence).permit(:document_number, :document_type, :year_of_birth) end end \ No newline at end of file diff --git a/app/models/officing/residence.rb b/app/models/officing/residence.rb index 2a8cca51d..cf69e6aba 100644 --- a/app/models/officing/residence.rb +++ b/app/models/officing/residence.rb @@ -1,22 +1,19 @@ class Officing::Residence include ActiveModel::Model - include ActiveModel::Dates include ActiveModel::Validations::Callbacks - attr_accessor :user, :officer, :document_number, :document_type, :date_of_birth + attr_accessor :user, :officer, :document_number, :document_type, :year_of_birth before_validation :call_census_api validates_presence_of :document_number validates_presence_of :document_type - validates_presence_of :date_of_birth + validates_presence_of :year_of_birth validate :allowed_age validate :residence_in_madrid def initialize(attrs={}) - self.date_of_birth = parse_date('date_of_birth', attrs) - attrs = remove_date('date_of_birth', attrs) super clean_document_number end @@ -62,7 +59,8 @@ class Officing::Residence end def allowed_age - return if errors[:date_of_birth].any? + return if errors[:year_of_birth].any? + return unless @census_api_response.valid? unless allowed_age? errors.add(:date_of_birth, I18n.t('verification.residence.new.error_not_allowed_age')) @@ -70,7 +68,7 @@ class Officing::Residence end def allowed_age? - self.date_of_birth <= User.minimum_required_age.years.ago + date_of_birth <= User.minimum_required_age.years.ago end def geozone @@ -85,6 +83,10 @@ class Officing::Residence @census_api_response.gender end + def date_of_birth + @census_api_response.date_of_birth + end + private def call_census_api @@ -93,7 +95,11 @@ class Officing::Residence def residency_valid? @census_api_response.valid? && - @census_api_response.date_of_birth == date_of_birth + @census_api_response.date_of_birth.year.to_s == year_of_birth.to_s + end + + def census_year_of_birth + @census_api_response.date_of_birth.year end def clean_document_number diff --git a/app/views/officing/residence/new.html.erb b/app/views/officing/residence/new.html.erb index 4d15db993..63a543f50 100644 --- a/app/views/officing/residence/new.html.erb +++ b/app/views/officing/residence/new.html.erb @@ -14,9 +14,7 @@
- <%= f.date_select :date_of_birth, - prompt: true, - start_year: 1900, end_year: 16.years.ago.year %> + <%= f.text_field :year_of_birth %>
diff --git a/spec/factories.rb b/spec/factories.rb index 706607a37..0e94ecf88 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -470,7 +470,7 @@ FactoryGirl.define do user document_number document_type "1" - date_of_birth Date.new(1980, 12, 31) + year_of_birth "1980" end factory :organization do diff --git a/spec/features/officing/residence_spec.rb b/spec/features/officing/residence_spec.rb index bc147a919..ef3ce5ab3 100644 --- a/spec/features/officing/residence_spec.rb +++ b/spec/features/officing/residence_spec.rb @@ -13,9 +13,9 @@ feature 'Residence' do click_link "Validate document" end - fill_in 'residence_document_number', with: "12345678Z" select 'DNI', from: 'residence_document_type' - select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_document_number', with: "12345678Z" + fill_in 'residence_year_of_birth', with: '1980' click_button 'Validate document' @@ -31,16 +31,28 @@ feature 'Residence' do expect(page).to have_content /\d errors? prevented the verification of this document/ end - scenario "Error on Census" do + scenario "Error on Census (document number)" do within("#side_menu") do click_link "Validate document" end - fill_in 'residence_document_number', with: "12345678Z" select 'DNI', from: 'residence_document_type' - select '1997', from: 'residence_date_of_birth_1i' - select 'January', from: 'residence_date_of_birth_2i' - select '1', from: 'residence_date_of_birth_3i' + fill_in 'residence_document_number', with: "9999999A" + fill_in 'residence_year_of_birth', with: '1980' + + click_button 'Validate document' + + expect(page).to have_content 'The Census was unable to verify this document' + end + + scenario "Error on Census (year of birth)" do + within("#side_menu") do + click_link "Validate document" + end + + select 'DNI', from: 'residence_document_type' + fill_in 'residence_document_number', with: "12345678Z" + fill_in 'residence_year_of_birth', with: '1981' click_button 'Validate document' diff --git a/spec/models/officing/residence_spec.rb b/spec/models/officing/residence_spec.rb index 1f09f8123..825c80a76 100644 --- a/spec/models/officing/residence_spec.rb +++ b/spec/models/officing/residence_spec.rb @@ -11,23 +11,33 @@ describe Officing::Residence do expect(residence).to be_valid end - describe "dates" do - it "should be valid with a valid date of birth" do - residence = Officing::Residence.new({"date_of_birth(3i)"=>"1", "date_of_birth(2i)"=>"1", "date_of_birth(1i)"=>"1980"}) - expect(residence.errors[:date_of_birth].size).to eq(0) - end - - it "should not be valid without a date of birth" do - residence = Officing::Residence.new({"date_of_birth(3i)"=>"", "date_of_birth(2i)"=>"", "date_of_birth(1i)"=>""}) - expect(residence).to_not be_valid - expect(residence.errors[:date_of_birth]).to include("can't be blank") - end + it "should not be valid without a document number" do + residence.document_number = nil + expect(residence).to_not be_valid end - it "should validate user has allowed age" do - residence = Officing::Residence.new({"date_of_birth(3i)"=>"1", "date_of_birth(2i)"=>"1", "date_of_birth(1i)"=>"#{5.year.ago.year}"}) + it "should not be valid without a document type" do + residence.document_type = nil expect(residence).to_not be_valid - expect(residence.errors[:date_of_birth]).to include("You don't have the required age to participate") + end + + it "should not be valid without a year of birth" do + residence.year_of_birth = nil + expect(residence).to_not be_valid + end + + describe "allowed age" do + it "should not be validate if user is under allowed age" do + allow_any_instance_of(Officing::Residence).to receive(:date_of_birth).and_return(15.years.ago) + expect(residence).to_not be_valid + expect(residence.errors[:date_of_birth]).to include("You don't have the required age to participate") + end + + it "should be validate if user is above allowed age" do + allow_any_instance_of(Officing::Residence).to receive(:date_of_birth).and_return(16.years.ago) + expect(residence).to be_valid + expect(residence.errors[:date_of_birth]).to be_empty + end end end From 2d0756ef1d7ac2da906aa35effaac78d3d000ef5 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 1 Feb 2017 11:20:22 +0100 Subject: [PATCH 2/4] updates translations for year birth --- config/locales/activemodel.en.yml | 2 +- config/locales/activemodel.es.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/activemodel.en.yml b/config/locales/activemodel.en.yml index 04de6d97f..fd378e705 100644 --- a/config/locales/activemodel.en.yml +++ b/config/locales/activemodel.en.yml @@ -19,4 +19,4 @@ en: officing/residence: document_type: "Document type" document_number: "Document number (including letters)" - date_of_birth: "Date of birth" \ No newline at end of file + year_of_birth: "Year born" \ No newline at end of file diff --git a/config/locales/activemodel.es.yml b/config/locales/activemodel.es.yml index 12d2902ff..dafc45a87 100644 --- a/config/locales/activemodel.es.yml +++ b/config/locales/activemodel.es.yml @@ -19,4 +19,4 @@ es: officing/residence: document_type: "Tipo documento" document_number: "Numero de documento (incluida letra)" - date_of_birth: "Fecha de nacimiento" + year_of_birth: "Año de nacimiento" From 495a753a5ac0df3e81adbc6c867d12b99a08a7bb Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 1 Feb 2017 11:21:41 +0100 Subject: [PATCH 3/4] cleans up --- spec/models/officing/residence_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/officing/residence_spec.rb b/spec/models/officing/residence_spec.rb index 825c80a76..3653baf7c 100644 --- a/spec/models/officing/residence_spec.rb +++ b/spec/models/officing/residence_spec.rb @@ -27,13 +27,13 @@ describe Officing::Residence do end describe "allowed age" do - it "should not be validate if user is under allowed age" do + it "should not be valid if user is under allowed age" do allow_any_instance_of(Officing::Residence).to receive(:date_of_birth).and_return(15.years.ago) expect(residence).to_not be_valid expect(residence.errors[:date_of_birth]).to include("You don't have the required age to participate") end - it "should be validate if user is above allowed age" do + it "should be valid if user is above allowed age" do allow_any_instance_of(Officing::Residence).to receive(:date_of_birth).and_return(16.years.ago) expect(residence).to be_valid expect(residence.errors[:date_of_birth]).to be_empty From 5c676498e2570d95166f0c63a3051e41eadc3fc8 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 1 Feb 2017 11:37:11 +0100 Subject: [PATCH 4/4] fixes specs --- spec/support/common_actions.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 008403dbe..e0fdfe425 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -142,9 +142,9 @@ module CommonActions end def officing_verify_residence - fill_in 'residence_document_number', with: "12345678Z" select 'DNI', from: 'residence_document_type' - select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_document_number', with: "12345678Z" + fill_in 'residence_year_of_birth', with: "1980" click_button 'Validate document'