From 19bebc7299a4f913604e6fd1df78896beae1f5c0 Mon Sep 17 00:00:00 2001 From: Santiago Bartesaghi Date: Mon, 27 Jun 2022 21:28:07 -0300 Subject: [PATCH] Fix class_name when in a namespace --- lib/ar2dto/model_config.rb | 13 ++++++------- spec/ar2dto/options_spec.rb | 6 ++++++ spec/spec_helper.rb | 2 ++ spec/support/fixtures/core.rb | 4 ++++ spec/support/fixtures/payments/transaction.rb | 11 +++++++++++ spec/support/schema.rb | 6 ++++++ 6 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 spec/support/fixtures/core.rb create mode 100644 spec/support/fixtures/payments/transaction.rb diff --git a/lib/ar2dto/model_config.rb b/lib/ar2dto/model_config.rb index 4672a0a..a084d73 100644 --- a/lib/ar2dto/model_config.rb +++ b/lib/ar2dto/model_config.rb @@ -13,26 +13,25 @@ def setup_config(configs) end def except - configs["except"] + @except ||= configs["except"] end def class_name - configs["class_name"] || - model_name.sub(/#{configs["replace_suffix"]["from"]}$/, configs["replace_suffix"]["to"].to_s) + @class_name ||= namespaced_class_name.split("::").last end def namespace - @namespace ||= model.name.deconstantize.presence&.constantize || Object + @namespace ||= namespaced_class_name.deconstantize.presence&.constantize || Object end def namespaced_class_name - "#{namespace}::#{class_name}" + @namespaced_class_name ||= configs["class_name"] || model_name_replaced_suffix end private - def model_name - model.name.split("::").last + def model_name_replaced_suffix + model.name.sub(/#{configs["replace_suffix"]["from"]}$/, configs["replace_suffix"]["to"].to_s) end end end diff --git a/spec/ar2dto/options_spec.rb b/spec/ar2dto/options_spec.rb index f837d17..44c8f9a 100644 --- a/spec/ar2dto/options_spec.rb +++ b/spec/ar2dto/options_spec.rb @@ -31,6 +31,12 @@ def self.name it "respects the custom class_name" do expect(Car.new.to_dto).to be_a(VehicleDTO) end + + context "with a namespace" do + it "respects the custom class_name" do + expect(Payments::Transaction.new.to_dto).to be_a(Core::TransactionDTO) + end + end end describe "option replace_suffix" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6f782e1..4758b1e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,6 +11,8 @@ require "support/active_model" require "support/schema" require "support/fixtures/car" +require "support/fixtures/core" +require "support/fixtures/payments/transaction" require "support/fixtures/person_dto" require "support/fixtures/person" require "support/fixtures/shop/order" diff --git a/spec/support/fixtures/core.rb b/spec/support/fixtures/core.rb new file mode 100644 index 0000000..87cb4ca --- /dev/null +++ b/spec/support/fixtures/core.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +module Core +end diff --git a/spec/support/fixtures/payments/transaction.rb b/spec/support/fixtures/payments/transaction.rb new file mode 100644 index 0000000..bf72968 --- /dev/null +++ b/spec/support/fixtures/payments/transaction.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Payments + class Transaction < ActiveRecord::Base + self.table_name = "payments_transactions" + + belongs_to :shop_order, class_name: "Shop::Order" + + has_dto class_name: "Core::TransactionDTO" + end +end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index e746ba9..a87451f 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -32,4 +32,10 @@ t.timestamps end + + create_table :payments_transactions, force: true do |t| + t.bigint :shop_order_id + + t.string :result + end end