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