From 31d45b527ebe69ef7b7b768d150b950a5d509a34 Mon Sep 17 00:00:00 2001 From: VladislavSokov Date: Thu, 31 Aug 2023 17:24:01 +0300 Subject: [PATCH] feat(unaccepted shares) My accounts show unaccepted shares --- app/controllers/my_accounts_controller.rb | 4 ++ .../my_accounts/_account_share.html.slim | 5 +++ app/views/my_accounts/show.html.erb | 40 ----------------- app/views/my_accounts/show.html.slim | 45 +++++++++++++++++++ spec/controllers/my_accounts_spec.rb | 20 ++++++++- 5 files changed, 72 insertions(+), 42 deletions(-) create mode 100644 app/views/my_accounts/_account_share.html.slim delete mode 100644 app/views/my_accounts/show.html.erb create mode 100644 app/views/my_accounts/show.html.slim diff --git a/app/controllers/my_accounts_controller.rb b/app/controllers/my_accounts_controller.rb index e31a6500..f88ce3a2 100644 --- a/app/controllers/my_accounts_controller.rb +++ b/app/controllers/my_accounts_controller.rb @@ -14,4 +14,8 @@ def show; end helper_method memoize def shared_accounts Account.shared_for(current_user) end + + helper_method memoize def unaccepted_shares + AccountShare.unaccepted.for(current_user) + end end diff --git a/app/views/my_accounts/_account_share.html.slim b/app/views/my_accounts/_account_share.html.slim new file mode 100644 index 00000000..8230820f --- /dev/null +++ b/app/views/my_accounts/_account_share.html.slim @@ -0,0 +1,5 @@ +tr + td= account_share.account.name + td= account_share.account.parent.email + td= account_share.created_at.to_formatted_s(:short) + td= link_to 'link', accept_account_share_url(token: account_share.token) diff --git a/app/views/my_accounts/show.html.erb b/app/views/my_accounts/show.html.erb deleted file mode 100644 index 390bb1b2..00000000 --- a/app/views/my_accounts/show.html.erb +++ /dev/null @@ -1,40 +0,0 @@ -<%= [current_user.name, account.email].select(&:present?).join(" ") %> -
-Account balance: <%= account.balance %> -
-<% account.children.each do |child| %> -
-
-
-
-

<%= link_to child.name, account_path(child) %>

-

<%= child.balance %>

-
-
-
-
- <%= link_to '+ Add', new_account_topup_path(child), class: 'card-footer-item' %> - <%= link_to '- Spend', new_account_spend_path(child), class: 'card-footer-item' %> -
-
-<% end %> -<%= link_to 'add account', new_account_path %> -
-Shared accounts -<% shared_accounts.each do |account| %> -
-
-
-
-

<%= "Owner: #{account.parent.email}" %>

-

<%= link_to account.name, account_path(account) %>

-

<%= account.balance %>

-
-
-
- -
-<% end %> diff --git a/app/views/my_accounts/show.html.slim b/app/views/my_accounts/show.html.slim new file mode 100644 index 00000000..7a446bdb --- /dev/null +++ b/app/views/my_accounts/show.html.slim @@ -0,0 +1,45 @@ += [current_user.name, account.email].select(&:present?).join(" ") +br +| Account balance: #{account.balance} +br +- account.children.each do |child| + .card.box + .card-content + .media + .media-content + p.title.is-4= link_to child.name, account_path(child) + p.subtitle.is-6= child.balance + footer.card-footer + = link_to '+ Add', new_account_topup_path(child), class: 'card-footer-item' + = link_to '- Spend', new_account_spend_path(child), class: 'card-footer-item' + += link_to 'add account', new_account_path +br +| Shared accounts +- shared_accounts.each do |shared_account| + .card.box + .card-content + .media + .media-content + p.title.is-6 Owner: #{shared_account.parent.email} + p.title.is-4= link_to shared_account.name, account_path(shared_account) + p.subtitle.is-6= shared_account.balance + footer.card-footer + = link_to '+ Add', new_account_topup_path(shared_account), class: 'card-footer-item' + = link_to '- Spend', new_account_spend_path(shared_account), class: 'card-footer-item' + +- if unaccepted_shares.present? + h2.title + | Unaccepted shares + .columns + .column + .card + .card-content + table.table.is-fullwidth.is-striped + thead + tr + th Account + th From + th Created + th Link + = render partial: 'account_share', collection: unaccepted_shares.order(created_at: :desc) diff --git a/spec/controllers/my_accounts_spec.rb b/spec/controllers/my_accounts_spec.rb index 6ae70a3f..0bf84415 100644 --- a/spec/controllers/my_accounts_spec.rb +++ b/spec/controllers/my_accounts_spec.rb @@ -25,11 +25,27 @@ end context 'when account share accepted' do let(:accepted_at) { Time.current } - it { expect(controller.send(:shared_accounts, shared_user)).to include(account) } + it { expect(controller.send(:shared_accounts)).to include(account) } end context 'when account share unaccepted' do let(:accepted_at) { nil } - it { expect(controller.send(:shared_accounts, shared_user)).to be_empty } + it { expect(controller.send(:shared_accounts)).to be_empty } + end + end + + describe '#unaccepted_shares' do + let(:shared_user) { create(:user) } + let!(:account_share) { create(:account_share, user: user, account: account, email: shared_user.email, accepted_at: accepted_at) } + + before { sign_in shared_user } + + context 'when account share accepted' do + let(:accepted_at) { Time.current } + it { expect(controller.send(:unaccepted_shares)).to be_empty } + end + context 'when account share unaccepted' do + let(:accepted_at) { nil } + it { expect(controller.send(:unaccepted_shares)).to include(account_share) } end end end