Skip to content

Commit

Permalink
Avoid checking dependent scopes unless necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
norman committed Aug 9, 2010
1 parent 68c2f4a commit 03e48a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/friendly_id/active_record_adapter/slugged_model.rb
Expand Up @@ -84,6 +84,7 @@ def update_scope
# Update the slugs for any model that is using this model as its
# FriendlyId scope.
def update_dependent_scopes
return unless friendly_id_config.class.scopes_used?
if slugs(true).size > 1 && @new_friendly_id
friendly_id_config.child_scopes.each do |klass|
Slug.update_all "scope = '#{@new_friendly_id}'", ["sluggable_type = ? AND scope = ?",
Expand Down
20 changes: 19 additions & 1 deletion lib/friendly_id/configuration.rb
Expand Up @@ -64,7 +64,7 @@ class Configuration
attr_accessor :reserved_words

# The method or relation to use as the friendly_id's scope.
attr_accessor :scope
attr_reader :scope

# The string that separates slug names from slug sequences. Defaults to "--".
attr_accessor :sequence_separator
Expand Down Expand Up @@ -102,6 +102,24 @@ def reserved_error_message(word)
[method, reserved_message % word] if reserved? word
end

def scope=(scope)
self.class.scopes_used = true
@scope = scope
end

# This will be set if FriendlyId's scope feature is used in any model. It is here
# to provide a way to avoid invoking costly scope lookup methods when the scoped
# slug feature is not being used by any models.
def self.scopes_used=(val)
@scopes_used = !!val
end

# Are scoped slugs being used by any model?
# @see Configuration.scoped_used=
def self.scopes_used?
@scopes_used
end

%w[approximate_ascii scope strip_non_ascii use_slug].each do |method|
class_eval(<<-EOM)
def #{method}?
Expand Down
3 changes: 1 addition & 2 deletions test/active_record_adapter/scoped_model_test.rb
Expand Up @@ -121,7 +121,6 @@ def teardown
Resident.friendly_id_config.send(:associated_friendly_classes)
end
end

end
end
end
end

0 comments on commit 03e48a3

Please sign in to comment.