Permalink
Browse files

Name anonymous class to fix marshalling.

Resolves #314
  • Loading branch information...
1 parent 88091d0 commit 451b88d228919616c6a665f8f86436bf38901667 @parndt parndt committed with Jul 22, 2012
Showing with 42 additions and 13 deletions.
  1. +15 −9 lib/friendly_id/base.rb
  2. +22 −2 test/core_test.rb
  3. +5 −2 test/schema.rb
View
@@ -215,13 +215,13 @@ def relation
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
@@ -238,10 +238,16 @@ def relation
# 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
View
@@ -1,13 +1,14 @@
require "helper"
-class Author < ActiveRecord::Base
+class Book < ActiveRecord::Base
extend FriendlyId
friendly_id :name
end
-class Book < ActiveRecord::Base
+class Author < ActiveRecord::Base
extend FriendlyId
friendly_id :name
+ has_many :books
end
class CoreTest < MiniTest::Unit::TestCase
@@ -32,4 +33,23 @@ def model_class
test "instances should have a friendly id" do
with_instance_of(model_class) {|record| assert record.friendly_id}
end
+
+ test "instances can be marshaled when a relationship is used" do
+ transaction do
+ author = Author.create :name => 'Philip'
+ author.books.create :name => 'my book'
+ begin
+ assert Marshal.load(Marshal.dump(author))
+ rescue TypeError => e
+ flunk(e.to_s)
+ end
+ end
+ end
+
+
+ # test "instances can be marshaled" do
+ # with_instance_of(model_class) do |record|
+ # assert Marshal.load(Marshal.dump(record))
+ # end
+ # end
end
View
@@ -54,17 +54,20 @@ def up
# This will be used to test globalize translations
TranslatedArticle.create_translation_table! :slug => :string, :title => :string
+ # This will be used to test relationships
+ add_column :books, :author_id, :integer
+
@done = true
end
private
def slugged_tables
- ["journalists", "articles", "novelists", "novels", "manuals", "translated_articles"]
+ %w[journalists articles novelists novels manuals translated_articles]
end
def simple_tables
- ["authors", "books", "publishers"]
+ %w[authors books publishers]
end
def tables

0 comments on commit 451b88d

Please sign in to comment.