From 2ff049707dbdbce41911ec200df03360d81a474b Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 18 Aug 2021 22:06:46 +0300 Subject: [PATCH 01/13] added registrar credit changing functionality --- app/controllers/admin/accounts_controller.rb | 46 ++++++++++++++++ app/models/ability.rb | 1 + app/models/account.rb | 1 + app/views/admin/accounts/_account.html.erb | 7 +++ app/views/admin/accounts/_form.html.erb | 52 +++++++++++++++++++ app/views/admin/accounts/_search_form.haml | 34 ++++++++++++ .../admin/accounts/_search_form.html.erb | 32 ++++++++++++ app/views/admin/accounts/edit.html.erb | 5 ++ app/views/admin/accounts/index.html.erb | 38 ++++++++++++++ app/views/admin/accounts/new.html.erb | 5 ++ app/views/admin/accounts/show.html.erb | 4 ++ app/views/admin/base/_menu.haml | 1 + app/views/admin/registrars/index.html.erb | 2 +- .../admin/registrars/show/_details.html.erb | 2 +- config/locales/admin/accounts.en.yml | 21 ++++++++ config/locales/en.yml | 1 + config/routes.rb | 1 + test/system/admin_area/accounts_test.rb | 20 +++++++ 18 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 app/controllers/admin/accounts_controller.rb create mode 100644 app/views/admin/accounts/_account.html.erb create mode 100644 app/views/admin/accounts/_form.html.erb create mode 100644 app/views/admin/accounts/_search_form.haml create mode 100644 app/views/admin/accounts/_search_form.html.erb create mode 100644 app/views/admin/accounts/edit.html.erb create mode 100644 app/views/admin/accounts/index.html.erb create mode 100644 app/views/admin/accounts/new.html.erb create mode 100644 app/views/admin/accounts/show.html.erb create mode 100644 config/locales/admin/accounts.en.yml create mode 100644 test/system/admin_area/accounts_test.rb diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb new file mode 100644 index 000000000..c3f72371a --- /dev/null +++ b/app/controllers/admin/accounts_controller.rb @@ -0,0 +1,46 @@ +module Admin + class AccountsController < BaseController + load_and_authorize_resource + + def index + @q = Account.includes(:registrar).search(params[:q]) + @accounts = @q.result.page(params[:page]) + @accounts = @accounts.per(params[:results_per_page]) if params[:results_per_page].to_i.positive? + + render_by_format('admin/accounts/index', 'accounts') + end + + def show; end + + def edit; end + + def update + if @account.valid? + @sum = params[:account][:balance].to_f - @account.balance + redirect_to admin_accounts_path, notice: t('.updated') if create_activity + else + render 'edit' + end + end + + private + + def create_activity + activity = AccountActivity.new(account: @account, + sum: @sum, + currency: @account.currency, + description: params[:description], + activity_type: AccountActivity::ADD_CREDIT) + + if activity.save + true + else + false + end + end + + def account_params + params.require(:account).permit(:id, :currency, :balance) + end + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb index 66a8793bc..febde4e8b 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -101,6 +101,7 @@ class Ability can :manage, BankTransaction can :manage, Invoice can :manage, WhiteIp + can :manage, Account can :manage, AccountActivity can :manage, Dispute can :read, ApiLog::EppLog diff --git a/app/models/account.rb b/app/models/account.rb index 420f43e48..fe0820888 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,4 +1,5 @@ class Account < ApplicationRecord + extend ToCsv include Versions belongs_to :registrar, required: true diff --git a/app/views/admin/accounts/_account.html.erb b/app/views/admin/accounts/_account.html.erb new file mode 100644 index 000000000..43a9a2c06 --- /dev/null +++ b/app/views/admin/accounts/_account.html.erb @@ -0,0 +1,7 @@ + + <%= account.id %> + <%= account.balance %> + <%= account.currency %> + <%= link_to account.registrar, admin_registrar_path(account.registrar) %> + <%= link_to(t(:edit_balance), edit_admin_account_path(account), class: 'btn btn-primary btn-xs') %> + diff --git a/app/views/admin/accounts/_form.html.erb b/app/views/admin/accounts/_form.html.erb new file mode 100644 index 000000000..01533cf20 --- /dev/null +++ b/app/views/admin/accounts/_form.html.erb @@ -0,0 +1,52 @@ +<%= form_for([:admin, @account], html: { class: 'form-horizontal', autocomplete: 'off' }) do |f| %> + <%= render 'form_errors', target: @account %> + +
+
+
+
+ <%= f.label :id, nil, class: 'required' %> +
+
+ <%= f.text_field :id, required: true, autofocus: true, + class: 'form-control', disabled: true %> +
+
+ +
+
+ <%= f.label :currency, nil, class: 'required' %> +
+
+ <%= f.text_field :currency, required: true, class: 'form-control', disabled: true %> +
+
+ +
+
+ <%= f.label :balance %> +
+
+ <%= f.text_field(:balance, class: 'form-control') %> +
+
+ +
+
+ <%= f.label :description %> +
+
+ <%= text_area_tag :description, params[:description], class: 'form-control' %> +
+
+
+
+ +
+ +
+
+ <%= button_tag t('.save_btn'), class: 'btn btn-warning' %> +
+
+<% end %> diff --git a/app/views/admin/accounts/_search_form.haml b/app/views/admin/accounts/_search_form.haml new file mode 100644 index 000000000..f9cbfccaa --- /dev/null +++ b/app/views/admin/accounts/_search_form.haml @@ -0,0 +1,34 @@ +.row + .col-md-12 + = search_form_for [:admin, @q], html: { style: 'margin-bottom: 0;', class: 'js-form', autocomplete: 'off' } do |f| + .row + .col-md-3 + .form-group + = label_tag :name + = f.search_field :name, value: params[:q][:name], class: 'form-control', placeholder: t(:name) + .col-md-3 + .form-group + = label_tag :registrant + = f.search_field :registrant, value: params[:q][:registrant], class: 'form-control', placeholder: t('.registrant_placeholder') + .col-md-3 + .form-group + = label_tag t(:registrar_name) + = select_tag '[q][registrar]', options_for_select(Registrar.all.map { |x| [x, x.name] }, selected: params[:q][:registrar]), { include_blank: true, class: 'form-control', placeholder: t('.registrant')} + .col-md-3 + .form-group + = label_tag :action + = select_tag '[q][event]', options_for_select([['Update', 'update'], ['Destroy', 'destroy'], ['Create', 'create']], params[:q][:event]), { include_blank:true, multiple: false, placeholder: t(:choose), class: 'form-control js-combobox' } + .row + .col-md-3 + .col-md-3 + .col-md-3 + .form-group + = label_tag t(:results_per_page) + = text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) + .col-md-3{style: 'padding-top: 25px;'} + %button.btn.btn-primary +   + %span.glyphicon.glyphicon-search +   + = link_to(t('.reset_btn'), admin_domain_versions_path, class: 'btn btn-default') +%hr diff --git a/app/views/admin/accounts/_search_form.html.erb b/app/views/admin/accounts/_search_form.html.erb new file mode 100644 index 000000000..9a0a83521 --- /dev/null +++ b/app/views/admin/accounts/_search_form.html.erb @@ -0,0 +1,32 @@ +
+
+ <%= search_form_for @q, url: [:admin, :accounts], html: { style: 'margin-bottom: 0;' } do |f| %> +
+
+
+ <%= f.label t(:registrar_name) %> + <%= f.select :registrar_id_in, Registrar.all.map { |x| [x, x.id] }, {}, class: 'form-control js-combobox', placeholder: t(:choose), multiple: true %> +
+
+
+
+ <%= label_tag t(:results_per_page) %> + <%= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) %> +
+
+ +
+ + <%= button_tag t('.download_csv_btn'), + formaction: admin_accounts_path(format: 'csv'), + class: 'btn btn-default' %> + <%= link_to(t('.reset_btn'), admin_accounts_path, class: 'btn btn-default') %> +
+
+ <% end %> +
+
diff --git a/app/views/admin/accounts/edit.html.erb b/app/views/admin/accounts/edit.html.erb new file mode 100644 index 000000000..d3b22c2b0 --- /dev/null +++ b/app/views/admin/accounts/edit.html.erb @@ -0,0 +1,5 @@ +

