Skip to content

Commit

Permalink
Fix class_name when in a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed Jun 29, 2022
1 parent a40364f commit 3b7643f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
13 changes: 6 additions & 7 deletions lib/ar2dto/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/ar2dto/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions spec/ar2dto/record_to_dto/namespaces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/support/fixtures/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module Core
end
11 changes: 11 additions & 0 deletions spec/support/fixtures/shop/line_item.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3b7643f

Please sign in to comment.