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

feat(restore accounts): add restore for archived accounts #165

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/controllers/archived_accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class ArchivedAccountsController < ApplicationController
before_action :authenticate_user!

def index; end

def restore
archived_account.update!(archived_at: nil)
redirect_to archived_accounts_path
end

private

helper_method memoize def archived_accounts
Account.visible_for_owner(current_user).archived
end

helper_method memoize def archived_account
archived_accounts.find(ps.fetch(:id))
end
end
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Account < ApplicationRecord
validates :name, presence: true

scope :unarchived, -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }

scope :visible_for, lambda { |current_user|
where(id: [current_user.account_id] +
Expand Down
29 changes: 29 additions & 0 deletions app/views/archived_accounts/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
h2.title.is-4
| Archived accounts

.buttons.for-desktop
= link_to my_account_path, class: 'button is-light' do
span.icon
i.fa.fa-chevron-left
| &nbsp; Back

.buttons.for-mobile
= link_to my_account_path, class: 'button is-light' do
span.icon
i.fa.fa-chevron-left

- if archived_accounts.present?
- archived_accounts.each do |account|
.card.box
.card-content
.columns
.column
p.title.is-4= account.name
.column
nav.level
.level-item.has-text-centered
div
p.heading Balance
p.title #{account.balance}
.column.is-flex.is-justify-content-flex-end
= link_to 'Restore', restore_archived_account_path(account), class: 'button is-primary'
4 changes: 3 additions & 1 deletion app/views/my_accounts/show.html.slim
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
.columns
.column.is-flex.is-justify-content-flex-end
- if account.children.unarchived.present? || shared_accounts.present?
= button_to new_account_path, method: :get, class: 'button is-primary' do
= button_to new_account_path, method: :get, class: 'button is-primary mr-2' do
span.icon
i.fa.fa-plus
| &nbsp; Add account
= link_to archived_accounts_path, method: :get, class: 'button is-primary' do
| &nbsp; Archived accounts

- if account.children.unarchived.present? || shared_accounts.present?
- account.children.unarchived.each do |child|
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
resources :objectives, only: [:destroy]
resources :public_accounts, param: :token, only: :show, controller: 'public_account_shares'
resources :feedbacks, only: [:new, :create]
resources :archived_accounts, only: %i[index] do
member do
get 'restore', action: 'restore'
end
end
end
43 changes: 43 additions & 0 deletions spec/controllers/archived_accounts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ArchivedAccountsController, type: :controller do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }
let!(:archived_account) { create(:account, :children, parent: account, archived_at: Time.current) }

before { sign_in user }

describe '#index' do
subject(:index) { get :index }

it { is_expected.to have_http_status(:success) }
it { is_expected.to render_template(:index) }
it { is_expected.to_not render_template('home/index') }
end

describe '#archived_accounts' do
context 'when user have archived account' do
it { expect(controller.send(:archived_accounts)).to include(archived_account) }
end
end

describe '#restore' do
subject(:restore) { get :restore, params: { id: archived_account.id } }

context 'when the current user owns the account' do
it { expect { subject }.to change { archived_account.reload.archived_at }.to(nil) }
it { is_expected.to redirect_to(archived_accounts_path) }
end

context 'when the current user does not own the account' do
let(:other_account) { create(:account, :parent) }
let(:other_user) { create(:user, account: other_account) }

before { sign_in other_user }

it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
end