diff --git a/app/models/user.rb b/app/models/user.rb index 14a8ac1e0..e3b1fa6a5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -50,11 +50,10 @@ class User < ApplicationRecord devise :omniauthable, :rememberable, :registerable, :database_authenticatable, :confirmable validates :email, - uniqueness: { allow_blank: true, case_sensitive: false }, length: { maximum: 50 }, email: true, - allow_blank: true - validates :email, uniqueness: { case_sensitive: false }, if: :email_changed? + allow_nil: true + validates :email, uniqueness: { allow_nil: true, case_sensitive: false }, if: :email_changed? validates :name, length: { minimum: 1, maximum: 100 } validates :username, presence: true, @@ -142,7 +141,7 @@ class User < ApplicationRecord before_update :mentorship_status_update before_validation :set_username # make sure usernames are not empty, to be able to use the database unique index - before_validation :verify_twitter_username, :verify_github_username + before_validation :verify_twitter_username, :verify_github_username, :verify_email before_validation :set_config_input before_validation :downcase_email before_validation :check_for_username_change @@ -421,6 +420,10 @@ class User < ApplicationRecord self.github_username = nil if github_username == "" end + def verify_email + self.email = nil if email == "" + end + def set_username set_temp_username if username.blank? self.username = username&.downcase diff --git a/db/migrate/20190404102732_allow_null_on_users_email.rb b/db/migrate/20190404102732_allow_null_on_users_email.rb new file mode 100644 index 000000000..06b05a076 --- /dev/null +++ b/db/migrate/20190404102732_allow_null_on_users_email.rb @@ -0,0 +1,5 @@ +class AllowNullOnUsersEmail < ActiveRecord::Migration[5.2] + def change + change_column :users, :email, :string, null: true, default: nil + end +end diff --git a/db/schema.rb b/db/schema.rb index 125158b58..0d66ac43d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,8 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_04_02_224426) do +ActiveRecord::Schema.define(version: 2019_04_04_102732) do # These are extensions that must be enabled in order to support this database - enable_extension "pg_stat_statements" enable_extension "plpgsql" create_table "ahoy_messages", id: :serial, force: :cascade do |t| @@ -777,7 +776,7 @@ ActiveRecord::Schema.define(version: 2019_04_02_224426) do t.string "dribbble_url" t.string "editor_version", default: "v1" t.string "education" - t.string "email", default: "", null: false + t.string "email" t.boolean "email_badge_notifications", default: true t.boolean "email_comment_notifications", default: true t.boolean "email_connect_messages", default: true diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d176e5a65..5b6e2f9b5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -50,7 +50,7 @@ RSpec.describe User, type: :model do service.get_user end - describe "makes sure usernames are not blank" do + describe "makes sure usernames and email are not blank" do it "sets twitter username to nil" do user = create(:user, twitter_username: "") user.reload @@ -69,6 +69,18 @@ RSpec.describe User, type: :model do expect(user.github_username).to eq("hello") expect(user.twitter_username).to eq("world") end + + it "sets email to nil" do + user = create(:user, email: "") + user.reload + expect(user.email).to eq(nil) + end + + it "sets correct email if it's not blank" do + user = create(:user, email: "anna@example.com") + user.reload + expect(user.email).to eq("anna@example.com") + end end describe "validations" do diff --git a/spec/requests/dashboard_spec.rb b/spec/requests/dashboard_spec.rb index f02b859d1..83337f86f 100644 --- a/spec/requests/dashboard_spec.rb +++ b/spec/requests/dashboard_spec.rb @@ -115,7 +115,7 @@ RSpec.describe "Dashboards", type: :request do end it "lists followed organizations" do - expect(response.body).to include organization.name + expect(response.body).to include CGI.escapeHTML(organization.name) end end end