Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[38] Create Rails User model #46

Merged
merged 13 commits into from
Jul 26, 2024
Merged
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"

gem "annotate"
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"

Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
Expand Down Expand Up @@ -299,6 +302,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
annotate
bootsnap
capybara
codeclimate-test-reporter
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module UsersHelper
end
87 changes: 87 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string(255) not null
# password_hash :string(255)
# token :uuid
# first_name :string(255)
# last_name :string(255)
# phone_number :string(255)
# inserted_at :datetime not null
# updated_at :datetime not null
# email_verification_token :string(255)
# email_verified_at :datetime
# role :string(255) default("user"), not null
# password_reset_token :uuid
# password_reset_expires_at :datetime
# finalized :boolean default(TRUE), not null
# avatar_key :uuid
# avatar_extension :string(255)
# display :boolean default(TRUE), not null
# terms_of_use :datetime
# privacy_guidelines :datetime
# agency_id :bigint
# last_active :datetime
# status :string(255)
# active_session :boolean default(FALSE)
# renewal_request :string(255)
# jwt_token :text
# recertification_expired_at :datetime
#
class User < ApplicationRecord
has_many :challenges, dependent: :destroy
has_many :challenge_managers, dependent: :destroy
has_many :challenge_manager_challenges, through: :challenge_managers, source: :challenge, dependent: :destroy
has_many :members, dependent: :destroy
has_many :supporting_documents, class_name: 'Document', dependent: :destroy
has_many :submissions, foreign_key: :submitter_id, inverse_of: :submitter, dependent: :destroy
has_many :managed_submissions,
class_name: 'Submission',
foreign_key: :manager_id,
inverse_of: :manager,
dependent: :destroy
has_many :submission_documents, class_name: 'Submissions::Document', dependent: :destroy
has_many :message_context_statuses, dependent: :destroy

attribute :role, :string, default: -> { self[:role] }
attribute :status, :string, default: 'pending'
attribute :finalized, :boolean, default: true
attribute :display, :boolean, default: true

attribute :email, :string
attribute :password_hash, :string
attribute :token, :uuid
attribute :jwt_token, :string

attribute :email_verification_token, :string
attribute :email_verified_at, :datetime

attribute :password_reset_token, :uuid
attribute :password_reset_expires_at, :datetime

attribute :first_name, :string
attribute :last_name, :string
attribute :phone_number, :string

attribute :avatar_key, :uuid
attribute :avatar_extension, :string

attribute :terms_of_use, :datetime
attribute :privacy_guidelines, :datetime
attribute :agency_id, :integer

attribute :last_active, :datetime
attribute :recertification_expired_at, :datetime
attribute :active_session, :boolean, default: false

attribute :renewal_request, :string

attribute :created_at, :datetime, precision: 6
attribute :updated_at, :datetime, precision: 6

validates :email, presence: true
end
12 changes: 12 additions & 0 deletions spec/helpers/users_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the UsersHelper. For example:
#
# describe UsersHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
52 changes: 52 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string(255) not null
# password_hash :string(255)
# token :uuid
# first_name :string(255)
# last_name :string(255)
# phone_number :string(255)
# inserted_at :datetime not null
# updated_at :datetime not null
# email_verification_token :string(255)
# email_verified_at :datetime
# role :string(255) default("user"), not null
# password_reset_token :uuid
# password_reset_expires_at :datetime
# finalized :boolean default(TRUE), not null
# avatar_key :uuid
# avatar_extension :string(255)
# display :boolean default(TRUE), not null
# terms_of_use :datetime
# privacy_guidelines :datetime
# agency_id :bigint
# last_active :datetime
# status :string(255)
# active_session :boolean default(FALSE)
# renewal_request :string(255)
# jwt_token :text
# recertification_expired_at :datetime
#
require 'rails_helper'

RSpec.describe User do
describe 'validations' do
it 'validates presence of email' do
user = described_class.new(email: nil)
expect(user).not_to be_valid
expect(user.errors[:email]).to include("can't be blank")
end
end

describe 'default values' do
it 'sets active_session to false by default' do
user = described_class.new
expect(user.active_session).to be_falsey
end
end
end
Loading