From fa5ddf6e7c4cb7ef27650c65825cb6152b29ec71 Mon Sep 17 00:00:00 2001 From: Przemek Kosakowski Date: Mon, 14 Oct 2019 20:10:14 +0200 Subject: [PATCH] CR remarks && migration/controller/view changes --- .../avalara_entity_use_codes_controller.rb | 20 ++++++++++--------- .../spree/admin/users_controller_decorator.rb | 2 +- .../spree/user_decorator.rb | 2 +- app/overrides/spree/admin/sidebar.rb | 2 +- .../transactions/create_presenter.rb | 2 +- .../admin/users/avalara_information.html.erb | 4 ++-- config/locales/en.yml | 2 +- ..._avatax_official_entity_use_codes_table.rb | 6 +++++- ...valara_entity_use_codes_controller_spec.rb | 8 ++++++-- .../transactions/create_presenter_spec.rb | 2 +- .../partial_refund_presenter_spec.rb | 2 +- 11 files changed, 31 insertions(+), 21 deletions(-) diff --git a/app/controllers/spree/admin/avalara_entity_use_codes_controller.rb b/app/controllers/spree/admin/avalara_entity_use_codes_controller.rb index b17a60a8..70ed06ca 100644 --- a/app/controllers/spree/admin/avalara_entity_use_codes_controller.rb +++ b/app/controllers/spree/admin/avalara_entity_use_codes_controller.rb @@ -16,29 +16,31 @@ def edit; end def update if @use_code.update(entity_use_codes_params) - flash[:notice] = t('spree_avatax_official.entity_use_code_updated') - redirect_to admin_avalara_entity_use_codes_path + redirect_to admin_avalara_entity_use_codes_path, success: t('spree_avatax_official.entity_use_code_updated') + else + redirect_to edit_admin_avalara_entity_use_code_path(@use_code), error: @use_code.errors.full_messages.to_sentence end end def create @use_code = SpreeAvataxOfficial::EntityUseCode.new(entity_use_codes_params) if @use_code.save - flash[:notice] = t('spree_avatax_official.entity_use_code_created') - redirect_to admin_avalara_entity_use_codes_path + redirect_to admin_avalara_entity_use_codes_path, success: t('spree_avatax_official.entity_use_code_created') else - flash[:error] = @use_code.errors.full_messages.to_sentence - redirect_to new_admin_avalara_entity_use_code_path + redirect_to new_admin_avalara_entity_use_code_path, error: @use_code.errors.full_messages.to_sentence end end def destroy if @use_code.destroy - flash[:notice] = t('spree_avatax_official.entity_use_code_removed') - redirect_to admin_avalara_entity_use_codes_path + flash[:success] = t('spree_avatax_official.entity_use_code_removed') else flash[:error] = @use_code.errors.full_messages.to_sentence - redirect_to admin_avalara_entity_use_codes_path + end + + respond_with(@use_code) do |format| + format.html { redirect_to admin_avalara_entity_use_codes_path } + format.js { render_js_for_destroy } end end diff --git a/app/controllers/spree/admin/users_controller_decorator.rb b/app/controllers/spree/admin/users_controller_decorator.rb index 521ce21a..e9e89a87 100644 --- a/app/controllers/spree/admin/users_controller_decorator.rb +++ b/app/controllers/spree/admin/users_controller_decorator.rb @@ -22,7 +22,7 @@ def load_use_codes end def user_params - params.require(:user).permit(:spree_avatax_official_entity_use_codes_id, :exemption_number, :vat_id) + params.require(:user).permit(:avatax_entity_use_code_id, :exemption_number, :vat_id) end end end diff --git a/app/models/spree_avatax_official/spree/user_decorator.rb b/app/models/spree_avatax_official/spree/user_decorator.rb index cd32fbad..9a9a6e28 100644 --- a/app/models/spree_avatax_official/spree/user_decorator.rb +++ b/app/models/spree_avatax_official/spree/user_decorator.rb @@ -2,7 +2,7 @@ module SpreeAvataxOfficial module Spree module UserDecorator def self.prepended(base) - base.has_one :avatax_entity_use_code, class_name: 'SpreeAvataxOfficial::EntityUseCode' + base.belongs_to :avatax_entity_use_code, class_name: 'SpreeAvataxOfficial::EntityUseCode' end end end diff --git a/app/overrides/spree/admin/sidebar.rb b/app/overrides/spree/admin/sidebar.rb index c6e7cbad..569ef912 100644 --- a/app/overrides/spree/admin/sidebar.rb +++ b/app/overrides/spree/admin/sidebar.rb @@ -6,7 +6,7 @@ <<~HTML
  • <%= link_to_with_icon 'money', - Spree.t('spree_avatax_official.information_url'), + t('spree_avatax_official.information_url'), avalara_information_admin_user_path(@user), class: "nav-link" %>
  • diff --git a/app/presenters/spree_avatax_official/transactions/create_presenter.rb b/app/presenters/spree_avatax_official/transactions/create_presenter.rb index ca6679b7..0f6cb1c6 100644 --- a/app/presenters/spree_avatax_official/transactions/create_presenter.rb +++ b/app/presenters/spree_avatax_official/transactions/create_presenter.rb @@ -37,7 +37,7 @@ def company_code end def entity_use_code - user.try(:spree_avatax_official_entity_use_codes_id) + user.avatax_entity_use_code.try(:code) end def formatted_date(date) diff --git a/app/views/spree/admin/users/avalara_information.html.erb b/app/views/spree/admin/users/avalara_information.html.erb index 253b9305..bace22cd 100644 --- a/app/views/spree/admin/users/avalara_information.html.erb +++ b/app/views/spree/admin/users/avalara_information.html.erb @@ -13,8 +13,8 @@
    <%= form_for [:admin, @user], method: :put do |f| %>
    - <%= label_tag :spree_avatax_official_entity_use_codes_id, Spree.t(:avalara_entity_use_code) %>
    - <%= f.select(:spree_avatax_official_entity_use_codes_id, @use_codes, { include_blank: false }, class: 'form-control' ) %> + <%= label_tag :avatax_entity_use_code_id, Spree.t(:avalara_entity_use_code) %>
    + <%= f.select(:avatax_entity_use_code_id, @use_codes, { include_blank: false }, class: 'form-control' ) %>
    diff --git a/config/locales/en.yml b/config/locales/en.yml index 3988662b..ec2fd45d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -32,6 +32,6 @@ en: connection_rejected: Avatax rejected connection entity_use_code_created: Entity use code created entity_use_code_updated: Entity use code updated - use_code_removed: Entity use code removed + entity_use_code_removed: Entity use code removed information_url: Avalara Information avalara_entity_use_code: Avalara Entity Use Code diff --git a/db/migrate/20191001092353_create_spree_avatax_official_entity_use_codes_table.rb b/db/migrate/20191001092353_create_spree_avatax_official_entity_use_codes_table.rb index 856c4379..49779ac0 100644 --- a/db/migrate/20191001092353_create_spree_avatax_official_entity_use_codes_table.rb +++ b/db/migrate/20191001092353_create_spree_avatax_official_entity_use_codes_table.rb @@ -9,6 +9,10 @@ def change t.timestamps end - add_reference :spree_users, :spree_avatax_official_entity_use_codes, type: :integer, index: true + return if column_exists?(:spree_users, :avatax_entity_use_code_id) + + change_table :spree_users do |t| + t.integer :avatax_entity_use_code_id, index: { unique: true }, foreign_key: true + end end end diff --git a/spec/features/spree/admin/avalara_entity_use_codes_controller_spec.rb b/spec/features/spree/admin/avalara_entity_use_codes_controller_spec.rb index a00c2895..b6485c8f 100644 --- a/spec/features/spree/admin/avalara_entity_use_codes_controller_spec.rb +++ b/spec/features/spree/admin/avalara_entity_use_codes_controller_spec.rb @@ -55,8 +55,12 @@ expect(page).to have_content(avalara_entity_use_code.code) - page.accept_confirm do - click_on('Delete') + within("tr#entity_use_code_#{avalara_entity_use_code.id}") do + page.accept_alert do + click_on('Delete') + end + + expect(page.document).to have_content('Entity use code removed') end visit spree.admin_avalara_entity_use_codes_path diff --git a/spec/presenters/spree_avatax_official/transactions/create_presenter_spec.rb b/spec/presenters/spree_avatax_official/transactions/create_presenter_spec.rb index 2fad784a..021e095a 100644 --- a/spec/presenters/spree_avatax_official/transactions/create_presenter_spec.rb +++ b/spec/presenters/spree_avatax_official/transactions/create_presenter_spec.rb @@ -25,7 +25,7 @@ discount: 0.0, currencyCode: order.currency, purchaseOrderNo: order.number, - entityUseCode: order.try(:user).try(:spree_avatax_official_entity_use_codes_id) + entityUseCode: order.try(:user).avatax_entity_use_code.try(:code) } end diff --git a/spec/presenters/spree_avatax_official/transactions/partial_refund_presenter_spec.rb b/spec/presenters/spree_avatax_official/transactions/partial_refund_presenter_spec.rb index fb08ceb2..04036788 100644 --- a/spec/presenters/spree_avatax_official/transactions/partial_refund_presenter_spec.rb +++ b/spec/presenters/spree_avatax_official/transactions/partial_refund_presenter_spec.rb @@ -24,7 +24,7 @@ lines: [SpreeAvataxOfficial::ItemPresenter.new(item: line_item, custom_quantity: quantity, custom_amount: amount).to_json], commit: true, discount: 0.0, - entityUseCode: order.try(:user).try(:spree_avatax_official_entity_use_codes_id), + entityUseCode: order.try(:user).avatax_entity_use_code.try(:code), currencyCode: order.currency, purchaseOrderNo: order.number, taxOverride: {