Skip to content

Commit

Permalink
Fix issues with subclassing, such as when using ActiveRecord STI.
Browse files Browse the repository at this point in the history
Fixes mobility to work if subclassing a subclass of a mobility-enabled class, and looks up mobility_backend_classes later in execution (upon being called) to be more flexible.
  • Loading branch information
mrbrdo authored and shioyama committed Jun 18, 2022
1 parent 10fd352 commit b3d8806
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/mobility/plugins/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,22 @@ def mobility_backend_class(name)
raise KeyError, "No backend for: #{name}"
end

def inherited(klass)
parent_classes = mobility_backend_classes.freeze # ensure backend classes are not modified after being inherited
klass.class_eval { @mobility_backend_classes = parent_classes.dup }
super
end

protected

def mobility_backend_classes
@mobility_backend_classes ||= {}
if @mobility_backend_classes
@mobility_backend_classes
else
@mobility_backend_classes = {}
parent_class = self.superclass
while parent_class&.respond_to?(:mobility_backend_classes, true)
# ensure backend classes are not modified after being inherited
parent_class_classes = parent_class.mobility_backend_classes.freeze
@mobility_backend_classes.merge!(parent_class_classes)
parent_class = parent_class.superclass
end
@mobility_backend_classes
end
end
end

Expand Down

0 comments on commit b3d8806

Please sign in to comment.