lib/friendly_id/base.rb in friendly_id-4.0.7 vs lib/friendly_id/base.rb in friendly_id-4.0.8

- old
+ new

@@ -197,15 +197,13 @@ # @note In the case of Single Table Inheritance (STI), this method will # duplicate the parent class's FriendlyId::Configuration and relation class # on first access. If you're concerned about thread safety, then be sure # to invoke {#friendly_id} in your class for each model. def friendly_id_config - @friendly_id_config or begin - @friendly_id_config = base_class.friendly_id_config.dup.tap do |config| - config.model_class = self - @relation_class = base_class.send(:relation_class) - end + @friendly_id_config ||= base_class.friendly_id_config.dup.tap do |config| + config.model_class = self + @relation_class = base_class.send(:relation_class) end end private @@ -215,17 +213,17 @@ @relation = nil unless @relation.class <= relation_class @relation ||= relation_class.new(self, arel_table) super end - # Gets an anonymous subclass of the model's relation class. + # Gets (and if necessary, creates) a subclass of the model's relation class. # # Rather than including FriendlyId's overridden finder methods in - # ActiveRecord::Relation directly, FriendlyId adds them to the anonymous - # subclass, and makes #relation return an instance of this class. By doing - # this, we ensure that only models that specifically extend FriendlyId have - # their finder methods overridden. + # ActiveRecord::Relation directly, FriendlyId adds them to a subclass + # specific to the AR model, and makes #relation return an instance of this + # class. By doing this, we ensure that only models that specifically extend + # FriendlyId have their finder methods overridden. # # Note that this method does not directly subclass ActiveRecord::Relation, # but rather whatever class the @relation class instance variable is an # instance of. In practice, this will almost always end up being # ActiveRecord::Relation, but in case another plugin is using this same @@ -238,13 +236,19 @@ # since the authors of Rails are probably not intending this kind of usage # against a private API. If this ends up being problematic I will probably # revert back to the old behavior of simply extending # ActiveRecord::Relation. def relation_class - @relation_class ||= Class.new(relation_without_friendly_id.class) do - alias_method :find_one_without_friendly_id, :find_one - alias_method :exists_without_friendly_id?, :exists? - include FriendlyId::FinderMethods + @relation_class or begin + @relation_class = Class.new(relation_without_friendly_id.class) do + alias_method :find_one_without_friendly_id, :find_one + alias_method :exists_without_friendly_id?, :exists? + include FriendlyId::FinderMethods + end + # Set a name so that model instances can be marshalled. Use a + # ridiculously long name that will not conflict with anything. + # TODO: just use the constant, no need for the @relation_class variable. + const_set('FriendlyIdActiveRecordRelation', @relation_class) end end end # Instance methods that will be added to all classes using FriendlyId.