Skip to content

Commit a64acc7

Browse files
committed
change user locale to be not null and have default value
1 parent c9e67bc commit a64acc7

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class User < ApplicationRecord
6969

7070
validates :email, uniqueness: true
7171
validates :name, presence: true
72-
validates :locale, inclusion: {in: I18n.available_locales.map(&:to_s), allow_blank: true}
72+
validates :locale, presence: true, inclusion: {in: I18n.available_locales.map(&:to_s)}
7373
# password length and confirmation match is handled by Devise
7474
validates :password_confirmation, presence: true, if: -> { password.present? }
7575
validates :password, format: {with: /\A(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+\z/, message: :password_complexity}, if: -> { password.present? }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class ChangeUserLocaleToNotNull < ActiveRecord::Migration[7.2]
2+
class User < ApplicationRecord
3+
end
4+
5+
def up
6+
User.where(locale: ["", nil]).update(locale: "en")
7+
8+
change_column :users, :locale, :string, null: false, default: "en"
9+
end
10+
11+
def down
12+
change_column :users, :locale, :string, null: true, default: nil
13+
end
14+
end

db/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.2].define(version: 2025_05_21_075002) do
13+
ActiveRecord::Schema[7.2].define(version: 2025_07_16_100255) do
1414
create_schema "tiger"
1515
create_schema "tiger_data"
1616
create_schema "topology"
@@ -1127,7 +1127,7 @@
11271127
t.integer "observer_id"
11281128
t.integer "operator_id"
11291129
t.integer "holding_id"
1130-
t.string "locale"
1130+
t.string "locale", default: "en", null: false
11311131
t.string "first_name"
11321132
t.string "last_name"
11331133
t.boolean "organization_account", default: false, null: false

spec/models/user_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858

5959
it { is_expected.to validate_length_of(:password).is_at_least(10).is_at_most(128) }
6060

61+
it "is invalid without locale" do
62+
subject.locale = nil
63+
expect(subject.valid?).to eq(false)
64+
expect(subject.errors[:locale]).to include("can't be blank")
65+
end
66+
6167
it "is invalid when password does not contain lowercase letter" do
6268
subject.password = "PASSWORD1234"
6369
subject.password_confirmation = "PASSWORD1234"

0 commit comments

Comments
 (0)