CONSUL
Search…
Models
If you need to create new models or customize existent ones, you can do it so at the app/models/custom folder. Keep in mind that for old models you'll need to firstly require the dependency.
For example for Madrid's City Hall fork its required to check the zip code's format (it always starts with 280 followed by 2 digits). That check is at app/models/custom/verification/residence.rb:
1
require_dependency Rails.root.join('app', 'models', 'verification', 'residence').to_s
2
3
class Verification::Residence
4
5
validate :postal_code_in_madrid
6
validate :residence_in_madrid
7
8
def postal_code_in_madrid
9
errors.add(:postal_code, I18n.t('verification.residence.new.error_not_allowed_postal_code')) unless valid_postal_code?
10
end
11
12
def residence_in_madrid
13
return if errors.any?
14
15
unless residency_valid?
16
errors.add(:residence_in_madrid, false)
17
store_failed_attempt
18
Lock.increase_tries(user)
19
end
20
end
21
22
private
23
24
def valid_postal_code?
25
postal_code =~ /^280/
26
end
27
28
end
Copied!
Do not forget to cover your changes with a test at the spec/models/custom folder. Following the example we could create spec/models/custom/residence_spec.rb:
1
require 'rails_helper'
2
3
describe Verification::Residence do
4
5
let(:residence) { build(:verification_residence, document_number: "12345678Z") }
6
7
describe "verification" do
8
9
describe "postal code" do
10
it "should be valid with postal codes starting with 280" do
11
residence.postal_code = "28012"
12
residence.valid?
13
expect(residence.errors[:postal_code].size).to eq(0)
14
15
residence.postal_code = "28023"
16
residence.valid?
17
expect(residence.errors[:postal_code].size).to eq(0)
18
end
19
20
it "should not be valid with postal codes not starting with 280" do
21
residence.postal_code = "12345"
22
residence.valid?
23
expect(residence.errors[:postal_code].size).to eq(1)
24
25
residence.postal_code = "13280"
26
residence.valid?
27
expect(residence.errors[:postal_code].size).to eq(1)
28
expect(residence.errors[:postal_code]).to include("In order to be verified, you must be registered in the municipality of Madrid.")
29
end
30
end
31
32
end
33
34
end
Copied!
Last modified 6mo ago
Copy link