From 3f9a4b9624fecb8581f0f26fd31faaa667723b4c Mon Sep 17 00:00:00 2001 From: Celia Collins Date: Mon, 23 Sep 2024 14:30:02 +0100 Subject: [PATCH] Store child age as birthday rather than no of months --- app/controllers/users_controller.rb | 2 +- app/models/user.rb | 4 ++-- app/views/users/new.html.erb | 2 +- .../20240923131205_add_child_birthday_to_usr.rb | 12 ++++++++++++ db/schema.rb | 4 ++-- test/factories/user.rb | 2 +- test/models/user_test.rb | 4 ++-- 7 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20240923131205_add_child_birthday_to_usr.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b165d12..a36e759 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -34,6 +34,6 @@ def create private def user_params - params.require(:user).permit(:first_name, :last_name, :phone_number, :child_age, :interest_ids) + params.require(:user).permit(:first_name, :last_name, :phone_number, :child_birthday, :interest_ids) end end diff --git a/app/models/user.rb b/app/models/user.rb index 85c62bf..44b5091 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,7 +2,7 @@ class User < ApplicationRecord has_many :interests has_many :messages, dependent: :destroy has_many :contents, through: :messages - validates :phone_number, :first_name, :last_name, :child_age, presence: true + validates :phone_number, :first_name, :last_name, :child_birthday, presence: true validates_uniqueness_of :phone_number phony_normalize :phone_number, default_country_code: "UK" @@ -11,7 +11,7 @@ class User < ApplicationRecord scope :contactable, -> { where(contactable: true) } def child_age_in_months_today - child_age + ((Time.now.to_date.year * 12 + Time.now.to_date.month) - (created_at.to_date.year * 12 + created_at.to_date.month)) + (Time.now.year * 12 + Time.now.month) - (child_birthday.year * 12 + child_birthday.month) end def full_name diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index cb678fa..52b2311 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -21,7 +21,7 @@
- <%= form.input :child_age, label: "Your child's age in months" %> + <%= form.input :child_birthday, label: "Your child's age in months", as: :date, order: [:day, :month, :year] %>
<%= form.input :interest_ids, as: :hidden, input_html: { value: params["interest_ids"] } %> diff --git a/db/migrate/20240923131205_add_child_birthday_to_usr.rb b/db/migrate/20240923131205_add_child_birthday_to_usr.rb new file mode 100644 index 0000000..e97bf92 --- /dev/null +++ b/db/migrate/20240923131205_add_child_birthday_to_usr.rb @@ -0,0 +1,12 @@ +class AddChildBirthdayToUsr < ActiveRecord::Migration[7.1] + def change + add_column :users, :child_birthday, :date + + User.find_each do |user| + user.update(child_birthday: user.created_at - 18.months) + end + + remove_column :users, :child_age, :date + change_column_null :users, :child_birthday, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 6855167..2cd0234 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_09_18_133122) do +ActiveRecord::Schema[7.1].define(version: 2024_09_23_131205) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -84,10 +84,10 @@ t.string "phone_number", null: false t.string "first_name", null: false t.string "last_name", null: false - t.integer "child_age", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "contactable", default: true + t.date "child_birthday", null: false end add_foreign_key "interests", "users" diff --git a/test/factories/user.rb b/test/factories/user.rb index c793928..1d59828 100644 --- a/test/factories/user.rb +++ b/test/factories/user.rb @@ -3,6 +3,6 @@ first_name { "Ali" } last_name { "Smith" } sequence(:phone_number) { |n| "07#{n}23456789" } - child_age { 18 } + child_birthday { Time.now - 18.months } end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index f4196a9..567ba35 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -22,7 +22,7 @@ class UserTest < ActiveSupport::TestCase test("phone_number required") { assert_present(:phone_number) } test("first_name required") { assert_present(:first_name) } test("last_name required") { assert_present(:last_name) } - test("child_age required") { assert_present(:child_age) } + test("child_birthday required") { assert_present(:child_birthday) } test "should have a contactable scope" do create(:user, contactable: false) @@ -32,7 +32,7 @@ class UserTest < ActiveSupport::TestCase end test "child_age_in_months_today method" do - user = create(:user, child_age: 5) + user = create(:user, child_birthday: Time.now - 5.months) assert_equal user.child_age_in_months_today, 5 end