Skip to content

Commit

Permalink
Merge pull request #6 from nestauk/ask-child-birthday
Browse files Browse the repository at this point in the history
Store child age as birthday rather than no of months
  • Loading branch information
cdccollins authored Sep 23, 2024
2 parents 3bd9074 + 3f9a4b9 commit 40e51a0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</div>

<div class="mt-3">
<%= 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] %>
</div>

<%= form.input :interest_ids, as: :hidden, input_html: { value: params["interest_ids"] } %>
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240923131205_add_child_birthday_to_usr.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/factories/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 40e51a0

Please sign in to comment.