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 c62c521..b319c97 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(Shop::LineItem.new.to_dto).to be_a(Core::LineItemDTO) + end + end end describe "option replace_suffix" do diff --git a/spec/ar2dto/record_to_dto/namespaces_spec.rb b/spec/ar2dto/record_to_dto/namespaces_spec.rb index fb50f5c..9660a38 100644 --- a/spec/ar2dto/record_to_dto/namespaces_spec.rb +++ b/spec/ar2dto/record_to_dto/namespaces_spec.rb @@ -2,19 +2,30 @@ RSpec.describe "#to_dto" do describe "namespaces" do - context "with a namespaced model" do + let(:attributes) do + { + user_id: 1 + } + end + + subject { order.to_dto } + + let(:order) { Shop::Order.new(attributes) } + + it { is_expected.to be_a(Shop::OrderDTO) } + + context "with a custom class_name" do let(:attributes) do { - user_id: 1 + order_id: 1 } end - let(:options) { {} } - subject { order.to_dto(options) } + subject { line_item.to_dto } - let(:order) { Shop::Order.new(attributes) } + let(:line_item) { Shop::LineItem.new(attributes) } - it { is_expected.to be_a(Shop::OrderDTO) } + it { is_expected.to be_a(Core::LineItemDTO) } end end end 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/shop/line_item.rb b/spec/support/fixtures/shop/line_item.rb new file mode 100644 index 0000000..30c668a --- /dev/null +++ b/spec/support/fixtures/shop/line_item.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Shop + class LineItem < ActiveRecord::Base + self.table_name = "shop_line_items" + + belongs_to :order + + has_dto class_name: "Core::LineItemDTO" + end +end diff --git a/spec/support/schema.rb b/spec/support/schema.rb index e8eba9c..5d2f7fa 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -33,4 +33,12 @@ t.timestamps end + + create_table :shop_line_items, force: true do |t| + t.bigint :order_id + t.string :name + t.bigint :price + + t.timestamps + end end