2024-11-04 20:27:31 -05:00
|
|
|
module AccountableResource
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2025-03-07 20:35:54 +05:30
|
|
|
include ScrollFocusable, Periodable
|
2025-01-30 14:12:01 -05:00
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
before_action :set_account, only: [ :show, :edit, :update, :destroy ]
|
2024-11-15 13:49:37 -05:00
|
|
|
before_action :set_link_token, only: :new
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def permitted_accountable_attributes(*attrs)
|
|
|
|
@permitted_accountable_attributes = attrs if attrs.any?
|
|
|
|
@permitted_accountable_attributes ||= [ :id ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@account = Current.family.accounts.build(
|
|
|
|
currency: Current.family.currency,
|
2024-11-15 13:49:37 -05:00
|
|
|
accountable: accountable_type.new
|
2024-11-04 20:27:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2025-03-07 17:35:55 -05:00
|
|
|
@chart_view = params[:chart_view] || "balance"
|
2025-01-30 14:12:01 -05:00
|
|
|
@q = params.fetch(:q, {}).permit(:search)
|
|
|
|
entries = @account.entries.search(@q).reverse_chronological
|
|
|
|
|
|
|
|
set_focused_record(entries, params[:focused_record_id])
|
|
|
|
|
|
|
|
@pagy, @entries = pagy(entries, limit: params[:per_page] || "10", params: ->(params) { params.except(:focused_record_id) })
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@account = Current.family.accounts.create_and_sync(account_params.except(:return_to))
|
2024-11-15 13:49:37 -05:00
|
|
|
redirect_to account_params[:return_to].presence || @account, notice: t("accounts.create.success", type: accountable_type.name.underscore.humanize)
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@account.update_with_sync!(account_params.except(:return_to))
|
2024-11-15 13:49:37 -05:00
|
|
|
redirect_back_or_to @account, notice: t("accounts.update.success", type: accountable_type.name.underscore.humanize)
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2024-11-15 13:49:37 -05:00
|
|
|
@account.destroy_later
|
|
|
|
redirect_to accounts_path, notice: t("accounts.destroy.success", type: accountable_type.name.underscore.humanize)
|
2024-11-04 20:27:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2024-11-15 13:49:37 -05:00
|
|
|
def set_link_token
|
2025-01-31 17:04:26 -05:00
|
|
|
@us_link_token = Current.family.get_link_token(
|
2025-02-07 10:35:42 -05:00
|
|
|
webhooks_url: plaid_us_webhooks_url,
|
2024-11-15 13:49:37 -05:00
|
|
|
redirect_url: accounts_url,
|
2025-01-31 12:13:58 -06:00
|
|
|
accountable_type: accountable_type.name,
|
2025-01-31 17:04:26 -05:00
|
|
|
region: :us
|
2024-11-15 13:49:37 -05:00
|
|
|
)
|
2025-01-31 17:04:26 -05:00
|
|
|
|
|
|
|
if Current.family.eu?
|
|
|
|
@eu_link_token = Current.family.get_link_token(
|
2025-02-07 10:35:42 -05:00
|
|
|
webhooks_url: plaid_eu_webhooks_url,
|
2025-01-31 17:04:26 -05:00
|
|
|
redirect_url: accounts_url,
|
|
|
|
accountable_type: accountable_type.name,
|
|
|
|
region: :eu
|
|
|
|
)
|
|
|
|
end
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-02-07 10:35:42 -05:00
|
|
|
def plaid_us_webhooks_url
|
2024-11-15 13:49:37 -05:00
|
|
|
return webhooks_plaid_url if Rails.env.production?
|
|
|
|
|
2025-02-07 10:35:42 -05:00
|
|
|
ENV.fetch("DEV_WEBHOOKS_URL", root_url.chomp("/")) + "/webhooks/plaid"
|
|
|
|
end
|
|
|
|
|
|
|
|
def plaid_eu_webhooks_url
|
|
|
|
return webhooks_plaid_eu_url if Rails.env.production?
|
|
|
|
|
|
|
|
ENV.fetch("DEV_WEBHOOKS_URL", root_url.chomp("/")) + "/webhooks/plaid_eu"
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
def accountable_type
|
|
|
|
controller_name.classify.constantize
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_account
|
|
|
|
@account = Current.family.accounts.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_params
|
|
|
|
params.require(:account).permit(
|
2024-11-15 13:49:37 -05:00
|
|
|
:name, :is_active, :balance, :subtype, :currency, :accountable_type, :return_to,
|
2024-11-04 20:27:31 -05:00
|
|
|
accountable_attributes: self.class.permitted_accountable_attributes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|