Editing Account

+ +<%= render 'form', account: @account %> + +<%= link_to 'Back', admin_accounts_path %> diff --git a/app/views/admin/accounts/index.html.erb b/app/views/admin/accounts/index.html.erb new file mode 100644 index 000000000..60ecdb15b --- /dev/null +++ b/app/views/admin/accounts/index.html.erb @@ -0,0 +1,38 @@ +

<%= t '.header' %>

+<%= render 'search_form', search: @search %> + +
+
+
+ + + + + + + + + + + + + <%= render @accounts %> + +
<%= sort_link(@q, 'ID', t('.')) %><%= sort_link(@q, 'Balance') %><%= sort_link(@q, 'Currency') %><%= sort_link(@q, 'registrar_name', Registrar.model_name.human) %> + <%= t(:actions) %> +
+
+
+
+
+ +
+
+ <%= paginate @accounts %> +
+
+ +
+
diff --git a/app/views/admin/accounts/new.html.erb b/app/views/admin/accounts/new.html.erb new file mode 100644 index 000000000..562a15b8e --- /dev/null +++ b/app/views/admin/accounts/new.html.erb @@ -0,0 +1,5 @@ +

New Admin Account

+ +<%= render 'form', admin_account: @admin_account %> + +<%= link_to 'Back', admin_accounts_path %> diff --git a/app/views/admin/accounts/show.html.erb b/app/views/admin/accounts/show.html.erb new file mode 100644 index 000000000..ef5583e83 --- /dev/null +++ b/app/views/admin/accounts/show.html.erb @@ -0,0 +1,4 @@ +

