Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
Fix slugged models with polymorphic relationships. Closes norman#65.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwood authored and norman committed Apr 26, 2010
1 parent 67db7b4 commit ba99498
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/friendly_id/active_record_adapter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ def autodiscover_cache_column

def associated_friendly_classes
configured_class.reflect_on_all_associations.select { |assoc|
assoc.klass.uses_friendly_id? }.map(&:klass)
!assoc.options[:polymorphic] &&
assoc.klass.uses_friendly_id?
}.map(&:klass)
end

end
end
end
end
11 changes: 11 additions & 0 deletions test/active_record_adapter/ar_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,15 @@ class Novel < ::Book
# A model with no table
class Question < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end

# A model to test polymorphic associations
class Site < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
has_friendly_id :name, :use_slug => true
end

# A model used as a polymorphic owner
class Company < ActiveRecord::Base
has_many :sites, :as => :owner
end
7 changes: 7 additions & 0 deletions test/active_record_adapter/scoped_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def setup
@canada = Country.create!(:name => "Canada")
@resident = Resident.create!(:name => "John Smith", :country => @usa)
@resident2 = Resident.create!(:name => "John Smith", :country => @canada)
@owner = Company.create!(:name => "Acme Events")
@site = Site.create!(:name => "Downtown Venue", :owner => @owner)
end

def teardown
Expand Down Expand Up @@ -105,6 +107,11 @@ def teardown
assert_match(/scope: badscope/, e.message)
end
end

test "should update the sluggable field when a polymorphic relationship exists" do
@site.update_attributes(:name => "Uptown Venue")
assert_equal "Uptown Venue", @site.name
end

end
end
Expand Down
9 changes: 9 additions & 0 deletions test/active_record_adapter/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def self.up
end
add_index :users, :name, :unique => true

create_table :sites do |t|
t.string :name
t.integer :owner_id
t.string :owner_type
end

create_table :companies do |t|
t.string :name
end
end

def self.down
Expand Down

0 comments on commit ba99498

Please sign in to comment.