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(archive account): add account archiving feature #124

Merged
merged 8 commits into from
Sep 26, 2023

Conversation

VladislavSokov
Copy link
Contributor

No description provided.

unless accessed?
flash.now.notice = 'Access denied!.'
return render :notification
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should just raise active record not found, i.e. fix the code so that it finds with "!" on a specific scope.


account.update!(archived_at: Time.current)
AccountShare.where(account_id: account).delete_all
AccountAutomaticTopupConfig.where(to_account: account).delete_all
Copy link
Member

@ka8725 ka8725 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also archive AccountAutomaticTopupConfig, no need to delete data from DB

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladislavSokov I have a better idea. Let's not change anything except the account. However, the place that selects the top-up configurations should be changed to filter those related to deleted accounts.

@@ -3,7 +3,7 @@
class Account < ApplicationRecord
has_many :income_transactions, class_name: 'Transaction', foreign_key: :to_account_id
has_many :outcome_transactions, class_name: 'Transaction', foreign_key: :from_account_id
has_many :children, class_name: 'Account', foreign_key: :parent_id
has_many :children, -> { where(archived_at: nil) }, class_name: 'Account', foreign_key: :parent_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a scope unarchived and use it here and below.


def accessed?
current_user.id == (account.user ? account.user.id : account.parent.user.id)
rescue ActiveRecord::RecordNotFound
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why catch it?

return redirect_to account_path unless accessed?

account.update!(archived_at: Time.current)
AccountShare.where(account_id: account).delete_all
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is needed? We should change only the account here. All other related data, if it affects the system, should definitely be disabled. But it should be disabled only by filtering, e.g. checking if the related account is not archived.

Copy link
Member

@ka8725 ka8725 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 35 to 37
return redirect_to account_path unless accessed?

account.update!(archived_at: Time.current)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT if we follow the same pattern with 404 here as well?

In this case, it's ok to not use the account helper method below but create a variable here:

account = Account.<scope>.find(ps.fetch(:id))
account.update!(archived_at: Time.current)
redirect_to my_account_path

@@ -1,4 +1,6 @@
class AccountAutomaticTopupConfig < ApplicationRecord
belongs_to :from_account, class_name: 'Account', foreign_key: :from_account_id
belongs_to :to_account, class_name: 'Account', foreign_key: :to_account_id

scope :active, -> { where(to_account: { archived_at: nil }) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scope :active, -> { where(to_account: { archived_at: nil }) }
scope :visible, -> { where(to_account: { archived_at: nil }) }

@@ -7,6 +7,7 @@ class AccountShare < ApplicationRecord
scope :for, ->(user) { where(email: user.email) }
scope :accepted, -> { where.not(accepted_at: nil) }
scope :unaccepted, -> { where(accepted_at: nil) }
scope :active, -> { includes(:account).where(accounts: { archived_at: nil }) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scope :active, -> { includes(:account).where(accounts: { archived_at: nil }) }
scope :visible, -> { includes(:account).where(accounts: { archived_at: nil }) }

@ka8725 ka8725 merged commit 1358aae into master Sep 26, 2023
2 checks passed
@ka8725 ka8725 deleted the add-archive-method-to-accounts branch September 26, 2023 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants