Skip to content

Commit

Permalink
feat(child-notifications): check email notification (#209)
Browse files Browse the repository at this point in the history
* feat(child-notifications): check email notification

* feat(child-notifications): change solutions

* feat(child-notifications): remove the garbage
  • Loading branch information
andreybakanovsky authored Oct 23, 2023
1 parent afd4efd commit b9f06ee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Account < ApplicationRecord
has_one_attached :avatar

validates :name, presence: true
validates :email, presence: true, if: :notification?

scope :unarchived, -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
Expand Down
22 changes: 19 additions & 3 deletions app/views/accounts/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
| Add photo
.field
= f.text_field :name, autofocus: true, class: "input", placeholder: "Name"
br
br
.field
= f.email_field :email, autocomplete: "email", placeholder: "email"
.field
= f.check_box :notification, class: "checkbox"
= f.check_box :notification, class: "checkbox", id: 'notification-check-box'
| &nbsp;
= f.label :notification, "Report transactions"
.field
= f.email_field :email, disabled: !account.notification, autocomplete: "email", id:"email-input", placeholder: "email"
.notification.is-light.mt-1
| You can check the box and specify the email of the person for whom you have created this account to send them notifications about balance changes
br
buttons.is-flex.is-justify-content-flex-end
= link_to account_path(account), class: 'button is-light' do
Expand All @@ -34,3 +38,15 @@
span.icon
i.fa.fa-hdd-o
| &nbsp; Save

javascript:
document.addEventListener('turbolinks:load', function () {
const checkbox = document.getElementById('notification-check-box');
const input = document.getElementById('email-input');
checkbox.addEventListener('click', function (e) {
input.disabled = !checkbox.checked
input.focus()
})
});
34 changes: 26 additions & 8 deletions spec/controllers/accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,34 @@
end

describe '#update' do
let(:name) { FFaker::Name.first_name }
let(:old_name) { old_name = account.name }
subject(:update) { patch :update, params: { id: account, account: attributes } }

subject(:bad_update) { patch :update, params: { id: user.account, account: { name: nil }} }
subject(:update) { patch :update, params: { id: user.account, account: { name: name }} }
context 'with valid params' do
let(:new_name) { FFaker::Name.first_name }
let(:attributes) {{ name: new_name }}

it { is_expected.to have_http_status(:redirect) }
it { expect(bad_update).not_to be_redirect }
it 'name changed' do
expect(Account.where(id: account.id).name).not_to eq(old_name)
it { is_expected.to have_http_status(:redirect) }
it { expect { subject }.to change { account.reload.name }.to(new_name) }
end

context 'with invalid params' do
let(:attributes) {{ name: nil }}

it { expect(subject).not_to be_redirect }
end

context 'with notification and email' do
let(:attributes) {{ notification: true, email: FFaker::Internet.email }}

it { is_expected.to have_http_status(:redirect) }
it { expect { subject }.to change { account.reload.notification }.to(true) }
end

context 'with notification and empty email' do
let(:account) { create(:account, :with_notify) }
let(:attributes) {{ notification: true, email: '' }}

it { expect(subject).not_to be_redirect }
end
end

Expand Down

0 comments on commit b9f06ee

Please sign in to comment.