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 28, 2022
1 parent a12fedf commit 19bebc7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 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(Payments::Transaction.new.to_dto).to be_a(Core::TransactionDTO)
end
end
end

describe "option replace_suffix" do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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/payments/transaction.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 19bebc7

Please sign in to comment.