<%= notice %>

+ +<%= link_to 'Edit', edit_admin_account_path(@account) %> | +<%= link_to 'Back', admin_accounts_path %> diff --git a/app/views/admin/base/_menu.haml b/app/views/admin/base/_menu.haml index 46910afa7..c5edd4708 100644 --- a/app/views/admin/base/_menu.haml +++ b/app/views/admin/base/_menu.haml @@ -21,6 +21,7 @@ %li= link_to t('.prices'), admin_prices_path %li= link_to t(:bank_statements), admin_bank_statements_path %li= link_to t(:invoices), admin_invoices_path + %li= link_to t(:accounts), admin_accounts_path %li= link_to t(:account_activities), admin_account_activities_path(created_after: 'today') %li.divider %li.dropdown-header= t('.archive') diff --git a/app/views/admin/registrars/index.html.erb b/app/views/admin/registrars/index.html.erb index 7095fd0ef..23f29115c 100644 --- a/app/views/admin/registrars/index.html.erb +++ b/app/views/admin/registrars/index.html.erb @@ -45,7 +45,7 @@ <%= x.reg_no %> - <%= "#{x.balance}" %> + <%= link_to "#{x.balance}", edit_admin_account_path(x.cash_account) %> <%= "#{x.test_registrar}" %> diff --git a/app/views/admin/registrars/show/_details.html.erb b/app/views/admin/registrars/show/_details.html.erb index 9ae9aac3f..28194ca0a 100644 --- a/app/views/admin/registrars/show/_details.html.erb +++ b/app/views/admin/registrars/show/_details.html.erb @@ -15,7 +15,7 @@
<%= registrar.code %>
<%= Registrar.human_attribute_name :balance %>
-
<%= number_to_currency registrar.balance %>
+
<%= link_to (number_to_currency registrar.balance), edit_admin_account_path(registrar.cash_account) %>
<%= Registrar.human_attribute_name :website %>
<%= registrar.website %>
diff --git a/config/locales/admin/accounts.en.yml b/config/locales/admin/accounts.en.yml new file mode 100644 index 000000000..d94b1775d --- /dev/null +++ b/config/locales/admin/accounts.en.yml @@ -0,0 +1,21 @@ +en: + admin: + accounts: + index: + header: Accounts + + update: + updated: Account has been successfully updated + + form: + update_btn: Update account + reset_btn: Reset + save_btn: Save + + search_form: + registrar_name: Registrar + reset_btn: Reset + download_csv_btn: CSV + + account: + edit_balance: Edit balance diff --git a/config/locales/en.yml b/config/locales/en.yml index 944c99367..4566d2302 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -358,6 +358,7 @@ en: contact_org_error: 'Parameter value policy error. Org must be blank' contact_fax_error: 'Parameter value policy error. Fax must be blank' invoices: 'Invoices' + accounts: 'Accounts' no_such_user: 'No such user' phone_no: 'Phone number' confirmation_sms_was_sent_to_your_phone_verification_code_is: 'Confirmation sms was sent to your phone. Verification code is %{code}.' diff --git a/config/routes.rb b/config/routes.rb index 0db226cf1..1a3b394d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -236,6 +236,7 @@ Rails.application.routes.draw do end end + resources :accounts resources :account_activities resources :bank_statements do diff --git a/test/system/admin_area/accounts_test.rb b/test/system/admin_area/accounts_test.rb new file mode 100644 index 000000000..7c8344080 --- /dev/null +++ b/test/system/admin_area/accounts_test.rb @@ -0,0 +1,20 @@ +require 'application_system_test_case' + +class AdminAccountsSystemTest < ApplicationSystemTestCase + setup do + sign_in users(:admin) + end + + def test_download_accounts + now = Time.zone.parse('2010-07-05 08:00') + travel_to now + + get admin_accounts_path(format: :csv) + + assert_response :ok + assert_equal 'text/csv; charset=utf-8', response.headers['Content-Type'] + assert_equal %(attachment; filename="accounts_#{Time.zone.now.to_formatted_s(:number)}.csv"; filename*=UTF-8''accounts_#{Time.zone.now.to_formatted_s(:number)}.csv), + response.headers['Content-Disposition'] + assert_not_empty response.body + end +end From 6f5cd11859542186a6ee0d52a548a1c2ed3ba2fe Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 18 Aug 2021 22:53:05 +0300 Subject: [PATCH 02/13] added tests --- config/locales/admin/accounts.en.yml | 4 +--- test/system/admin_area/accounts_test.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/config/locales/admin/accounts.en.yml b/config/locales/admin/accounts.en.yml index d94b1775d..d0e2d115a 100644 --- a/config/locales/admin/accounts.en.yml +++ b/config/locales/admin/accounts.en.yml @@ -1,4 +1,5 @@ en: + edit_balance: Edit balance admin: accounts: index: @@ -16,6 +17,3 @@ en: registrar_name: Registrar reset_btn: Reset download_csv_btn: CSV - - account: - edit_balance: Edit balance diff --git a/test/system/admin_area/accounts_test.rb b/test/system/admin_area/accounts_test.rb index 7c8344080..1316e63b6 100644 --- a/test/system/admin_area/accounts_test.rb +++ b/test/system/admin_area/accounts_test.rb @@ -3,6 +3,7 @@ require 'application_system_test_case' class AdminAccountsSystemTest < ApplicationSystemTestCase setup do sign_in users(:admin) + @account = registrars(:bestnames).cash_account end def test_download_accounts @@ -17,4 +18,16 @@ class AdminAccountsSystemTest < ApplicationSystemTestCase response.headers['Content-Disposition'] assert_not_empty response.body end + + def test_change_account_balance + puts @account.inspect + visit edit_admin_account_path(@account) + assert_button 'Save' + assert_field 'Balance' + fill_in 'Balance', with: '234' + click_on 'Save' + + assert_text 'Account has been successfully updated' + assert_text '234' + end end From ee442beb2e538b1cba7f19b01ce1a56662af1c9b Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 18 Aug 2021 22:57:44 +0300 Subject: [PATCH 03/13] fixed views --- app/views/admin/accounts/_search_form.haml | 34 ---------------------- app/views/admin/accounts/new.html.erb | 2 +- 2 files changed, 1 insertion(+), 35 deletions(-) delete mode 100644 app/views/admin/accounts/_search_form.haml diff --git a/app/views/admin/accounts/_search_form.haml b/app/views/admin/accounts/_search_form.haml deleted file mode 100644 index f9cbfccaa..000000000 --- a/app/views/admin/accounts/_search_form.haml +++ /dev/null @@ -1,34 +0,0 @@ -.row - .col-md-12 - = search_form_for [:admin, @q], html: { style: 'margin-bottom: 0;', class: 'js-form', autocomplete: 'off' } do |f| - .row - .col-md-3 - .form-group - = label_tag :name - = f.search_field :name, value: params[:q][:name], class: 'form-control', placeholder: t(:name) - .col-md-3 - .form-group - = label_tag :registrant - = f.search_field :registrant, value: params[:q][:registrant], class: 'form-control', placeholder: t('.registrant_placeholder') - .col-md-3 - .form-group - = label_tag t(:registrar_name) - = select_tag '[q][registrar]', options_for_select(Registrar.all.map { |x| [x, x.name] }, selected: params[:q][:registrar]), { include_blank: true, class: 'form-control', placeholder: t('.registrant')} - .col-md-3 - .form-group - = label_tag :action - = select_tag '[q][event]', options_for_select([['Update', 'update'], ['Destroy', 'destroy'], ['Create', 'create']], params[:q][:event]), { include_blank:true, multiple: false, placeholder: t(:choose), class: 'form-control js-combobox' } - .row - .col-md-3 - .col-md-3 - .col-md-3 - .form-group - = label_tag t(:results_per_page) - = text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) - .col-md-3{style: 'padding-top: 25px;'} - %button.btn.btn-primary -   - %span.glyphicon.glyphicon-search -   - = link_to(t('.reset_btn'), admin_domain_versions_path, class: 'btn btn-default') -%hr diff --git a/app/views/admin/accounts/new.html.erb b/app/views/admin/accounts/new.html.erb index 562a15b8e..8556f537a 100644 --- a/app/views/admin/accounts/new.html.erb +++ b/app/views/admin/accounts/new.html.erb @@ -1,5 +1,5 @@

New Admin Account

-<%= render 'form', admin_account: @admin_account %> +<%= render 'form', account: @account %> <%= link_to 'Back', admin_accounts_path %> From b425a14f39ca829a4e2b5bcac420b2a3e4a7e3c5 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 19 Aug 2021 09:26:17 +0300 Subject: [PATCH 04/13] added activity type --- app/controllers/admin/accounts_controller.rb | 2 +- app/models/account_activity.rb | 3 ++- config/locales/en.yml | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index c3f72371a..5f7616b3e 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -30,7 +30,7 @@ module Admin sum: @sum, currency: @account.currency, description: params[:description], - activity_type: AccountActivity::ADD_CREDIT) + activity_type: AccountActivity::UPDATE_CREDIT) if activity.save true diff --git a/app/models/account_activity.rb b/app/models/account_activity.rb index 9df64209a..d59670995 100644 --- a/app/models/account_activity.rb +++ b/app/models/account_activity.rb @@ -8,6 +8,7 @@ class AccountActivity < ApplicationRecord CREATE = 'create' RENEW = 'renew' ADD_CREDIT = 'add_credit' + UPDATE_CREDIT = 'update_credit' after_create :update_balance def update_balance @@ -20,7 +21,7 @@ class AccountActivity < ApplicationRecord class << self def types_for_select - [CREATE, RENEW, ADD_CREDIT].map { |x| [I18n.t(x), x] } + [CREATE, RENEW, ADD_CREDIT, UPDATE_CREDIT].map { |x| [I18n.t(x), x] } end def to_csv diff --git a/config/locales/en.yml b/config/locales/en.yml index 4566d2302..2ef9a5b81 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -603,6 +603,7 @@ en: receipt_date_from: 'Receipt date from' receipt_date_until: 'Receipt date until' add_credit: 'Add credit' + update_credit: 'Update credit' invalid_yaml: 'Invalid YAML' reserved_pw: 'Reserved pw' no_transfers_found: 'No transfers found' From 48555c23b615f6487a9014bcd3ec83b20ccf7fdf Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 19 Aug 2021 09:42:15 +0300 Subject: [PATCH 05/13] fixed codeclimate errors --- app/controllers/admin/accounts_controller.rb | 2 +- app/models/account_activity.rb | 8 ++++---- app/views/admin/accounts/index.html.erb | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index 5f7616b3e..2759ebfd3 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -5,7 +5,7 @@ module Admin def index @q = Account.includes(:registrar).search(params[:q]) @accounts = @q.result.page(params[:page]) - @accounts = @accounts.per(params[:results_per_page]) if params[:results_per_page].to_i.positive? + @accounts = @accounts.per(params[:results_per_page]) if paginate? render_by_format('admin/accounts/index', 'accounts') end diff --git a/app/models/account_activity.rb b/app/models/account_activity.rb index d59670995..432d444d8 100644 --- a/app/models/account_activity.rb +++ b/app/models/account_activity.rb @@ -5,10 +5,10 @@ class AccountActivity < ApplicationRecord belongs_to :invoice belongs_to :price, class_name: 'Billing::Price' - CREATE = 'create' - RENEW = 'renew' - ADD_CREDIT = 'add_credit' - UPDATE_CREDIT = 'update_credit' + CREATE = 'create'.freeze + RENEW = 'renew'.freeze + ADD_CREDIT = 'add_credit'.freeze + UPDATE_CREDIT = 'update_credit'.freeze after_create :update_balance def update_balance diff --git a/app/views/admin/accounts/index.html.erb b/app/views/admin/accounts/index.html.erb index 60ecdb15b..778d3d0af 100644 --- a/app/views/admin/accounts/index.html.erb +++ b/app/views/admin/accounts/index.html.erb @@ -18,7 +18,7 @@ - <%= render @accounts %> + <%= render partial: 'account', collection: @accounts %> From 83b9c58ad36f385abc838dbf86716f05bd777639 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 23 Aug 2021 09:11:41 +0300 Subject: [PATCH 06/13] test fixed, controller refactored --- app/controllers/admin/accounts_controller.rb | 25 +++++++---------- .../actions/account_activity_create.rb | 27 +++++++++++++++++++ test/system/admin_area/accounts_test.rb | 1 - 3 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 app/interactions/actions/account_activity_create.rb diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index 2759ebfd3..a749f948f 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -17,7 +17,16 @@ module Admin def update if @account.valid? @sum = params[:account][:balance].to_f - @account.balance - redirect_to admin_accounts_path, notice: t('.updated') if create_activity + action = Actions::AccountActivityCreate.new(@account, + @sum, + params[:description], + AccountActivity::UPDATE_CREDIT) + + unless action.call + handle_errors(@account) + render 'edit' + end + redirect_to admin_accounts_path, notice: t('.updated') else render 'edit' end @@ -25,20 +34,6 @@ module Admin private - def create_activity - activity = AccountActivity.new(account: @account, - sum: @sum, - currency: @account.currency, - description: params[:description], - activity_type: AccountActivity::UPDATE_CREDIT) - - if activity.save - true - else - false - end - end - def account_params params.require(:account).permit(:id, :currency, :balance) end diff --git a/app/interactions/actions/account_activity_create.rb b/app/interactions/actions/account_activity_create.rb new file mode 100644 index 000000000..41b351fbe --- /dev/null +++ b/app/interactions/actions/account_activity_create.rb @@ -0,0 +1,27 @@ +module Actions + class AccountActivityCreate + def initialize(account, sum, description, type) + @account = account + @sum = sum + @description = description + @type = type + end + + def call + create_activity + commit + end + + def create_activity + @activity = AccountActivity.new(account: @account, + sum: @sum, + currency: @account.currency, + description: @description, + activity_type: @type) + end + + def commit + @activity.save! + end + end +end diff --git a/test/system/admin_area/accounts_test.rb b/test/system/admin_area/accounts_test.rb index 1316e63b6..f07ced9c3 100644 --- a/test/system/admin_area/accounts_test.rb +++ b/test/system/admin_area/accounts_test.rb @@ -20,7 +20,6 @@ class AdminAccountsSystemTest < ApplicationSystemTestCase end def test_change_account_balance - puts @account.inspect visit edit_admin_account_path(@account) assert_button 'Save' assert_field 'Balance' From d25847571fddddd185d3f57b24fe479030b6dac9 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 23 Aug 2021 16:38:44 +0300 Subject: [PATCH 07/13] fixed codeclimate errors --- app/controllers/admin/accounts_controller.rb | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index a749f948f..6dc6d9f5e 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -17,19 +17,12 @@ module Admin def update if @account.valid? @sum = params[:account][:balance].to_f - @account.balance - action = Actions::AccountActivityCreate.new(@account, - @sum, - params[:description], + action = Actions::AccountActivityCreate.new(@account, @sum, params[:description], AccountActivity::UPDATE_CREDIT) - - unless action.call - handle_errors(@account) - render 'edit' - end - redirect_to admin_accounts_path, notice: t('.updated') - else - render 'edit' + redirect_to admin_accounts_path, notice: t('.updated') if action.call end + + render 'edit' end private From 43518b58325a3f4b82e5712027f606d54b6d38bc Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 23 Aug 2021 21:32:54 +0300 Subject: [PATCH 08/13] added action --- app/controllers/admin/accounts_controller.rb | 8 +++---- .../actions/account_activity_create.rb | 22 +++++++++++++++++-- config/locales/admin/accounts.en.yml | 2 ++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index 6dc6d9f5e..aa656d4f0 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -16,12 +16,12 @@ module Admin def update if @account.valid? - @sum = params[:account][:balance].to_f - @account.balance - action = Actions::AccountActivityCreate.new(@account, @sum, params[:description], - AccountActivity::UPDATE_CREDIT) - redirect_to admin_accounts_path, notice: t('.updated') if action.call + action = Actions::AccountActivityCreate.new(@account, params[:account][:balance], + params[:description], AccountActivity::UPDATE_CREDIT) + redirect_to admin_accounts_path, notice: t('.updated') and return if action.call end + flash[:alert] = t('invalid_balance') render 'edit' end diff --git a/app/interactions/actions/account_activity_create.rb b/app/interactions/actions/account_activity_create.rb index 41b351fbe..f76cdcf76 100644 --- a/app/interactions/actions/account_activity_create.rb +++ b/app/interactions/actions/account_activity_create.rb @@ -1,17 +1,35 @@ module Actions class AccountActivityCreate - def initialize(account, sum, description, type) + def initialize(account, new_balance, description, type) @account = account - @sum = sum + @new_balance = new_balance @description = description @type = type end def call + validate_new_balance + return false if @error + + calc_sum create_activity commit end + def calc_sum + @sum = @new_balance.to_f - @account.balance + end + + def validate_new_balance + return if @new_balance.blank? + + begin + !Float(@new_balance).nil? + rescue StandardError + @error = true + end + end + def create_activity @activity = AccountActivity.new(account: @account, sum: @sum, diff --git a/config/locales/admin/accounts.en.yml b/config/locales/admin/accounts.en.yml index d0e2d115a..8566f335e 100644 --- a/config/locales/admin/accounts.en.yml +++ b/config/locales/admin/accounts.en.yml @@ -1,5 +1,7 @@ en: edit_balance: Edit balance + invalid_balance: The balance must be a number + admin: accounts: index: From b8cf5a9009392de96981cf9280a37b4e6ff996a3 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 23 Aug 2021 21:37:28 +0300 Subject: [PATCH 09/13] fixed codeclimate errors --- app/controllers/admin/accounts_controller.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index aa656d4f0..3ed592092 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -16,9 +16,11 @@ module Admin def update if @account.valid? - action = Actions::AccountActivityCreate.new(@account, params[:account][:balance], - params[:description], AccountActivity::UPDATE_CREDIT) - redirect_to admin_accounts_path, notice: t('.updated') and return if action.call + action = Actions::AccountActivityCreate.new(@account, + params[:account][:balance], + params[:description], + AccountActivity::UPDATE_CREDIT) + redirect_to admin_accounts_path, notice: t('.updated') && return if action.call end flash[:alert] = t('invalid_balance') From f08e95642d9f525cbe4ec7ffee5ec6e7ee579b86 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 23 Aug 2021 22:22:10 +0300 Subject: [PATCH 10/13] fixed error --- app/controllers/admin/accounts_controller.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index 3ed592092..fa22ecbf2 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -15,16 +15,16 @@ module Admin def edit; end def update - if @account.valid? - action = Actions::AccountActivityCreate.new(@account, - params[:account][:balance], - params[:description], - AccountActivity::UPDATE_CREDIT) - redirect_to admin_accounts_path, notice: t('.updated') && return if action.call + action = Actions::AccountActivityCreate.new(@account, + params[:account][:balance], + params[:description], + AccountActivity::UPDATE_CREDIT) + unless action.call + flash[:alert] = t('invalid_balance') + render 'edit' end - flash[:alert] = t('invalid_balance') - render 'edit' + redirect_to admin_accounts_path, notice: t('.updated') if action.call end private From 7be7282d89050ec6e9eb44fd93c0ec231d9b9997 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 23 Aug 2021 22:39:13 +0300 Subject: [PATCH 11/13] minor fixes --- app/controllers/admin/accounts_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index fa22ecbf2..2f0545752 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -19,12 +19,12 @@ module Admin params[:account][:balance], params[:description], AccountActivity::UPDATE_CREDIT) - unless action.call + if action.call + redirect_to admin_accounts_path, notice: t('.updated') + else flash[:alert] = t('invalid_balance') render 'edit' end - - redirect_to admin_accounts_path, notice: t('.updated') if action.call end private From ce218c34b9a4c68766d398db1f03c34b86efc0f5 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Tue, 24 Aug 2021 21:50:40 +0300 Subject: [PATCH 12/13] strong params removed --- app/controllers/admin/accounts_controller.rb | 6 ------ app/interactions/actions/account_activity_create.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index 2f0545752..756bbc662 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -26,11 +26,5 @@ module Admin render 'edit' end end - - private - - def account_params - params.require(:account).permit(:id, :currency, :balance) - end end end diff --git a/app/interactions/actions/account_activity_create.rb b/app/interactions/actions/account_activity_create.rb index f76cdcf76..b1773736b 100644 --- a/app/interactions/actions/account_activity_create.rb +++ b/app/interactions/actions/account_activity_create.rb @@ -21,7 +21,7 @@ module Actions end def validate_new_balance - return if @new_balance.blank? + @error = true and return if @new_balance.blank? begin !Float(@new_balance).nil? From a6b37ff5ac3e8ef66b86a9566e69c207c6049979 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Tue, 24 Aug 2021 22:00:30 +0300 Subject: [PATCH 13/13] fixed codeclimate error --- app/interactions/actions/account_activity_create.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/interactions/actions/account_activity_create.rb b/app/interactions/actions/account_activity_create.rb index b1773736b..1bfb53baf 100644 --- a/app/interactions/actions/account_activity_create.rb +++ b/app/interactions/actions/account_activity_create.rb @@ -21,12 +21,14 @@ module Actions end def validate_new_balance - @error = true and return if @new_balance.blank? - - begin - !Float(@new_balance).nil? - rescue StandardError + if @new_balance.blank? @error = true + else + begin + !Float(@new_balance).nil? + rescue StandardError + @error = true + end end end