Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ class << self
def self.serializer_lookup_chain_for(klass)
chain = []

resource_class_name = klass.name.demodulize
resource_namespace = klass.name.deconstantize
resource_class_name = klass.name.demodulize
resource_namespace = klass.name.deconstantize
serializer_class_name = "#{resource_class_name}Serializer"

chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer
chain.push("#{resource_namespace}::#{serializer_class_name}")
tmp_namespace = name.to_s
name_namespaces = [tmp_namespace]

until tmp_namespace.empty?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to have two loops here? I imagine you could add to the chain array as you deconstantized the namespaces.

Could you also add some documentation (comments) in here explaining how the chain array looks after different steps of this process?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NullVoxPopuli Good call! Thanks for the feedback!

Adjustments added in most recent commit. Let me know your thoughts!

found_namespace = tmp_namespace.deconstantize
name_namespaces << found_namespace

tmp_namespace = found_namespace
end

if self != ActiveModel::Serializer
name_namespaces.each do |ns|
chain.push("#{ns}::#{serializer_class_name}")
end
end

chain.push("#{resource_namespace}::#{serializer_class_name}")
chain
end

Expand Down