diff --git a/.rubocop.yml b/.rubocop.yml index 46a30a5..63340e8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -25,6 +25,7 @@ Metrics/ModuleLength: Metrics/BlockLength: Exclude: - 'spec/**/*' + - 'config/routes.rb' # Allow both single-quoted and double-quoted string literals. Style/StringLiterals: diff --git a/app/controllers/my/emails_controller.rb b/app/controllers/my/emails_controller.rb index 1c749a3..78323b8 100644 --- a/app/controllers/my/emails_controller.rb +++ b/app/controllers/my/emails_controller.rb @@ -15,7 +15,7 @@ def create def set_primary email = Email.find(params[:id]) ActiveRecord::Base.transaction do - current_user.emails.where(primary: true).update_all(primary: false) + current_user.emails.where(primary: true).find_each { |email| email.update!(primary: false) } email.update!(primary: true) end diff --git a/app/models/email.rb b/app/models/email.rb index 317d9e0..f061482 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -1,4 +1,4 @@ -class Email < ActiveRecord::Base +class Email < ApplicationRecord belongs_to :user validates :email, presence: true, uniqueness: true diff --git a/app/models/user.rb b/app/models/user.rb index 19a6be6..18b3cf5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,7 +23,7 @@ class User < ApplicationRecord ) } scope :committee, -> { where(committee: true) } - scope :without_emails, -> { + scope :without_emails, lambda { left_outer_joins(:emails) .where(emails: { id: nil }) .where.not(email: nil) diff --git a/spec/factories/email.rb b/spec/factories/email.rb index 3e2897c..8a2fa58 100644 --- a/spec/factories/email.rb +++ b/spec/factories/email.rb @@ -4,7 +4,7 @@ email { Faker::Internet.email } trait :confirmed do - confirmed_at { Time.now } + confirmed_at { Time.current } end end end diff --git a/spec/models/email_spec.rb b/spec/models/email_spec.rb index db30a80..652adbc 100644 --- a/spec/models/email_spec.rb +++ b/spec/models/email_spec.rb @@ -27,7 +27,7 @@ context 'uniqueness' do it 'requires a unique email address regardless of case' do - existing_email = create(:email, email: 'TEST@example.com') + create(:email, email: 'TEST@example.com') new_email = build(:email, email: 'test@example.com') expect(new_email).not_to be_valid diff --git a/spec/requests/imported_members_spec.rb b/spec/requests/imported_members_spec.rb index 1be9a4d..9fcfc46 100644 --- a/spec/requests/imported_members_spec.rb +++ b/spec/requests/imported_members_spec.rb @@ -12,9 +12,9 @@ context "with valid parameters" do it "creates new imported members" do - expect { + expect do post admin_imported_members_path, params: { source: source, file: file } - }.to change(ImportedMember, :count).by(2) + end.to change(ImportedMember, :count).by(2) expect(response).to redirect_to(admin_imported_members_path) follow_redirect! @@ -45,9 +45,9 @@ it "updates the existing imported member" do existing_member = create(:imported_member, email: 'member1@example.com', full_name: 'Old Name', data: { sources: ['old_source'] }) - expect { + expect do post admin_imported_members_path, params: { source: source, file: file } - }.to change(ImportedMember, :count).by(1) + end.to change(ImportedMember, :count).by(1) existing_member.reload expect(existing_member.full_name).to eq('Old Name') @@ -59,9 +59,9 @@ it "skips the row" do create(:email, email: 'member1@example.com') - expect { + expect do post admin_imported_members_path, params: { source: source, file: file } - }.to change(ImportedMember, :count).by(1) + end.to change(ImportedMember, :count).by(1) expect(ImportedMember.find_by(email: 'member1@example.com')).to be_nil end @@ -71,9 +71,9 @@ let(:file) { fixture_file_upload('imported_members_blank_email.csv', 'text/csv') } it "skips the row" do - expect { + expect do post admin_imported_members_path, params: { source: source, file: file } - }.to change(ImportedMember, :count).by(1) + end.to change(ImportedMember, :count).by(1) expect(ImportedMember.find_by(email: '')).to be_nil end diff --git a/spec/requests/invitations_spec.rb b/spec/requests/invitations_spec.rb index 4e01461..0ca02d2 100644 --- a/spec/requests/invitations_spec.rb +++ b/spec/requests/invitations_spec.rb @@ -19,7 +19,6 @@ post "/invitations/#{imported_member.token}", params: { user: user_params } - expect(response).to redirect_to(root_path) expect(flash[:notice]).to eq("Your membership to Ruby Australia has been confirmed.") user = User.last @@ -32,7 +31,7 @@ end context "with invalid user params" do - let(:user_params) { { full_name: "", email: imported_member.email, password: "password", password_confirmation: "wrong_password" } } + let(:user_params) { { full_name: "", email: imported_member.email, password: "password", password_confirmation: "wrong_password" } } it "renders the new template" do post "/invitations/#{imported_member.token}", params: { user: user_params } diff --git a/spec/requests/mailing_lists_spec.rb b/spec/requests/mailing_lists_spec.rb index 03f176a..704d842 100644 --- a/spec/requests/mailing_lists_spec.rb +++ b/spec/requests/mailing_lists_spec.rb @@ -15,7 +15,7 @@ "Type" => "Subscribe", "EmailAddress" => user.email, "ListId" => list.api_id, - }, + } ], }.to_json end @@ -36,7 +36,7 @@ "Type" => "Deactivate", "EmailAddress" => user.email, "ListId" => list.api_id, - }, + } ], }.to_json end @@ -72,7 +72,7 @@ "Type" => "Subscribe", "EmailAddress" => "nonexistent@example.com", "ListId" => list.api_id, - }, + } ], }.to_json end diff --git a/spec/requests/my/emails_spec.rb b/spec/requests/my/emails_spec.rb index f9a900b..74585e7 100644 --- a/spec/requests/my/emails_spec.rb +++ b/spec/requests/my/emails_spec.rb @@ -17,18 +17,18 @@ describe "POST /create" do context "with valid parameters" do it "creates a new email and redirects to my_details_path" do - expect { + expect do post my_emails_path, params: { email: { email: "test@example.com" } } - }.to change(Email, :count).by(1) + end.to change(Email, :count).by(1) expect(response).to redirect_to(my_details_path) end end context "with invalid parameters" do it "re-renders the new template" do - expect { + expect do post my_emails_path, params: { email: { email: "" } } - }.not_to change(Email, :count) + end.not_to change(Email, :count) expect(response).to render_template(:new) end end @@ -51,9 +51,9 @@ let!(:email) { create(:email, user: user) } it "deletes the specified email and redirects to my_details_path" do - expect { + expect do delete my_email_path(email) - }.to change(Email, :count).by(-1) + end.to change(Email, :count).by(-1) expect(response).to redirect_to(my_details_path) end end diff --git a/spec/requests/reactivations_spec.rb b/spec/requests/reactivations_spec.rb index c0f1a2c..f9c8725 100644 --- a/spec/requests/reactivations_spec.rb +++ b/spec/requests/reactivations_spec.rb @@ -29,9 +29,9 @@ end it "does not create a new membership" do - expect { + expect do post "/reactivations", params: { user: { email: user.email, password: "wrong_password" } } - }.not_to change(Membership, :count) + end.not_to change(Membership, :count) end it "does not sign in the user" do