From fbb5db8062e29118b65607d2c9382d5d83596cdb Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 26 Sep 2015 13:45:37 +0200 Subject: [PATCH 1/3] removes unnecessary address from users --- app/models/address.rb | 3 -- app/models/user.rb | 1 - app/models/verification/letter.rb | 37 +------------------ config/locales/verification.en.yml | 3 -- config/locales/verification.es.yml | 3 -- .../20150926113052_drop_addresses_table.rb | 27 ++++++++++++++ db/schema.rb | 22 +---------- lib/census_api.rb | 6 +-- spec/factories.rb | 7 ---- spec/features/verification/letter_spec.rb | 15 -------- spec/models/letter_spec.rb | 22 ----------- 11 files changed, 31 insertions(+), 115 deletions(-) delete mode 100644 app/models/address.rb create mode 100644 db/migrate/20150926113052_drop_addresses_table.rb diff --git a/app/models/address.rb b/app/models/address.rb deleted file mode 100644 index 2c0c20b88..000000000 --- a/app/models/address.rb +++ /dev/null @@ -1,3 +0,0 @@ -class Address < ActiveRecord::Base - belongs_to :user -end diff --git a/app/models/user.rb b/app/models/user.rb index 326b7a7b4..5fad11cba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,7 +12,6 @@ class User < ActiveRecord::Base acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases - has_one :address has_one :administrator has_one :moderator has_one :organization diff --git a/app/models/verification/letter.rb b/app/models/verification/letter.rb index 6f1d559b2..aa6fd6a4b 100644 --- a/app/models/verification/letter.rb +++ b/app/models/verification/letter.rb @@ -1,20 +1,13 @@ class Verification::Letter include ActiveModel::Model - attr_accessor :user, :address, :verification_code + attr_accessor :user, :verification_code validates :user, presence: true - validates :address, presence: true - validate :correct_address def save valid? && - letter_requested! && - update_user_address - end - - def address - @address ||= CensusApi.new(user).address + letter_requested! end def letter_requested! @@ -37,32 +30,6 @@ class Verification::Letter user.letter_verification_code == verification_code end - def correct_address - errors.add(:address, I18n.t('verification.letter.errors.address_not_found')) unless - address.present? - end - - def update_user_address - user.address = Address.new(parsed_address) - user.save - end - - def parsed_address - { postal_code: address[:codigo_postal], - street: address[:nombre_via], - street_type: address[:sigla_via], - number: address[:numero_via], - number_type: address[:nominal_via], - letter: address[:letra_via], - portal: address[:portal], - stairway: address[:escalera], - floor: address[:planta], - door: address[:puerta], - km: address[:km], - neighbourhood: address[:nombre_barrio], - district: address[:nombre_distrito] } - end - def increase_letter_verification_tries user.update(letter_verification_tries: user.letter_verification_tries += 1) end diff --git a/config/locales/verification.en.yml b/config/locales/verification.en.yml index dcd080e64..d48a22ab6 100644 --- a/config/locales/verification.en.yml +++ b/config/locales/verification.en.yml @@ -75,8 +75,6 @@ en: success_html: "Thank you for requesting a maximum security code in a few days we will send it to the address on your census data." offices: "Oficinas de Atención al Ciudadano" offices_url: "http://www.madrid.es/portales/munimadrid/es/Inicio/El-Ayuntamiento/Atencion-al-ciudadano/Oficinas-de-Atencion-al-Ciudadano?vgnextfmt=default&vgnextchannel=5b99cde2e09a4310VgnVCM1000000b205a0aRCRD" - alert: - failure: "We could not verify your address with the Census please try again later" edit: title: "Empieza a participar" confirmation_code: "Introduce aquí el código de máxima seguridad cuando recibas tu carta" @@ -91,7 +89,6 @@ en: errors: letter_not_sent: "We have not sent you the letter with the code yet" incorect_code: "Incorrect confirmation code" - address_not_found: "Address not found" verified_user: show: title: "Available information" diff --git a/config/locales/verification.es.yml b/config/locales/verification.es.yml index 021c9ba1e..96e1ab79d 100644 --- a/config/locales/verification.es.yml +++ b/config/locales/verification.es.yml @@ -75,8 +75,6 @@ es: success_html: "Gracias por solicitar tu código de máxima seguridad (solo necesario para las votaciones finales), en unos días te lo enviaremos a la dirección que figura en tus datos del padrón. Recuerda que puedes ahorrar el envío recogiendo tu código en cualquiera de las %{offices}." offices: "Oficinas de Atención al Ciudadano" offices_url: "http://www.madrid.es/portales/munimadrid/es/Inicio/El-Ayuntamiento/Atencion-al-ciudadano/Oficinas-de-Atencion-al-Ciudadano?vgnextfmt=default&vgnextchannel=5b99cde2e09a4310VgnVCM1000000b205a0aRCRD" - alert: - failure: "No podemos verificar tu dirección con el Padrón, por favor inténtalo otra vez más tarde" edit: title: "Empieza a participar" confirmation_code: "Introduce aquí el código de máxima seguridad cuando recibas tu carta" @@ -91,7 +89,6 @@ es: errors: letter_not_sent: "Aún no te hemos enviado la carta con el código" incorect_code: "Código de verificación incorrecto" - address_not_found: "No se ha encontrado una dirección" verified_user: show: title: "Información disponible" diff --git a/db/migrate/20150926113052_drop_addresses_table.rb b/db/migrate/20150926113052_drop_addresses_table.rb new file mode 100644 index 000000000..90d22c41a --- /dev/null +++ b/db/migrate/20150926113052_drop_addresses_table.rb @@ -0,0 +1,27 @@ +class DropAddressesTable < ActiveRecord::Migration + def self.up + drop_table :addresses + end + + def self.down + create_table "addresses", force: :cascade do |t| + t.integer "user_id" + t.string "street" + t.string "street_type" + t.string "number" + t.string "number_type" + t.string "letter" + t.string "portal" + t.string "stairway" + t.string "floor" + t.string "door" + t.string "km" + t.string "neighbourhood" + t.string "district" + t.string "postal_code" + t.string "toponymy" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 22b6b730c..1ed684ce5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150921095553) do +ActiveRecord::Schema.define(version: 20150926113052) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,26 +28,6 @@ ActiveRecord::Schema.define(version: 20150921095553) do add_index "activities", ["actionable_id", "actionable_type"], name: "index_activities_on_actionable_id_and_actionable_type", using: :btree add_index "activities", ["user_id"], name: "index_activities_on_user_id", using: :btree - create_table "addresses", force: :cascade do |t| - t.integer "user_id" - t.string "street" - t.string "street_type" - t.string "number" - t.string "number_type" - t.string "letter" - t.string "portal" - t.string "stairway" - t.string "floor" - t.string "door" - t.string "km" - t.string "neighbourhood" - t.string "district" - t.string "postal_code" - t.string "toponymy" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - create_table "administrators", force: :cascade do |t| t.integer "user_id" end diff --git a/lib/census_api.rb b/lib/census_api.rb index a0403f6b0..30b321e96 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -37,10 +37,6 @@ class CensusApi data[:datos_vivienda][:item][:codigo_postal] end - def address - response[:get_habita_datos_response][:get_habita_datos_return][:datos_vivienda][:item] - end - def valid? return false unless data[:datos_habitante][:item].present? @@ -53,6 +49,6 @@ class CensusApi end def stubbed_response - {:get_habita_datos_response=>{:get_habita_datos_return=>{:hay_errores=>false, :datos_habitante=>{:item=>{:fecha_nacimiento_string=>"31-12-1980", :identificador_documento=>"12345678Z", }}, :datos_vivienda=>{:item=>{:codigo_postal=>"28013", :escalera=>"4", :km=>"0", :letra_via=>"B", :nombre_barrio=>"JUSTICIA", :nombre_distrito=>"CENTRO", :nombre_via=>"ALCALÁ", :nominal_via=>"NUM", :numero_via=>"1", :planta=>"PB", :portal=>"1", :puerta=>"DR", :sigla_via=>"CALLE"}}}}} + {:get_habita_datos_response=>{:get_habita_datos_return=>{:hay_errores=>false, :datos_habitante=>{:item=>{:fecha_nacimiento_string=>"31-12-1980", :identificador_documento=>"12345678Z", }}, :datos_vivienda=>{:item=>{:codigo_postal=>"28013"}}}}} end end diff --git a/spec/factories.rb b/spec/factories.rb index 2ea67d821..32c82f889 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -58,7 +58,6 @@ FactoryGirl.define do factory :verification_letter, class: Verification::Letter do user - address end factory :lock do @@ -67,12 +66,6 @@ FactoryGirl.define do locked_until Time.now end - factory :address do - street_type "Calle" - street "Alcalá" - number "1" - end - factory :verified_user do document_number '12345678Z' document_type 'dni' diff --git a/spec/features/verification/letter_spec.rb b/spec/features/verification/letter_spec.rb index 6378c0eaa..8c9282800 100644 --- a/spec/features/verification/letter_spec.rb +++ b/spec/features/verification/letter_spec.rb @@ -49,21 +49,6 @@ feature 'Verify Letter' do expect(page).to have_content "Incorrect confirmation code" end - scenario "Error accessing address from CensusApi" do - user = create(:user, residence_verified_at: Time.now, - confirmed_phone: "611111111", - letter_sent_at: 1.day.ago) - - login_as(user) - visit new_letter_path - - allow_any_instance_of(CensusApi).to receive(:address).and_return(nil) - - click_button "Send me a letter with the code" - - expect(page).to have_content "We could not verify your address with the Census please try again later" - end - scenario "Deny access unless verified residence" do user = create(:user) diff --git a/spec/models/letter_spec.rb b/spec/models/letter_spec.rb index e95e31e4b..683fb06d5 100644 --- a/spec/models/letter_spec.rb +++ b/spec/models/letter_spec.rb @@ -17,11 +17,6 @@ describe 'Verification::Letter' do expect(letter).to_not be_valid end - it "should not be valid without an address" do - letter.address = {} - expect(letter).to_not be_valid - end - end describe "save" do @@ -36,23 +31,6 @@ describe 'Verification::Letter' do expect(user.letter_requested_at).to be end - it "should update address" do - expect(user.address).to have_attributes({ - postal_code: "28013", - street: "ALCALÁ", - street_type: "CALLE", - number: "1", - number_type: "NUM", - letter: "B", - portal: "1", - stairway: "4", - floor: "PB", - door: "DR", - km: "0", - neighbourhood: "JUSTICIA", - district: "CENTRO" - }) - end end describe "#verified?" do From 24a60bdbf2e7d62b486a41b3d9998cfa4030a3d2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Sep 2015 11:59:03 +0200 Subject: [PATCH 2/3] removes unnecessary condition and flash --- app/controllers/verification/letter_controller.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/controllers/verification/letter_controller.rb b/app/controllers/verification/letter_controller.rb index a6b5125bc..11bf5585d 100644 --- a/app/controllers/verification/letter_controller.rb +++ b/app/controllers/verification/letter_controller.rb @@ -12,12 +12,8 @@ class Verification::LetterController < ApplicationController def create @letter = Verification::Letter.new(user: current_user) - if @letter.save - redirect_to edit_letter_path - else - flash.now.alert = t('verification.letter.create.alert.failure') - render :new - end + @letter.save! + redirect_to edit_letter_path end def edit From f0b1f1901e673dbb80b973a34d90e36a8d48cb65 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Sep 2015 12:05:06 +0200 Subject: [PATCH 3/3] fixes typo --- app/controllers/verification/letter_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/verification/letter_controller.rb b/app/controllers/verification/letter_controller.rb index 11bf5585d..105eef0ad 100644 --- a/app/controllers/verification/letter_controller.rb +++ b/app/controllers/verification/letter_controller.rb @@ -12,7 +12,7 @@ class Verification::LetterController < ApplicationController def create @letter = Verification::Letter.new(user: current_user) - @letter.save! + @letter.save redirect_to edit_letter_path end