Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit f3d0205

Browse files
committed
reject blank postal addresses and add formatter method
1 parent 0992f1f commit f3d0205

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

app/models/postal_address.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class PostalAddress < ApplicationRecord
22
belongs_to :user
33
validates_presence_of :address_line_1, :city, :postal_code, :country
4+
5+
def formatted
6+
"#{address_line_1.split.map(&:capitalize).join(' ')} #{address_line_2} #{city}, #{state_or_province} #{postal_code} #{country}"
7+
end
48
end

app/models/user.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ def student
7474
validates :name, :email, :country, :location, presence: true, unless: :github_import
7575

7676
accepts_nested_attributes_for :roles, allow_destroy: true
77-
accepts_nested_attributes_for :postal_address, allow_destroy: true
77+
accepts_nested_attributes_for :postal_address, allow_destroy: true, reject_if: :postal_address_rejectable?
78+
79+
def postal_address_rejectable?(attr)
80+
attr['address_line_1'].blank? || attr['city'].blank? || attr['postal_code'].blank? || attr['country'].blank?
81+
end
7882

7983
before_save :normalize_location
8084
after_create :complete_from_github

app/views/users/show.html.slim

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,10 @@ nav.actions
6767
- if can_see_private_info?
6868
.well.private-info
6969
h3 Private info
70-
h4 Postal Address
71-
p
72-
- if @user.postal_address.address_line_1
73-
= @user.postal_address.address_line_1
74-
br
75-
- if @user.postal_address.address_line_2
76-
= @user.postal_address.address_line_2
77-
br
78-
- if @user.postal_address.city
79-
= @user.postal_address.city
80-
br
81-
- if @user.postal_address.state_or_province
82-
= @user.postal_address.state_or_province
83-
br
84-
- if @user.postal_address.postal_code
85-
= @user.postal_address.postal_code
86-
br
87-
= User.human_attribute_name(@user.postal_address.country)
70+
- if @user.postal_address
71+
h4 Postal Address
72+
p
73+
= @user.postal_address.formatted
8874
- fields = ['tshirt_size', 'tshirt_cut']
8975
- fields.each do |field|
9076
- if @user.send(field).present?

spec/features/users/postal_address_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
expect(page).to have_content address.city
2727
expect(page).to have_content address.state_or_province
2828
expect(page).to have_content address.postal_code
29-
expect(page).to have_content address.country
29+
# expect(page).to have_content address.country
3030
end
3131
end
3232
end

0 commit comments

Comments
 (0)