From 0b247d390b1229579752d1dde46e41f92588ac70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Wed, 11 Aug 2021 23:30:51 -0300 Subject: [PATCH] add fiat balances dashboard --- .../admin/fiat_balances_controller.rb | 8 +++ app/dashboards/fiat_balance_dashboard.rb | 53 +++++++++++++++++++ app/models/fiat_balance.rb | 4 +- config/locales/activerecord.yml | 17 ++++++ config/routes.rb | 1 + spec/models/fiat_balance_spec.rb | 4 +- 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 app/controllers/admin/fiat_balances_controller.rb create mode 100644 app/dashboards/fiat_balance_dashboard.rb diff --git a/app/controllers/admin/fiat_balances_controller.rb b/app/controllers/admin/fiat_balances_controller.rb new file mode 100644 index 0000000..7ae29aa --- /dev/null +++ b/app/controllers/admin/fiat_balances_controller.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +module Admin + class FiatBalancesController < Admin::ApplicationController + def valid_action?(name, resource = resource_class) + ["destroy"].exclude?(name.to_s) && super + end + end +end diff --git a/app/dashboards/fiat_balance_dashboard.rb b/app/dashboards/fiat_balance_dashboard.rb new file mode 100644 index 0000000..227461b --- /dev/null +++ b/app/dashboards/fiat_balance_dashboard.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true +require "administrate/base_dashboard" + +class FiatBalanceDashboard < Administrate::BaseDashboard + # ATTRIBUTE_TYPES + # a hash that describes the type of each of the model's fields. + # + # Each different type represents an Administrate::Field object, + # which determines how the attribute is displayed + # on pages throughout the dashboard. + ATTRIBUTE_TYPES = { + id: Field::Number, + user: Field::BelongsTo, + amount_formatted: Field::String, + created_at: Field::DateTime, + updated_at: Field::DateTime, + }.freeze + + # COLLECTION_ATTRIBUTES + # an array of attributes that will be displayed on the model's index page. + # + # By default, it's limited to four items to reduce clutter on index pages. + # Feel free to add, remove, or rearrange items. + COLLECTION_ATTRIBUTES = [:id, :user, :amount_formatted].freeze + + # SHOW_PAGE_ATTRIBUTES + # an array of attributes that will be displayed on the model's show page. + SHOW_PAGE_ATTRIBUTES = [:user, :id, :amount_formatted, :created_at, :updated_at].freeze + + # FORM_ATTRIBUTES + # an array of attributes that will be displayed + # on the model's form (`new` and `edit`) pages. + FORM_ATTRIBUTES = [:user, :amount_formatted].freeze + + # COLLECTION_FILTERS + # a hash that defines filters that can be used while searching via the search + # field of the dashboard. + # + # For example to add an option to search for open resources by typing "open:" + # in the search field: + # + # COLLECTION_FILTERS = { + # open: ->(resources) { resources.where(open: true) } + # }.freeze + COLLECTION_FILTERS = {}.freeze + + # Overwrite this method to customize how fiat balances are displayed + # across all pages of the admin dashboard. + # + # def display_resource(fiat_balance) + # "FiatBalance ##{fiat_balance.id}" + # end +end diff --git a/app/models/fiat_balance.rb b/app/models/fiat_balance.rb index 6b7fb78..8722278 100644 --- a/app/models/fiat_balance.rb +++ b/app/models/fiat_balance.rb @@ -24,5 +24,7 @@ class FiatBalance < ApplicationRecord monetize :amount_cents - validates :amount, presence: true + def amount_formatted + amount.format + end end diff --git a/config/locales/activerecord.yml b/config/locales/activerecord.yml index 802053a..03538e7 100644 --- a/config/locales/activerecord.yml +++ b/config/locales/activerecord.yml @@ -4,17 +4,34 @@ pt-BR: user_document: one: Documentos de Usuário other: Documentos de Usuários + admin_user: one: Administrador other: Administradores + currency: one: Moeda other: Moedas + balance: one: Saldo other: Saldos + + fiat_balance: + one: Saldo Fiat + other: Saldos Fiat + attributes: user: first_name: Primeiro nome last_name: Último nome full_name: Nome completo + + balance: + amount: Quantia + + fiat_balance: + amount_formatted: Quantia + + currency: + name: Nome \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 6fb3107..bdc7191 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ Rails.application.routes.draw do namespace :admin do resources :users resources :balances + resources :fiat_balances resources :currencies resources :admin_users diff --git a/spec/models/fiat_balance_spec.rb b/spec/models/fiat_balance_spec.rb index f82fe37..704bfc1 100644 --- a/spec/models/fiat_balance_spec.rb +++ b/spec/models/fiat_balance_spec.rb @@ -22,5 +22,7 @@ require "rails_helper" RSpec.describe(FiatBalance, type: :model) do - pending "add some examples to (or delete) #{__FILE__}" + describe "associations" do + it { is_expected.to(belong_to(:user)) } + end end