Skip to content

fix(smart-field): smart fields declared on parent sti are now computed on subclasses #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
20 changes: 20 additions & 0 deletions lib/forest_liana/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def field(name, opts, &block)

if serializer_name && ForestLiana::UserSpace.const_defined?(
serializer_name)
compute_value = nil
ForestLiana::UserSpace.const_get(serializer_name).class_eval do
if block
# NOTICE: Smart Field case.
Expand All @@ -104,6 +105,18 @@ def field(name, opts, &block)
attribute(name)
end
end

# In case of smart field declare on STI class
# the smart field was added on these subclasses
unless active_record_class.subclasses.empty?
active_record_class.subclasses.each do |subclass|
if ForestLiana::UserSpace.const_defined?(serializer_name_for(subclass))
ForestLiana::UserSpace.const_get(serializer_name_for(subclass)).class_eval do
attribute(name, &compute_value)
end
end
end
end
end
end

Expand Down Expand Up @@ -205,6 +218,13 @@ def serializer_name
end
end

def serializer_name_for(subclass)
component_prefix = ForestLiana.component_prefix(subclass)
serializer_name = "#{component_prefix}Serializer"

"ForestLiana::UserSpace::#{serializer_name}"
end

def serializer_name_for_reference(reference)
association = opts[:reference].split('.').first
component_prefix = association.classify
Expand Down
Loading