Skip to content

Commit

Permalink
Merge pull request #562 from xymbol/fix-scope
Browse files Browse the repository at this point in the history
Drop conditional in slug generator scope
  • Loading branch information
norman committed May 29, 2014
2 parents 1f9f903 + 07af270 commit f1d8649
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
7 changes: 2 additions & 5 deletions lib/friendly_id/scoped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@ def scope_for_slug_generator
friendly_id_config.scope_columns.each do |column|
relation = relation.where(column => send(column))
end
if changed.include?(friendly_id_config.slug_column)
primary_key_name = self.class.primary_key
relation = relation.where.not(primary_key_name => send(primary_key_name))
end
relation
primary_key_name = self.class.primary_key
relation.where.not(primary_key_name => send(primary_key_name))
end
private :scope_for_slug_generator

Expand Down
8 changes: 2 additions & 6 deletions lib/friendly_id/slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,8 @@ def set_slug(normalized_slug = nil)
def scope_for_slug_generator
scope = self.class.base_class.unscoped
scope = scope.friendly unless scope.respond_to?(:exists_by_friendly_id?)

if changed.include?(friendly_id_config.slug_column)
primary_key_name = self.class.primary_key
scope = scope.where.not(primary_key_name => send(primary_key_name))
end
scope
primary_key_name = self.class.primary_key
scope.where.not(primary_key_name => send(primary_key_name))
end
private :scope_for_slug_generator

Expand Down
15 changes: 15 additions & 0 deletions test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def self.name
assert_equal old_id, record.friendly_id
end
end

test "should not update matching slug" do
with_instance_of(model_class) do |record|
class << record
def should_generate_new_friendly_id?
name_changed?
end
end
old_id = record.friendly_id
record.name += " "
record.save!
assert_equal old_id, record.friendly_id
end
end

end

class SlugGeneratorTest < MiniTest::Unit::TestCase
Expand Down

0 comments on commit f1d8649

Please sign in to comment.