-
Notifications
You must be signed in to change notification settings - Fork 138
Postal address fields #1040
base: master
Are you sure you want to change the base?
Postal address fields #1040
Changes from all commits
c954a2a
549d370
08bb3d2
dd200a3
752ab32
0992f1f
f3d0205
020fc85
30eb3b3
e57bc9c
89c8a4c
2eea06b
e73d7fa
b06aa4c
70ecfbc
f92b1c2
978544e
6b07d34
8565c6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # frozen_string_literal: true | ||
| class PostalAddress < ApplicationRecord | ||
| belongs_to :user | ||
|
|
||
| validates_presence_of :line1, :city, :zip, :country | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| = @user.postal_address.line1.titlecase + " " + @user.postal_address.line2.titlecase + " " | ||
| = @user.postal_address.city.titlecase + ", " + @user.postal_address.state.titlecase + " " | ||
| = @user.postal_address.zip + " " +@user.postal_address.country | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,9 +65,13 @@ nav.actions | |
| p = @user.company_info | ||
|
|
||
| - if can_see_private_info? | ||
| - fields = ['tshirt_size', 'tshirt_cut', 'postal_address'] | ||
| .well.private-info | ||
| h3 Private info | ||
| - if @user.postal_address | ||
| h4 Postal Address | ||
| p | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has been some time that I read What's the point of this empty paragraph? Should the postal-address partial rendered in a paragraph? If yes, then I think™ the way to go would be_ p
= render ...But looking at the partial not using any markup at all, I'd rather suggest to do the markup in there. |
||
| = render @user.postal_address | ||
| - fields = ['tshirt_size', 'tshirt_cut'] | ||
| - fields.each do |field| | ||
| - if @user.send(field).present? | ||
| p | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class CreatePostalAddresses < ActiveRecord::Migration[5.1] | ||
| def change | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not suuuuper necessary, but it would come in quite handy if you could just squash the two migrations. For instance: roll back the two migrations, edit this one to have the right names (and delete the other migration) and then run it again. |
||
| create_table :postal_addresses do |t| | ||
| t.string :address_line_1 | ||
| t.string :address_line_2 | ||
| t.string :city | ||
| t.string :state_or_province | ||
| t.string :postal_code | ||
| t.string :country | ||
|
|
||
| t.references :user, foreign_key: true | ||
| t.timestamps | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class RemovePostalAddressFromUsers < ActiveRecord::Migration[5.1] | ||
| def change | ||
| remove_column :users, :postal_address, :text | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we care about existing users having set up a |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class FixPostalAddressColumnNames < ActiveRecord::Migration[5.1] | ||
| def change | ||
| rename_column :postal_addresses, :address_line_1, :line1 | ||
| rename_column :postal_addresses, :address_line_2, :line2 | ||
| rename_column :postal_addresses, :state_or_province, :state | ||
| rename_column :postal_addresses, :postal_code, :zip | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| FactoryBot.define do | ||
| factory :postal_address do | ||
| user | ||
| line1 { FFaker::AddressUS.street_address } | ||
| line2 { FFaker::AddressUS.secondary_address } | ||
| city { FFaker::AddressUS.city} | ||
| state { FFaker::AddressUS.state} | ||
| zip { FFaker::AddressUS.zip_code} | ||
| country { "United States" } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Add Postal Address', type: :feature do | ||
| let(:user) { create(:user) } | ||
| let(:address) { create(:postal_address) } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As said in a previous comment, I'd personally prefer to just use actual strings, like "Canada" in the |
||
|
|
||
| context 'signed in' do | ||
| before { sign_in user } | ||
|
|
||
| context 'in the user edit page' do | ||
| before { visit edit_user_path(user) } | ||
|
|
||
| it 'allows creation of postal address if all required address fields are entered' do | ||
| fill_in 'Line1', with: address.line1 | ||
| fill_in 'Line2', with: address.line2 | ||
| fill_in 'City', with: address.city | ||
| fill_in 'State', with: address.state | ||
| fill_in 'Zip', with: address.zip | ||
| select address.country, from: 'Country' | ||
| click_on 'Save' | ||
|
|
||
| expect(current_path).to eq user_path(user) | ||
|
|
||
| expect(page).to have_content address.line1 | ||
| expect(page).to have_content address.line2 | ||
| expect(page).to have_content address.city | ||
| expect(page).to have_content address.state | ||
| expect(page).to have_content address.zip | ||
| expect(page).to have_content 'US' | ||
| end | ||
|
|
||
| it 'does not allow a postal address to be added if required fields are missing' do | ||
| fill_in 'Line1', with: address.line1 | ||
| fill_in 'Zip', with: address.zip | ||
| click_on 'Save' | ||
|
|
||
| expect(page).to have_content "Postal address city can't be blank" | ||
lilwillifo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expect(page).to have_content "Postal address country can't be blank" | ||
| end | ||
| end | ||
|
|
||
| context 'when the user already has a postal address set up' do | ||
| let!(:postal_address) { create(:postal_address, user: user) } | ||
|
|
||
| it 'autofills saved postal address info on edit form if it exists' do | ||
| visit edit_user_path(user) | ||
|
|
||
| expect(page).to have_selector("input[value='#{user.postal_address.line1}']") | ||
| expect(page).to have_selector("input[value='#{user.postal_address.city}']") | ||
| expect(page).to have_selector("input[value='#{user.postal_address.state}']") | ||
| expect(page).to have_selector("input[value='#{user.postal_address.zip}']") | ||
| end | ||
|
|
||
| it 'allows to delete an existing postal address' do | ||
| visit edit_user_path(user) | ||
|
|
||
| check 'user[postal_address_attributes][_destroy]' | ||
| click_on 'Save' | ||
|
|
||
| expect(current_path).to eq user_path(user) | ||
| expect(page).to_not have_content postal_address.line1 | ||
| expect(page).to_not have_content postal_address.line2 | ||
| expect(page).to_not have_content postal_address.city | ||
| expect(page).to_not have_content postal_address.state | ||
| expect(page).to_not have_content postal_address.zip | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe PostalAddress, type: :model do | ||
| describe 'associations' do | ||
| it { is_expected.to belong_to(:user) } | ||
| end | ||
|
|
||
| describe 'validations' do | ||
| it { is_expected.to validate_presence_of(:line1)} | ||
| it { is_expected.to validate_presence_of(:city)} | ||
| it { is_expected.to validate_presence_of(:zip)} | ||
| it { is_expected.to validate_presence_of(:country)} | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this being a bit contra Rails' conventions, I'd say this is making the maintainability of the app a bit harder 😉
The partial being in
postal_address/_postal_address, you could render it by just callingInternally, this will pass a local assigned variable with the name
postal_addressto the partial - so there's a variable calledpostal_addressavailable in the partial (same name as the model).To fully make sure everything is according to plan, you could even
fetchthis variable:Using
@userhere would be bypassing all this nice assignment magic that Rails does for us. Also - and that's the main point - it makes the partial incompatible for any other use aside from views with an@userset 😢