Skip to content

Instantly share code, notes, and snippets.

@ernie
Created October 4, 2010 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ernie/610121 to your computer and use it in GitHub Desktop.
Save ernie/610121 to your computer and use it in GitHub Desktop.
From 6cb5f1f85c8af1a37a6a27e6f69c0f41a84bbf2f Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Mon, 4 Oct 2010 13:35:38 -0400
Subject: [PATCH 1/2] Convert to model before calling model_name on a record in ActiveModel::Naming
---
activemodel/lib/active_model/naming.rb | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index 2d580fd..adb71f7 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -129,7 +129,11 @@ module ActiveModel
private
def self.model_name_from_record_or_class(record_or_class)
- (record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name
+ (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
+ end
+
+ def self.convert_to_model(object)
+ object.respond_to?(:to_model) ? object.to_model : object
end
end
--
1.7.2.2
From 6136766a182f374de99f1e33c1c3f1bfe085e894 Mon Sep 17 00:00:00 2001
From: Ernie Miller <ernie@metautonomo.us>
Date: Mon, 4 Oct 2010 15:04:39 -0400
Subject: [PATCH 2/2] Test to_model being called in ActiveModel::Naming helpers
---
activemodel/test/cases/naming_test.rb | 4 ++++
activemodel/test/models/track_back.rb | 7 +++++++
2 files changed, 11 insertions(+), 0 deletions(-)
diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb
index 40ce4c0..a7dde2c 100644
--- a/activemodel/test/cases/naming_test.rb
+++ b/activemodel/test/cases/naming_test.rb
@@ -125,6 +125,10 @@ class NamingHelpersTest < Test::Unit::TestCase
@param_key = 'contact'
end
+ def test_to_model_called_on_record
+ assert_equal 'post_named_track_backs', plural(Post::TrackBack.new)
+ end
+
def test_singular
assert_equal @singular, singular(@record)
end
diff --git a/activemodel/test/models/track_back.rb b/activemodel/test/models/track_back.rb
index d137e4f..545acd1 100644
--- a/activemodel/test/models/track_back.rb
+++ b/activemodel/test/models/track_back.rb
@@ -1,4 +1,11 @@
class Post
class TrackBack
+ def to_model
+ NamedTrackBack.new(self)
+ end
+ end
+
+ class NamedTrackBack
+ extend ActiveModel::Naming
end
end
\ No newline at end of file
--
1.7.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment